android – How do I make a separate clicklistener for each item in the listView?

Question:

I have 3 items in item , but when I click on it, the entire line is selected. I need each item clicked separately.

Tell setOnClickListener how to make the adapter setOnClickListener correct?

public class MountainFragment extends ListFragment {
public final static String TAG = MyTag.TAG_MOUNTAIN;
private MountainShop mountainShop = new MountainShop();

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    List<HashMap<String, String>> listMountainShop = new ArrayList<>();

    ((MainActivity) getActivity()).setLastFragmentTag(this.getClass().toString());

    for (int i = 0; i < mountainShop.getListShops().size(); i++) {
        HashMap<String, String> hm = new HashMap<>();

        hm.put("img", Integer.toString(mountainShop.getIconShops().get(i)));
        hm.put("txt", mountainShop.getListShops().get(i));
        hm.put("imgMy", Integer.toString(R.drawable.ic_control_point_black_24dp));
        listMountainShop.add(hm);
    }

    String[] from = {"img", "txt", "imgMy"};
    int[] to = {R.id.imgForList, R.id.textForList, R.id.imgForMyList};

    SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), listMountainShop, R.layout.list_single, from, to);
    setListAdapter(adapter);

    view.setBackgroundResource(R.drawable.background_mountain);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mountainShop.getLinkShop(position)));
    startActivity(intent);
}
}

Answer:

Create your own class that will inherit from the ArrayAdapter class in it in the getView () method via LayoutInflater inflate your view and from it you get the elements you need via findViewById (), to which you assign listeners. Don't forget about ViewHolder.

class CustomAdapter extends ArrayAdapter {
   private Button btnOne;
   private TextView tvTwo;
   private TextView tvThree;

   public CustomAdapter (Context context, int resource, ArrayList<MusicData> array) {
     super(context, resource, array);
   }

   @Override
   public View getView(int position, View convertView, ViewGroup p) {
      View view = LayoutInflater.from(getContext()).inflate(R.layout.your_layout, null);
      btnOne = view.findViewById(R.id.imgForList);
      tvTwo = view.findViewById(R.id.textForList);
      tvThree = view.findViewById(R.id.R.id.imgForMyList);
      btnOne.setOnClickListener(new OnClickListener() {

           @Override
           public void onClick(View v) {

           }
      });
      tvTwo.setOnClickListener(new OnClickListener() {

           @Override
           public void onClick(View v) {

           }
      });
      tvThree.setOnClickListener(new OnClickListener() {

           @Override
           public void onClick(View v) {

           }
      });
      return view;
   }
  }

adapt a little for yourself and that's it

Scroll to Top