0

リストビューでアイテムをクリックすることで新しいインテントを開始しようとしていますが、これを機能させる方法がわかりません。

コードは次のとおりです。

final ListView lv = getListView();
lv.setTextFilterEnabled(true);  
lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")                  
            Intent intent = new Intent(this, Profileviewer.class);  
            startActivity(intent); 
        }
});

でコンパイラエラーが発生しますnew Intent(this, Profileviewer.class);

The constructor Intent(new AdapterView.OnItemClickListener(){}, Class<Profileviewer>) is undefined
4

3 に答える 3

4

アクティビティのコンテキストを(置くことによってYourActivity.this)インテントに渡す必要があります。これだけを渡すことによって、.を渡しAdapterView.OnItemClickListener()ます。

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
        @SuppressWarnings("unchecked")                  
        Intent intent = new Intent(YourActivity.this, Profileviewer.class);  
        startActivity(intent); 
    }
});
于 2012-07-30T08:38:50.200 に答える
1

AndroidManifest.xmlに新しいアクティビティを追加するのを忘れたと思います。

于 2012-07-30T08:44:08.080 に答える
0

あなたが言ったように、あなたはリストビューからインテントを起動しようとしています。コードでは、これはリストビューを意味します。それがエラーメッセージの内容です。以下のいずれかの方法でパッケージ名を使用する必要があります。

  1. Intent intent = new Intent({package_name}, Profileviewer.class);
  2. Intent intent = new Intent(Profileviewer.this, Profileviewer.class);
于 2012-07-30T08:43:46.857 に答える