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

Tell setOnClickListener how to setOnClickListener to the adapter correctly?

 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); } } 

    2 answers 2

    Create your own class, which will inherit from the ArrayAdapter class in it in the getView () method via LayoutInflater inflate your view and get the items you need through findViewById (), which you assign to the listeners. About ViewHolder do not forget.

     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 everything

    • Thank you, create one. - grosha
    • really don't do that. It is better to define one global listener, and divide the clickable view by ID, and then transfer the necessary information to each tag in each view in order to somehow process it when clicked. In this version, imagine how many extra objects you have naked - andreich
    • a matter of taste no more - s_klepcha

    Made your adapter. In the constructor, I get my list, and then I cut it off into my elements:

     public class CustomAdapter extends ArrayAdapter { private final List object; private ImageView imgIcon; private TextView nameShop; private ImageView imgPlus; public CustomAdapter(Context context, int resource, List objects) { super(context, resource, objects); this.object = objects; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view = LayoutInflater.from(getContext()).inflate(R.layout.list_single, null); HashMap<String, String> list = (HashMap<String, String>) object; imgIcon = (ImageView) view.findViewById(R.id.imgForList); imgIcon.setImageResource(Integer.parseInt(list.get("img"))); nameShop = (TextView) view.findViewById(R.id.textForList); nameShop.setText(list.get("txt")); imgPlus = (ImageView) view.findViewById(R.id.imgForMyList); imgPlus.setImageResource(Integer.parseInt(list.get("imgMy"))); imgIcon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(MyTag.TEST, "1"); } }); nameShop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(MyTag.TEST, "2"); } }); imgPlus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(MyTag.TEST, "3"); } }); return view; } } 
    • “I am cutting it into my elements” - I didn’t quite understand what it meant. - 0xdb