Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView setOnItemClickListener not working in custom list view

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

like image 643
Kuldeep Avatar asked Mar 28 '14 06:03

Kuldeep


2 Answers

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>
like image 81
Piyush Avatar answered Nov 10 '22 06:11

Piyush


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. @.@

like image 28
Luna Kong Avatar answered Nov 10 '22 08:11

Luna Kong