Delete a ListItem by clicking the ImageView within the ListItem

感情迁移 提交于 2019-12-08 08:19:35

问题


I have a ListItem which includes an ImgaeView. I want to delete the ListItem whenever its image or icon is clicked. Here is my ListItemActivity. How would I call the remove method of the adapter to remove the ListItem? Im having the problem with referencing. Please let me know if there is any better way of doing it.

public class TaskListItem extends LinearLayout {

    private Task task;
    private TextView taskName;
    private TextView responsible;
    private TextView priority;
    private ImageView bin;
    protected TaskListAdapter adapter;

    public TaskListItem(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        taskName = (TextView)findViewById(R.id.task_name);
        responsible = (TextView)findViewById(R.id.responsible);
        priority = (TextView)findViewById(R.id.priority);
        bin = (ImageView)findViewById(R.id.remove_task);        
    }

    public void setTask( final Task task) {
        this.task = task;
        taskName.setText(task.getName() + " ");
        //Set responsibility text
        responsible.setText("Resp: " + task.getReponsible());
        //Set priority text
        priority.setText(" Prio: " + task.getPiotiry());
        /*
         * onClickListener for image to delete
         */
        bin.setOnClickListener(new OnClickListener() {          
            public void onClick(View v) {
                **call the adapters remove method to delete this item with parameter (this).**  
            }
        });
    }

    public Task getTask() {
        return task;
    }

}

回答1:


If you just want to hide it on the ListView you could use:

TaskListItem.this.setVisibility(View.GONE);

If you want to remove it from the list, you will need this items position in the adapter's data source.

You could create the onClickListener in your adapter's getView() method and assign it to the ImageView. It could look like this:

public class MyAdapter extends BaseAdapter{

    List<Task> mData = null;

  public MyAdapter(List<Task> dataSource){
    mData = dataSource;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
//    ... 
    final Tast task = getItem(position);
    TaskListItem listItem = new TaskListItem();
    listItem.setTask(task);
    listItem.bin.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
        mData.remove(task); // or mData.remove(position);
        // might need to call notifyDataSetChanged() depending on the adapter you're using
      }
     };
    return listItem;
    }
  }



回答2:


You'd wanna catch the clicking of a list item from the activity that contains the list, e.g. inside the callback of list.setOnListItemClick() or in onListItemClick of a ListActivtiy.

When you get the click event, then you call adapter.remove(index).



来源:https://stackoverflow.com/questions/12030363/delete-a-listitem-by-clicking-the-imageview-within-the-listitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!