I have Button
in each row of ListView
and onClickListener
for Button
. I want to add onItemSelectListener
to my ListView
too. Is it possible? If yes how can I do that?
Any help will be appreciated.
Asked
Active
Viewed 1,287 times
0

Spring Breaker
- 8,233
- 3
- 36
- 60

max
- 5,963
- 12
- 49
- 80
-
@max: it's suppose to be possible.look on this video: https://www.youtube.com/watch?v=wDBM6wVEO70 . watch from minute 24 – Tal Kanel May 13 '14 at 05:02
-
Hi after some research on internet i found [this good example][1] [1]: http://stackoverflow.com/questions/15249632/android-custom-listview-with-imagebutton-is-not-getting-focus – max May 13 '14 at 10:00
2 Answers
3
Yes it is possible ...
While creating custom view adapter for listview, u have to add onclicklistener on button and u can also need to add onItemSelectListener on Listview. It would work.
use listview code as
listView = (ListView) findViewById(R.id.listView2);
listView .setAdapter(new CustomListAdapter (this,userIDArr));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast.makeText(Activity.this,
"Item in position " + position + " clicked", Toast.LENGTH_LONG).show();
}
});
and create adapter like
public class CustomListAdapter extends ArrayAdapter<String>
{
Activity context;
public CustomListAdapter (Activity context, ArrayList<String> names) {
super(context, R.layout.list_item, names);
this.context = context;
}
private class ViewHolder {
public TextView Description;
public Button UploadBtn;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_item, null, true);
holder = new ViewHolder();
holder.Description = (TextView) rowView.findViewById(R.id.User_status);
holder.UploadBtn = (Button) rowView.findViewById(R.id.uploadbutton);
holder.UploadBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(Activity.this," Button clicked",Toast.LENGTH_SHORT).show();
}
});
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.Description.setText("U r in middle");
return rowView;
}
}
Now to handle click inside a list item use the below code
android:focusable="false"
android:focusableInTouchMode="false"
set these lines while creating Button tag
It would work ... Please let me know your feedback..

Neha Shukla
- 3,572
- 5
- 38
- 69
-
@Simple Plan .. Plz look at my updated answer.. and let me kw if it worked for u or not – Neha Shukla May 13 '14 at 05:06
-
-
thanks for your attention.where i should use this code android:focusable="false" android:focusableInTouchMode="false" in my singlelistitem or in listview? – max May 13 '14 at 05:12
-
where u have created ur layout for custom view. use these lines inside button tag on which u wanna act on click – Neha Shukla May 13 '14 at 05:15
-
-
@NehaShukla sorry for late. i actually have customadapter for listview and image button inested of button. i try my custmadapter which extend baseadapter and use this android:focusable="false" android:focusableInTouchMode="false" codes for my goal and thats not work. its different if i use baseadapter or Stringadapter in this case? – max May 13 '14 at 08:01