1

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

}
afollestad
  • 2,929
  • 5
  • 30
  • 44
i_ch3ry
  • 29
  • 1
  • 8

2 Answers2

2

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;
    }
  }
Bartek Filipowicz
  • 1,235
  • 7
  • 9
0

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).

afollestad
  • 2,929
  • 5
  • 30
  • 44