2

i manage to do when i click the item in my listview , beside the column will show a tick over there, but now i have a problem in when i click on my item the tick will show but there is no function for my onitemclicklistener event. how to let this two function work in same time. sorry for my english, hope can understand

this is my setOnItemClickListener event

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

     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]));
                      }
 ------condimentitem.setOnClickListener(new View.OnClickListener()-------
        {
            @Override
            public void onClick(View v)
            {
                int visibility = btntick.getVisibility();
                if(visibility == View.VISIBLE)
                {
                    btntick.setVisibility(View.GONE);
                }
                else
                {
                    btntick.setVisibility(View.VISIBLE);
                }
            }
        });

               }
 });

this is my baseadapter to contor the tick

 public class condimentlist extends BaseAdapter {

    LayoutInflater mInflater;

    private ArrayList<Integer> positions = new ArrayList<Integer>();

    public ArrayList<Integer> getPositions() {
        return positions;
    }

    public condimentlist() {
        mInflater = LayoutInflater.from(getActivity());
    }

    @Override
    public int getCount() {
        return CondimentList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.condimentlistview_details, null);
        }
        condcb = (TextView) convertView.findViewById(R.id.condcb);
        final TextView tremark = (TextView) convertView.findViewById(R.id.Tremark);
        btntick = (ImageView) convertView.findViewById(R.id.iv_tick);

    Condiment myObj = CondimentList.get(position);
    condcb.setText("" + myObj.getCondimentName());


 -----------convertView.performClick ();------------------
        return convertView;
    }

condimentlistview_details.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/condcb"
        android:text="Press"
        android:layout_width="150dp"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textSize="18sp" />

    <ImageButton
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:background="@drawable/tick"
        android:id="@+id/iv_tick"
        android:visibility="gone"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="150dp"
        android:layout_marginStart="150dp" />

</RelativeLayout>
Wei Ming Tang
  • 105
  • 3
  • 14

5 Answers5

1

I think problem is ImageButton in your listview Row xml file.

Put android:focusable="false" , and see it works!!!

<ImageButton
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:background="@drawable/tick"
    android:id="@+id/iv_tick"
    android:visibility="gone"
    android:focusable="false"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="150dp"
    android:layout_marginStart="150dp" />
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
0

Add convertView.performClick() inside condcb.OnClick method in adapter.

John Joe
  • 12,412
  • 16
  • 70
  • 135
Sandeep Kharat
  • 500
  • 1
  • 4
  • 12
0

Below is one of the question i answered before, you may need to create a list with one of the element of boolean as a flag. When you select, you turn that flag into true. Hope it helps.

enter link description here

Community
  • 1
  • 1
kggoh
  • 742
  • 1
  • 10
  • 24
0

Write this code inside BASE ADAPTER's getView()

condimentitem.setOnClickListener(new View.OnClickListener()-------
        {
            @Override
            public void onClick(View v)
            {
                int visibility = btntick.getVisibility();
                if(visibility == View.VISIBLE)
                {
                    btntick.setVisibility(View.GONE);
                }
                else
                {
                    btntick.setVisibility(View.VISIBLE);
                }
            }
        });
John Joe
  • 12,412
  • 16
  • 70
  • 135
MUKUND U
  • 44
  • 6
0

You could define a static ArrayList in your activity class, then retrieve everything from that array list in the base adapter with a Hashmap. Then, you could use SetOnItemClickListener in your activity class to get the correct value of each row by referencing the activity's array list's position.

Works for me flawlessly. Let me know if you need example code.

grantespo
  • 2,233
  • 2
  • 24
  • 62