从列表视图的Andr​​oid删除一行 [英] delete a row from a listview android

查看:149
本文介绍了从列表视图的Andr​​oid删除一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个textviews和我的ListView的每一行中的一个按钮,按钮单击我想从列表视图中删除paticular一行。我已经尝试了许多codeS,但没有成功。请提前做help.Thanks!

I have three textviews and a button in each row of my listview , on button click i want to remove the paticular row from the listview. I have tried many codes but without any success. Please do help.Thanks in advance!!!

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

}

推荐答案

使用:

 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,是这样的:

note that you can delete just foodname because that just passed to super constructor, I think you should declare objects and send that object to arraylist, like this :

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

和使用这样的

VivzAdapter(Context c,ArrayList<object>){

}

希望帮助你!

这篇关于从列表视图的Andr​​oid删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆