Search code examples
androidlistviewonitemclicklistener

Android how to detect second click in setOnItemClickListener


I manage to do there is a ListView which contains the TextView. if click on the TextView it will show the ImageButton, else the second click the ImageButton in the ListView will hide. My problem is I can only show the ImageButton for the first click but I can't hide ImageButton when I clicked on TextView again. Any suggestion to solve this problem.

this is my listview setontimeclicklistener

condimentlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                      TextView condimentitem =(TextView)view.findViewById(R.id.condcb);
                      String citem= condimentitem.getText().toString();
                      ImageView btntick = (ImageView) view.findViewById(R.id.iv_tick);

                           int visibility = btntick.getVisibility();
                           if(visibility == View.VISIBLE)
                           {
                                      btntick.setVisibility(View.GONE);

                            }
                                      else if(visibility == View.GONE)
                            {
                                      btntick.setVisibility(View.VISIBLE);
                            }

                            ArrayList<String> data = new ArrayList<String>();
                            data.add(citem);
                            String array[] = data.toArray(new String[0]);
                            for (int j = 0; j < array.length; j++) {
                                       remark.append(String.valueOf(array[j]));
                            }

              }
});

Solution

  • This will works if the textView clicked. But I'm not sure whether this can work since the textView is inside the listView.

    condimentitem.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    if(btntick.getVisibility() == View.VISIBLE){
                        btntick.setVisibility(View.GONE);
                    }else{
                        btntick.setVisibility(View.VISIBLE);
                    }
    
                }
            });
    

    I would suggest you write the ImageView and TextView inside the adapter.


    Correct Approach

    Write this in Adapter class

    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.your_activity, parent, false);
         .....
        condimentim.setOnClickListener(new OnClickListener() {
           @Override
            public void onClick(View v) {
                if(btntick.getVisibility() == View.VISIBLE){
                    btntick.setVisibility(View.GONE);
                }else{
                    btntick.setVisibility(View.VISIBLE);
                }
    
            }
        });
    }