从listview android中删除一行

时间:2014-08-02 03:10:03

标签: android listview android-listview android-arrayadapter

我在列表视图的每一行都有三个文本视图和一个按钮,点击按钮我想从列表视图中删除特定行。我尝试了很多代码但没有任何成功。请帮帮忙。谢谢!

VivzAdapter adapter=new VivzAdapter(this,foodname,quantity,amount);
view.setAdapter(adapter);
view.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view2,
   int position, long id) {
  int itemPosition     = position;}
}); 

}

}
class VivzAdapter extends ArrayAdapter<String>
{
String[] t;
String[] u;
String[] d;
Context context;

VivzAdapter(Context c,String[] foodname,String [] quantity,String[] amount)

{

    super(c,R.layout.listviewcustom,R.id.tv1,foodname);
    this.context=c;
    this.t=foodname;
    this.u=quantity;
    this.d=amount;



}



@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflater=(LayoutInflater)       context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row=inflater.inflate(R.layout.listviewcustom,parent,false);
    TextView t1=(TextView) row.findViewById(R.id.tv1);
    TextView t2=(TextView) row.findViewById(R.id.tv2);
    TextView t3=(TextView) row.findViewById(R.id.tv3);
     Button b=(Button)row.findViewById(R.id.cancelbutton);
     b.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View arg0) {

        }


        });

    t1.setText(t[position]);
    t2.setText(u[position]);
    t3.setText(d[position]);



    return row;
}

}

1 个答案:

答案 0 :(得分:0)

使用它:

 final int listItemPosition = position;
 Button b=(Button)row.findViewById(R.id.cancelbutton);
 b.setOnClickListener(new View.OnClickListener() {


    @Override
    public void onClick(View arg0) {

    foodname.remove(listItemPosition);
    VivzAdapter.this.notifyDataSetChanged();

    }


    });

请注意,您可以删除foodname,因为它只是传递给超级构造函数,我认为您应该这样做 声明对象并将该对象发送到arraylist,如下所示:

class object {
String foodname;
String quantity;
String amount;
} 

并像这样使用

VivzAdapter(Context c,ArrayList<object>){

}
希望能帮到你!!!