简体   繁体   中英

How to set a click listener on an image view in a list item of a listview

I am implementing a custom adapter to use in a listview.I am populating the listview with some text messages and one of my classes object. There is an imageview in each list item and on click of that imageview i am showing a view with the objects information and on click of the listview item i am showing a different view. I am able to achieve this but the problem here is that when i click on the imageview the view that opens up contains the information of some other list items object and not the object contained in the list item whose image view was clicked.I tried a lot to get a solution for this but didnt get one. So i am putting up this question.Please help me.

  1. This is the implementation of my custom adapters getView() method-

      List<Map<String, Object>> data = getTasksData(tasks); SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.list_item_detail_layout, new String[] { "itemstatus","itemname", "itemdesc"}, new int[] {R.id.taskPercentage, R.id.taskname, R.id.taskdesc}){ @Override public View getView(int position, View convertView, ViewGroup parent){ if (convertView == null) { LayoutInflater inflater = getLayoutInflater(); convertView = inflater.inflate(R.layout.list_item_detail_layout, null, false) } @SuppressWarnings("unchecked") //map object used to populate each list item Map<String, Object> listItem = (Map<String, Object>) listView.getItemAtPosition(position); //object whose information is to be displayed in the view Task task = (Task)listItem.get("item"); if(task != null){ activeTask = task; } //text views to be displayed in each list item TextView taskpercentage = (TextView)convertView.findViewById(R.id.taskPercentage); taskpercentage.setText((String)listItem.get("itemstatus")); TextView taskname = (TextView)convertView.findViewById(R.id.taskname); taskname.setText((String)listItem.get("itemname")); TextView taskDescription = (TextView)convertView.findViewById(R.id.taskdesc); taskDescription.setText((String)listItem.get("itemdesc")); //imageview whose click listener has been set to navigate to a view ImageView taskEdit = (ImageView)convertView.findViewById(R.id.task_edit); taskEdit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //method called when image icon on the list view is clicked newTask(v, false); } }); return convertView; } }; 

    //method which is called when image icon in the list view is clicked

  2. newTask(v,false) code -

     private void newTask(final View view, final boolean isNew) { setContentView(R.layout.task_view); final EditText taskName = (EditText) this .findViewById(R.id.task_name_edtxt); if (isNew == false) { taskName.setText(activeTask.getName()); } } 

3.ListView Implementation -

               listView = ((ListView)this.findViewById(R.id.listTaskView));
           listView.setAdapter(simpleAdapter);
               listView.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
              Task task = null;
                  @SuppressWarnings("unchecked")
                  Map<String, Object> listItem = (Map<String, Object>) listView
                       .getItemAtPosition(position);
                     task = (Task) listItem.get("item");

                  if (task != null) {
                  activeTask = task;

                 //method called when listview item is clicked
                 showActivityListView(view);
             }
          }
        }
    });
  1. layout file used as listview item -

      <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/taskPercentage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/blue_shade" android:textSize="20sp" android:textStyle="bold" > </TextView> <LinearLayout android:layout_width="0px" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:layout_weight="1" android:orientation="vertical" android:paddingLeft="0px" android:paddingRight="5dp" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/taskname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:textColor="#FFF38585" android:textSize="15sp" > </TextView> <ImageView android:id="@+id/edit_task" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:src="@drawable/edit_form"/> </RelativeLayout> <TextView android:id="@+id/taskdesc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:textColor="#FF2CE4A4" android:textSize="13sp" > </TextView> </LinearLayout> </LinearLayout> 

this is the implementation reference. Please, if any one can tell me what the problem is or how to get this functionality working correctly, do help me.Thanks in advance.

try:

final ImageView taskEdit = (ImageView)convertView.findViewById(R.id.task_edit);
taskEdit.setOnClickListener(new OnClickListener() { 
  @Override
  public void onClick(View v) {
     newTask(taskEdit, false);
    }
  });
     return convertView;
}

From your MainActivity or your Fragment , you should initialize the ListView and set its adapter. You should also set onItemClickListener in it ( MainActivity ). Then get the object of in the given position parameter from the list and execute your action based on it.

Example:

ListView listViewItems = new ListView(this);
    listViewItems.setAdapter(adapter);
    listViewItems.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        Toast.makeText(getApplicationContext(),
        "Click ListItem Number " + position, Toast.LENGTH_LONG)
        .show();
       }
      }); 

Your can create your own Object class and pass it to the list, then check on the item position and perform your action.

I hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM