我正在尝试通过单击图片(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.
}
答案 0 :(得分: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.
}