ListView setOnItemClickListener not working in custom list view
Asked Answered
T

5

8

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

Here My Java Code.

public class CreateChallan extends Activity {


ListView lstCreate;

String[] strmainItemCode;
String[] strItem;
String[] strQuantity;
Context context=this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.createchallan);
    lstCreate= (ListView) findViewById(R.id.createlist);
    lstCreate.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);

    strmainItemCode= new String[]{"555551","255555","355555","455555","555555"};

    strItem =new String[]{"A","B","C","D","F"};

    strQuantity =new String[]{"100","200","30","400","500"};

    CreateAdapter adapter= new CreateAdapter(this, strmainItemCode, strItem, s trQuantity);

    lstCreate.setAdapter(adapter);

    lstCreate.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position1, long id) {
            // TODO Auto-generated method stub

            Toast.makeText(context, "Position",   Toast.LENGTH_LONG).show();

        }
    });
}







// Create List Adapter

class CreateAdapter extends ArrayAdapter<String>
 {
    TextView txtItecode, txtItem;
    EditText editQuantity;
    String[] strItecode;
    String[] strItem;
    String[] strQuantity;
    Context context;

    CreateAdapter(Context context, String[] strItemcode, String[] strItem,  String[] strQauntity)
    {
           super(context,R.layout.create_list_item,R.id.txtItemcode,strItemcode);
        this.context= context;
        this.strItecode= strItemcode;
        this.strItem= strItem;
        this.strQuantity= strQauntity;
    }
     public View getView(int position, View convertView, ViewGroup parent) {
         LayoutInflater mInflater = (LayoutInflater)  context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
         View row;
         row=mInflater.inflate(R.layout.create_list_item, parent,false);

         txtItecode= (TextView) row.findViewById(R.id.txtItemcode);
         txtItem =(TextView) row.findViewById(R.id.txtItem);
         editQuantity = (EditText)  row.findViewById(R.id.editcreateQuantity);

         txtItecode.setText(strItecode[position]);
         txtItem.setText(strItem[position]);
        editQuantity.setText(strQuantity[position]);

         txtItecode.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "click", Toast.LENGTH_LONG).show();
            }
        });

         return row;


     }
 }


}

Here MY list xml code

 <ListView
    android:id="@+id/createlist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:clickable="true"
    android:cacheColorHint="#00000000"
    android:divider="#adb8c2"
    android:dividerHeight="1dp"
    android:scrollingCache="false"
    android:smoothScrollbar="true" 
    android:focusable="false"
android:focusableInTouchMode="false"

   >

</ListView>

Please Suggest me How i can fix this problem.

Thanks In Advance

Troy answered 28/3, 2014 at 6:31 Comment(2)
You want to write two click listener in the listviewUnruffled
I posted the custom adapter which is having the both onitem click and onclick function in itself so you won't need to write it externally Just try and You just do your on item click into the convertview click functionUnruffled
S
12

Set these properties

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

for your all UI elements in your create_list_item xml file.

Also remove that properties from ListView.

So your ListView will be

 <ListView
   android:id="@+id/createlist"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:clickable="true"
   android:cacheColorHint="#00000000"
   android:divider="#adb8c2"
   android:dividerHeight="1dp"
   android:scrollingCache="false"
   android:smoothScrollbar="true">
 </ListView>
Slider answered 28/3, 2014 at 6:34 Comment(2)
this works, but for that the property android: clickable must be false for the "view" in question. Attention to the parents of the "View" to be not using "android: clickable" on. this will block the pattern selector of your list or derivative. You can match the pattern selector with a custom selector using the property android: foreground like this: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:fitsSystemWindows="true"> <FrameLayout "android: foreground="CUSTOM SELECTOR""> <RelativeLayout><RelativeLayout/> <.../> <.../>Altheta
I tried this but its not working. I can click in empty space but not on text views or any other widgetsJannjanna
S
7

In my opinion, because there is Button or ImageButton in your custom list item layout.

So there is two solution,

  1. Replace the button to other elements, and remove onClick listener in the Adapter code. This is the easy way to solve this.

  2. Disable the button focus, like this,

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

Please refer this ListView setOnItemClickListener not working by adding button

Have fun. @.@

Secretory answered 24/9, 2014 at 9:52 Comment(1)
Didn'r work for me and I'm not sure why. I'm using an ImageButton along side some TextViews only. Already tried all the things provided here and in the link referenced. Nothing works atm.Outwork
U
0

This custom adapter is for you here you can achieve tha both onclick and on item click listener also....Here i used the custom listener which is used to pass the object of the selected item..That's all If you have any query comment...All the best

public class CustomAdapter extends ArrayAdapter<Sample> {

public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;

public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
    super(context, resource);
    this.mlist = mlist;
    this.context = context;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getPosition(Sample item) {
    return super.getPosition(item);
}

@Override
public Sample getItem(int position) {
    return mlist.get(position);
}

@Override
public int getCount() {
    return mlist.size();
}

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = inflater.inflate(R.layout.listitem, null);
    LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.linearlayoutSample);;
    TextView text1 = (TextView) convertView.findViewById(R.id.item1);
    TextView text2 = (TextView) convertView.findViewById(R.id.item2);
    layout.setBackgroundColor(Color.GREEN);
    text1.setText(mlist.get(position).getListitem1());
    text2.setText(mlist.get(position).getListitem2());
    text2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // you just put your Logic here And use this custom adapter to
            // load your Data By using this particular custom adapter to
            // your listview

        }
    });
    convertView.setOnClickListener(new ListenerT(Model m) {

        @Override
        public void onClick(View v) {
            Model m = study;

        }
    });
    return convertView;
}
private class ListenerT implements OnClickListener {

    Model study;

    public ListenerT(Model nm) {
        study = nm;
    }

    @Override
    public void onClick(View v) {

    }
}

  }
Unruffled answered 28/3, 2014 at 6:40 Comment(0)
M
0

Its always better to check with a Log.i() inside the setOnItemClickListener, because your event/code may not have happened, maybe because of an Exception.

So If your log prints, but your code is not working, there is a high probability that there is an exception and it probably did not crash the app due to error handling or try catch. Also recommend putting Log.e inside catch block if present.

Matter answered 25/3, 2021 at 6:41 Comment(0)
T
0

The issue is with the ImageButton. I changed it to ImageView and the issue was solved. For check this error set ImageButton Visibility.GONE then it will work. I solved this issue by changing ImageButton to ImageView

Teferi answered 17/8, 2023 at 12:2 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Vermiculation

© 2022 - 2024 — McMap. All rights reserved.