0

我正在尝试通过单击图片 (imageView) 从 ListView 中删除一个项目。我必须这样做,我已经设法通过单击项目本身来做到这一点,但对于这个项目,我不允许这样做。

我的适配器扩展了 BaseAdapter,我的 TaskListItem 扩展了相对布局我使用的是自定义布局 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();
            }
        });
    }

deleteTask()方法:

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

我的问题是可变位置。我找不到获取所选项目位置的方法(通过单击 imageView 小部件)。

最后,这是我的适配器代码。任何帮助都感激不尽:

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

}

更新 ImageView 上 OnClickListener 的代码

    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. 
}
4

1 回答 1

1

要获取位置,请将位置添加为视图标签

@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. 
}
于 2012-08-17T18:13:25.753 回答