Kishore Kishore - 1 year ago 247
Android Question

ListView setOnItemClickListener not working by adding button

I have a list view with text and button in each row, list view setOnItemClickListener() is not working. is it possible to handle item click and button click events differently(item click should call ActivityA and button click should call ActivityB). Does anyone have a solution

private ArrayList<String> userIDArr = null;
private ArrayList<String> userNameArr = null;
private DatabaseHelper dbHelper = null;
private ListView userListView=null;

public void onCreate(Bundle savedInstanceState)
dbHelper = new DatabaseHelper(this.getApplicationContext());
Map<String,ArrayList<String>> displayMap = dbHelper.getUserListToDisplay();
userIDArr = displayMap.get("UserID");
userNameArr = displayMap.get("FirstName1");

userListView = (ListView) findViewById(;
userListView.setAdapter(new UserListAdapter(this,userIDArr));

userListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

"Item in position " + position + " clicked", Toast.LENGTH_LONG).show();

public class UserListAdapter extends ArrayAdapter<String>
Activity context;
public UserListAdapter(Activity context, ArrayList<String> names) {
super(context, R.layout.list_item, names);
this.context = context;
private class ViewHolder {
public TextView UserNameAndID;
public TextView Description;
public Button UploadBtn;

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.UserNameAndID = (TextView) rowView.findViewById(;
holder.Description = (TextView) rowView.findViewById(;
holder.UploadBtn = (Button) rowView.findViewById(;
holder.UploadBtn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
Toast.makeText(usersListActivity.this," Button clicked",Toast.LENGTH_SHORT).show();
} else {
holder = (ViewHolder) rowView.getTag();
String s = userNameArr.get(position)+","+userIDArr.get(position);
holder.Description.setText("U r in middle");
return rowView;

Answer Source

Try setting your buttons (or any other views you want to handle click inside a list item) like this:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download