删除从定制列表视图

问题描述:

一个项目我有我做了一个自定义适配器它称为Myadp一个ListView:删除从定制列表视图

public class Myadp extends ArrayAdapter { 
    private final String[] web; 
    private final String[] t; 
    public Myadp(Context context, int resource, int textViewResourceId, final String[] objects, String[] total) { 
     super(context, resource, textViewResourceId, objects); 
     this.web = objects; 
     this.t = total; 
    } 

    @Override 
    public View getView(final int position, final View convertView, ViewGroup parent) { 

     final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = inflater.inflate(R.layout.list_temp, parent, false); 

     TextView tv2 = (TextView) row.findViewById(R.id.textView2); 
     TextView tv4 = (TextView) row.findViewById(R.id.textView4); 


     tv2.setText(web[position]); 
     tv4.setText(String.valueOf(t[position])); 
     ImageButton del = (ImageButton) row.findViewById(R.id.imageButton); 

     del.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

     }); 


     return row; 
    } 
} 

当上删除用户点击的ImageButton该项目将被删除。 我尝试了很多,例如我试图从数组中删除该项目(都是网络& t数组),并重新调用ListView适配器,但我没有成功,我搜索谷歌和Stackoverflow,但所有的代码只是简单的listview适配器。所以现在我需要你的帮助。

+0

你实际上没有在你的ListView的按钮的OnClickListener中做任何事情,所以没有什么会发生。这是你使用的代码吗?如果你正在做这样的事情,只需扩展BaseAdapter而不是ArrayAdapter可能更有意义。另外[看到这个](http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons),如果你只有一个按钮是一样的。 – JonasCz

+0

不我已经删除了del按钮的代码 –

+0

我想显示我的列表视图适配器的一般视图形式 –

删除按钮的点击:

  1. 从中用来填充ListView
  2. 列表或字符串[]对象中删除元素然后调用notifyDataSetChanged()

在您的自定义适配器调用this.notifyDataSetChanged();您正在执行删除功能并从设置为该适配器的arrayList中删除该元素。如果你知道哪个记录是你可以使用ArrayList.remove(index),那么你必须从ArrayList中移除该记录。然后使用notifyDataSetChanged();

编辑:转到您的适配器并添加一个方法delete();

public void delete(int position){ 
data.remove(position); 
notifyItemRemoved(position); 
} 

在你的onClick();方法添加此:

delete(getPosition()); 
+0

正如你所看到的,我使用数组而不是ArrayList,并且从数组中删除一个元素与ArrayList不同 –