通过点击列表项中的ImageView删除列表项 [英] Delete a ListItem by clicking the ImageView within the ListItem

查看:181
本文介绍了通过点击列表项中的ImageView删除列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表项,它包括一个ImgaeView。
我想删除每次单击它的图像或图标的列表项。
这是我的ListItemActivity。
我将如何调用适配器的remove方法来删除列表项?
有林与引用的问题。
请让我知道是否有这样做的没有更好的办法。

 公共类TaskListItem扩展的LinearLayout {    私人任务的任务;
    私人TextView的TASKNAME;
    私人TextView的责任;
    私人TextView的优先权;
    私人ImageView的箱;
    保护TaskListAdapter适配器;    公共TaskListItem(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }    @覆盖
    保护无效onFinishInflate(){
        super.onFinishInflate();
        TASKNAME =(的TextView)findViewById(R.id.task_name);
        负责=(的TextView)findViewById(R.id.responsible);
        优先级=(的TextView)findViewById(R.id.priority);
        斌=(ImageView的)findViewById(R.id.remove_task);
    }    公共无效setTask(最终任务的任务){
        this.task =任务;
        taskName.setText(task.getName()+);
        //设置文本的责任
        responsible.setText(RESP:+ task.getReponsible());
        //设置优先级文本
        priority.setText(PRIO:+ task.getPiotiry());
        / *
         * onClickListener图像删除
         * /
        bin.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                **调用适配器删除方法与参数(this)来删除这个项目。**
            }
        });
    }    公共任务getTask(){
        返回任务;
    }}


解决方案

如果你只是想隐藏它,你可以使用的ListView:

  TaskListItem.this.setVisibility(View.GONE);

如果您想从列表中删除它,你需要在适配器的数据源这个项目的立场。

您可以在您的适配器getView()方法创建onClickListener并将其分配给ImageView的。
它可能是这样的:

 公共类MyAdapter延伸BaseAdapter {    清单<任务> MDATA = NULL;  公共MyAdapter(列表<任务>数据源){
    MDATA =数据源;
  }  @覆盖
  公共查看getView(INT位置,查看convertView,父母的ViewGroup){
// ...
    最后TAST任务=的getItem(位置);
    TaskListItem的listItem =新TaskListItem();
    listItem.setTask(任务);
    listItem.bin.setOnClickListener(新OnClickListener(){
      公共无效的onClick(视图v){
        mData.remove(任务); //或mData.remove(位置);
        //这取决于你所使用的适配器上可能需要调用notifyDataSetChanged()
      }
     };
    返回的listItem;
    }
  }

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

}

解决方案

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

这篇关于通过点击列表项中的ImageView删除列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆