Deleting an Item from Listview by Clicking on ImageView widget

痞子三分冷 提交于 2019-12-11 19:27:17

问题


I'm trying to delete an item from ListView by Clicking on a picture (imageView). I must do it this way, I've managed to do it by click on the item itself but for this project I'm not allowed to do that.

My Adapter extends BaseAdapter and my TaskListItem extends Relative Layout I'm using a custom layout xml.

protected void onFinishInflate() {
    super.onFinishInflate();
    textV = (TextView)findViewById(R.id.textViewTask);
    textP = ((TextView)findViewById(R.id.textPriority));
    textR = ((TextView)findViewById(R.id.textResponsible));

    imageD = ((ImageView)findViewById(R.id.imageDeleteTask));
    imageD.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deleteTask();
            }
        });
    }

and the deleteTask() method:

protected void deleteTask() {
//MainActivity.adapter.clear(position)
Toast.makeText(getContext(), "Task Deleted", Toast.LENGTH_SHORT).show();
MainActivity.adapter.notifyDataSetChanged();    
    }

my problem is the variable position. I cannot find a way to get the position of the item selected (by clicking on the imageView widget).

Finally, here's my adapter code. Any help will be much appreciated:

public class TaskListAdapter extends BaseAdapter {

private ArrayList<Task> tasks;
private Context context;


public TaskListAdapter(ArrayList<Task> tasks, Context context) {
    super();
    this.tasks = tasks;
    this.context = context;
}

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

@Override
public Task getItem(int position) {
    return (null==tasks)? null: tasks.get(position);
}

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


@Override
public View getView(int position, View convertView, ViewGroup parent) {
         TaskListItem tli;
        Log.v("test","dasfsd");
    if (null == convertView) {
        tli = (TaskListItem)View.inflate(context, R.layout.task_list_item, null);

    }
        else {
        tli = (TaskListItem)convertView;
        }
    tli.setTask(tasks.get(position));
    return tli;
    }


public void forceReload() {
    notifyDataSetChanged();
}

public void clear(int position) {

    tasks.remove(position);

}

Update Code for OnClickListener on ImageView

    public void setTask(final Task task) {
    this.task = task;
    //textV.set(task.isComplete());
    textV.setText(task.getName());
    textP.setText(task.getPriority());
    textR.setText(task.getResponsible());
    imageD.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {   
            // probably get position
            // deleteTask();
            MainActivity.adapter.notifyDataSetChanged();

            }
        });
}

protected void deleteTask() {
    int position=0;
    MainActivity.adapter.clear(position);
    Toast.makeText(getContext(), "Task Deleted", Toast.LENGTH_SHORT).show();
    MainActivity.adapter.notifyDataSetChanged();

    //testing  - problem cannot delete item. 
}

回答1:


To get position , Add position as Tag of view

@Override
public View getView(int position, View convertView, ViewGroup parent) {
         TaskListItem tli;
        Log.v("test","dasfsd");
    if (null == convertView) {
        tli = (TaskListItem)View.inflate(context, R.layout.task_list_item, null);

    }else {
        tli = (TaskListItem)convertView;
    }
    tli.setTask(tasks.get(position)  , position);
    return tli;
}



public void setTask(final Task task , int position) {
    this.task = task;
    //textV.set(task.isComplete());
    textV.setText(task.getName());
    textP.setText(task.getPriority());
    textR.setText(task.getResponsible());
    imageD.setTag(new Integer(position));
    imageD.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {   
             int position = (Integer) v.getTag();
             deleteTask(position);
             MainActivity.adapter.notifyDataSetChanged();

            }
        });
}

protected void deleteTask(int position) {
    MainActivity.adapter.clear(position);
    Toast.makeText(getContext(), "Task Deleted", Toast.LENGTH_SHORT).show();
    MainActivity.adapter.notifyDataSetChanged();

    //testing  - problem cannot delete item. 
}


来源:https://stackoverflow.com/questions/12009983/deleting-an-item-from-listview-by-clicking-on-imageview-widget

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