0

I have made a ListView which has some rows. In the ItemClickListener I want to access which item of the row is clicked

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

            String v=lstcontact.getItemAtPosition(position).toString();
            if(view.getId()==R.id.btncall) {
                Toast.makeText(MainActivity.this, "Make A Call at " + position, Toast.LENGTH_SHORT).show();
            }else if(view.getId()==R.id.btnmess) {
                Toast.makeText(MainActivity.this, "Make A Message at " + position, Toast.LENGTH_SHORT).show();
            } 
        }


    });

I want to display a toast when the user clicks on the message or call button

Vin Norman
  • 2,749
  • 1
  • 22
  • 33

2 Answers2

0

you are not calling onclick for items in that row. To do this you need to implement listener for your adapter.

public interface CustomListener{
    void onSomeItemClick(View view);
}

in your adapter constructor you need to initialize this listener;

public YourAdapter(Context context, CustomListener listener, List<YourClass> list){
    this.context = context;
    this.listener = listener;
    this.list = list;
 }

in activity where you create this adapter you need to implement listener

adapter = new YourAdapter(this,this,list);

after that you need to override onSomeItemClick method

@Override 
public void someItemClick(View view){
    if(view.getId()==R.id.btncall) {
        Toast.makeText(MainActivity.this, "MAke A Call at "+position, Toast.LENGTH_SHORT).show();
    }else if(view.getId()==R.id.btnmess) {
        Toast.makeText(MainActivity.this, "MAke A Message at "+position, Toast.LENGTH_SHORT).show();
    } 
}

in your adapter's getView() you need to implement onClickListener in which view you want to be clicked.

view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(listener!=null{
            listener.onSomeItemClick(v);
        }       
    }
});
Ergin Ersoy
  • 890
  • 8
  • 28
0

The view parameter in onItemClick is the whole ListView row (with all buttons and everything inside), so view.getId() == R.id.btncall isn't going to make sense.

You are best setting the Toasts in the onClickListener of the buttons in your custom adapter. It would have been nice to see your layout / adapter code, but a basic custom adapter (using the ViewHolder pattern, for efficiency) could look something like this:

public class MyListAdapter extends ArrayAdapter<YourItemType> {

static class ViewHolder {
    private Button callButton;
    private Button messageButton;
    // declare any other views you want to control here
}


public MyListAdapter(Context context, List<YourItemType> items) {
    super(context,0,items);
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.your_listview_layout, null);
        holder = new ViewHolder();
        holder.callButton = (Button) convertView.findViewById(R.id.btncall);
        holder.messageButton = (Button) convertView.findViewById(R.id.btnmess);
        // find views for any other views in your ViewHolder / Listview layout
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }
    YourItemType item = getItem(position);
    holder.callButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // simple say what position is
            Toast.makeText(getContext(), "Make A Call at "+String.valueOf(position), Toast.LENGTH_SHORT).show();
            // do something with the item itself
            Toast.makeText(getContext(), "This toast is for info on the item, which is  "+ item.toString(), Toast.LENGTH_SHORT).show();

        }
    });
    holder.messageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // simple say what position is
            Toast.makeText(getContext(), "Make A Call at "+String.valueOf(position), Toast.LENGTH_SHORT).show();
            // do something with the item itself
            Toast.makeText(getContext(), "This toast is for info on the item, which is  "+ item.toString(), Toast.LENGTH_SHORT).show();
        }
    });
    return convertView;
}

}

Vin Norman
  • 2,749
  • 1
  • 22
  • 33