SetOnClickListener vs setOnItemClickListener

62 Views Asked by At

The View for a ListView two buttons (for example to perform: call, send SMS). I want to set up a listeners for clicks on the buttons. It is my understanding that if I set up a OnItemSelectedListener, I won't know which of the two buttons was clicked.

When I set up listeners for a button, when I click on it, I get the listener of the corresponding button that is in the last item in the list (not the one I clicked on) (which makes sense programmatically) but this is not what I want. I can tell because it always calls the number that belongs to the last item in the list. Any tips on how set up OnClick for multiple views in each item ?

@NonNull
@Override
public View getView(final int position, @Nullable final View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inf = ((Activity)context).getLayoutInflater();

    View view = inf.inflate(R.layout.place , parent , false);


    name = view.findViewById(R.id.name);
    type = view.findViewById(R.id.type);
    minAge = view.findViewById(R.id.minAge);
    open = view.findViewById(R.id.open);
    close = view.findViewById(R.id.close);
    distance = view.findViewById(R.id.distance);
    phone = view.findViewById(R.id.phone);
    picture = view.findViewById(R.id.picture);
    btnCall = view.findViewById(R.id.btnCall);

    final Place place = placeList.get(position);
    // btnCall.setOnClickListener(this);
    // I tried both setups with the same result.
    // the button is always the one belonging to the last item in the list.
    btnCall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
             String s = btnPhone.getText().toString();

        }
    });
    // another button would have its own OnClick
    name.setText(place.getName());
    type.setText(place.getType());
    minAge.setText(Integer.toString(place.getMinAge()));
    open.setText(Integer.toString(place.getOpen()));
    close.setText(Integer.toString(place.getClose()));
    distance.setText(Integer.toString(place.getDistance()));
    phone.setText(place.getPhone());
    int id = context.getResources().getIdentifier("place"+position,"drawable" ,
            ((Activity)context).getPackageName());
    //row_pic.setImageResource(id);
    return view;
}

@Override
public void onClick(View v) {
   Intent intent = new Intent(Intent.ACTION_DIAL);
    intent.setData(Uri.parse("tel:" + phone.getText()));
    if (intent.resolveActivity(context.getPackageManager()) != null) {
        context.startActivity(intent);
    }


    if (intent.resolveActivity(context.getPackageManager())  != null)
    {
        context.startActivity(intent);
    }
}
1

There are 1 best solutions below

0
Sam Mazza On

I found the solution just after posting the question. USE TAGS to pass data.

''' phone.setText(place.getPhone()); btnCall.setTag(phone.getText().toString());

    btnCall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String s =  (String) ((Button) view).getTag().toString();
            // now I can call the correct number
        }
    });

'''