-3

I have a list view with two text views and one edit text in each row listView.setOnItemClickListener() is not working.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Log.e("position", String.valueOf(position));
           }
       });

Log.e("position", String.valueOf(position)); not displaying on Logcat

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

1

If you items are coming with Model then create an object of your model in your setOnItemCLickListner like below code and then get data from model according to position.

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        ActivityModel booking = movieList.get(position);
                        String id = booking.getActivityId();
                        Toast.makeText(this, id , Toast.LENGTH_LONG).show();

                    }
                });

In your XML add below property in listview.

android:focusable="false"
Moin Khan
  • 674
  • 2
  • 9
  • 27
1

I think the focus is on the EditText. In XML add this to your editText view android:focusable="false"

Further read: https://stackoverflow.com/a/8955441/5954246

codeconscious
  • 396
  • 3
  • 15
1

This is because the editText is consuming your focus and thus no click is getting registered.

When I set android:focusable="false" for EditText then onItemClick on the ListView item is working, but EditText doesn't get cursor when I click inside.

If I'll set android:focusable="true" for EditText, then EditText is focusable, but onItemClick for the ListView doesn't work when I click on it.

So modify your adapter in this way:

    public class AdapterListCards extends CursorAdapter implements View.OnTouchListener {
 public AdapterListCards(Context context) {
    super(context, null, true);
 }

 @Override
 public boolean onTouch(View view, MotionEvent motionEvent) {
    if (view instanceof EditText) {
        EditText editText = (EditText) view;
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
    } else {
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.edtCode.setFocusable(false);
        holder.edtCode.setFocusableInTouchMode(false);
    }
    return false;
 }

 private class ViewHolder {
    TextView txtName;
    EditText edtCode;
}

 @Override
 public View newView(final Context context, Cursor cursor, ViewGroup parent) {
    View convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
    final ViewHolder holder = new ViewHolder();
    holder.txtName = (TextView) convertView.findViewById(R.id.txt_name);
    holder.edtCode = (EditText) convertView.findViewById(R.id.pass);
    holder.edtCode.setOnTouchListener(this);
    convertView.setOnTouchListener(this);
    convertView.setTag(holder);

    return convertView;
 }

@Override
public void bindView(View view, Context context, Cursor cur) {
    ViewHolder holder = (ViewHolder) view.getTag();
    if (cur!=null) holder.txtName.setText(cur.getString(cur.getColumnIndex("name")));
 }
}