2017-07-02 11 views
1

私はリストビューのアイテムリストをクリックするとトーストを表示しようとしています。listview.setOnItemClickListenerがCursorAdapterを使用していません

my listviewはうまくいきますが、問題は私のlistviewの項目をクリックしても実行されません。

ここは私のコードです。

MainActivity.java

lv.setAdapter(adapter); 

lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
     Toast.makeText(MainActivity.this, "Test", Toast.LENGTH_SHORT).show(); 
    } 
}); 

ClientCursorAdapter

class ClientCursorAdapter extends CursorAdapter { 
    Context context; 
    public ClientCursorAdapter(Context context, Cursor c, int flags) { 
     super(context, c, flags); 
     this.context = context; 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) { 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflater.inflate(R.layout.row_layout,viewGroup,false); 
     return v; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 

     TextView title = view.findViewById(R.id.Title); 
     TextView artist = view.findViewById(R.id.Artist); 
     ToggleButton music = view.findViewById(R.id.minus_one); 
     ToggleButton favorite = view.findViewById(R.id.favorite); 

     title.setText(cursor.getString(cursor.getColumnIndex("TITLE"))); 
     artist.setText(cursor.getString(cursor.getColumnIndex("ARTIST"))); 

    } 
} 
+0

あなたの「row_layout」レイアウトファイルを追加します。 –

答えて

1

はあなたのボタン(またはあなたがリスト項目内をクリックし処理したい任意の他のビュー)このように設定してみてください:

android:focusable="false" 
android:focusableInTouchMode="false" 

参考:https://stackoverflow.com/a/6703671/1084174

+1

はい、これはme.itの仕事のための最良の答えです。 –

0

の.javaあなたのリストビューの列がクリック可能か、フォーカス可能なビューの項目が含まれている場合は、onItemClickは呼び出されません。レイアウトにandroid:descendantFocusability="blocksDescendants"を設定するか、例えば、

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:descendantFocusability="blocksDescendants">  

</LinearLayout> 

またはあなたの項目、例えば、のImageButtonにandroid:focusable="false"を設定します。

+0

ありがとうございます。このコードも機能します。 :-) –

関連する問題