从listview xamarin中删除一行

时间:2014-08-08 11:03:01

标签: android listview android-listview

我有一个活动,其中有一些按钮和listview.eview行的listview有一个删除按钮,我打算在用户单击删除按钮时删除该行。 我正在使用自定义适配器,所以这是我的getview方法:

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            convertView = menuActivity.LayoutInflater.Inflate(Resource.Layout.OrderItemLayout, null);
            ImageButton btn = convertView.FindViewById<ImageButton>(Resource.Id.btn_del);
            btn.Tag = position;
            btn.Click += btn_Click;
            return convertView;
        }

void btn_Click(object sender, EventArgs e)
    {
        _lstOrder.RemoveAt((int)((sender as ImageButton)).Tag);
        NotifyDataSetChanged();
    }

但问题是,当我点击删除按钮时,列表视图不会刷新,因此删除的行仍然存在,但如果我返回然后再次进入活动,它将会消失。 我怎么能解决这个问题所以我删除一行后listview刷新? 谢谢

1 个答案:

答案 0 :(得分:0)

创建一个共同的arraylist

ArrayList<GeterSetterClass> comn_arylist = new ArrayList<GeterSetterClass>();

自定义适配器设置

custdet_adapter adapter = new custdet_adapter(this,comn_arylist);

listview.setAdapter(adapter);
listview.invalidateViews();

自定义适配器类

public static class custdet_adapter extends BaseAdapter {
    private ArrayList<GeterSetterClass> cust_arrayList;
    private LayoutInflater l_Inflater;

    public custdet_adapter(Context context,
                ArrayList<GeterSetterClass> list_details) {
            cust_arrayList = list_details;
            l_Inflater = LayoutInflater.from(context);
    }

    public int getCount() {
            return cust_arrayList.size();
    }

    public Object getItem(int position) {
            return cust_arrayList.get(position);
    }

    public long getItemId(int position) {
            return position;
    }

    public View getView(final int position, View convertView,
                ViewGroup parent) {
            final ViewHolder holder;
             if (convertView == null) {
                 convertView = l_Inflater.inflate(R.layout.layoutname_struct,null);
                holder = new ViewHolder();
                holder.txt_custname = (TextView) convertView
                        .findViewById(R.id.textView1);
                holder.txt_mobno = (TextView) convertView
                        .findViewById(R.id.textView2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txt_custname.setText(cust_arrayList.get(position).getName());
        holder.txt_mobno.setText(cust_arrayList.get(position).getunitcd());

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

                comn_arylist.remove(position);
                listview.invalidateViews();
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView txt_custname;
        TextView txt_mobno;
    }
}