问题描述
我想通过点击图片(ImageView的),以删除ListView的项目。我一定要做到这样,我已经成功通过点击该项目本身做,但这个项目我不能这样做。
我的适配器扩展了BaseAdapter和我TaskListItem延伸相对布局
我使用的是自定义布局XML。
保护无效onFinishInflate(){
super.onFinishInflate();
textV =(的TextView)findViewById(R.id.textViewTask);
textP =((的TextView)findViewById(R.id.textPriority));
textR =((的TextView)findViewById(R.id.textResponsible)); 成像=((ImageView的)findViewById(R.id.imageDeleteTask));
imageD.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
DeleteTask活动();
}
});
}
和 DeleteTask活动()
方法:
保护无效DeleteTask活动(){
//MainActivity.adapter.clear(position)
Toast.makeText(的getContext(),任务已删除,Toast.LENGTH_SHORT).show();
MainActivity.adapter.notifyDataSetChanged();
}
我的问题是变量的位置。我不能找到一种方式来获得选择的项目的位置(通过点击ImageView的部件)。
最后,这里是我的适配器code。任何帮助将大大AP preciated:
公共类TaskListAdapter延伸BaseAdapter {私人的ArrayList<任务>任务;
私人上下文的背景下;
公共TaskListAdapter(ArrayList的<任务>的任务,上下文的背景下){
超();
this.tasks =任务;
this.context =背景;
}@覆盖
公众诠释的getCount(){
返回tasks.size();
}@覆盖
公共任务的getItem(INT位置){
返回(空==任务)?空:tasks.get(位置);
}@覆盖
众长getItemId(INT位置){
返回的位置;
}
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
TaskListItem TLI;
Log.v(测试,dasfsd);
如果(空== convertView){
TLI =(TaskListItem)View.inflate(背景下,R.layout.task_list_item,NULL); }
其他{
TLI =(TaskListItem)convertView;
}
tli.setTask(tasks.get(位置));
返回TLI;
}
公共无效forceReload(){
notifyDataSetChanged();
}公共无效清除(INT位置){ tasks.remove(位置);}
更新code为OnClickListener上的ImageView
公共无效setTask(最终任务的任务){
this.task =任务;
//textV.set(task.isComplete());
textV.setText(task.getName());
textP.setText(task.getPriority());
textR.setText(task.getResponsible());
imageD.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
//可能会得到位置
// DeleteTask活动();
MainActivity.adapter.notifyDataSetChanged(); }
});
}保护无效DeleteTask活动(){
INT位置= 0;
MainActivity.adapter.clear(位置);
Toast.makeText(的getContext(),任务已删除,Toast.LENGTH_SHORT).show();
MainActivity.adapter.notifyDataSetChanged(); //测试 - 问题无法删除项目。
}
要获得位置,添加为看标签位置
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
TaskListItem TLI;
Log.v(测试,dasfsd);
如果(空== convertView){
TLI =(TaskListItem)View.inflate(背景下,R.layout.task_list_item,NULL); }其他{
TLI =(TaskListItem)convertView;
}
tli.setTask(tasks.get(位置),位置);
返回TLI;
}公共无效setTask(最终任务任务,INT位置){
this.task =任务;
//textV.set(task.isComplete());
textV.setText(task.getName());
textP.setText(task.getPriority());
textR.setText(task.getResponsible());
imageD.setTag(新的整数(位置));
imageD.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
INT位置=(整数)v.getTag();
DeleteTask活动(位置);
MainActivity.adapter.notifyDataSetChanged(); }
});
}保护无效DeleteTask活动(INT位置){
MainActivity.adapter.clear(位置);
Toast.makeText(的getContext(),任务已删除,Toast.LENGTH_SHORT).show();
MainActivity.adapter.notifyDataSetChanged(); //测试 - 问题无法删除项目。
}
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.
}
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.
}
这篇关于点击ImageView的插件删除列表视图,从一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!