-1

我想通过单击“删除”按钮从列表视图中删除一行。我的列表视图项目水平放置了以下内容:TextView-1、TextView-2、TextView-3、ImageButton-delete 按钮。现在,当我单击删除按钮时,应该从视图中删除该行。以下是适配器代码;

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            View view = (View) v.getParent();
            TextView tv = (TextView) view.findViewById(R.id.item_name);
            String item = tv.getText().toString();
            String tableno = mListItems.get(0).getTableNumber();

            orderDetailsDB = new OrderDetailsDBAdapter( 
                    getApplicationContext());

            orderDetailsDB.deleteItem(item,tableno);

我尝试将单个文本视图设置为空白,但它不起作用。

holder.itemName.setText("");
holder.amount.setText("");
holder.quantity.setText("");

我阅读了几篇文章,他们建议从我的列表中删除项目(mListItems),然后执行adapter.notifyDataSetChanged();. 问题是我没有使用数组适配器来填充列表视图,而是使用自定义适配器,因此无法获取要删除的项目的位置。请指教。谢谢。

4

3 回答 3

6

getView首先在适配器的方法中写下一行。

button.setTag(position)

onClick方法中

            @Override
            public void onClick(View v) {
                int position = (Integer)v.getTag();
                yourarraylistObject.remove(position);
               // your remaining code
                notifyDataSetChanged();
               }
于 2013-08-22T13:52:22.060 回答
2

只需使用 remove() 从适配器中删除列表项

供你参考

adapter = new MyListAdapter(this);
    lv = (ListView) findViewById(android.R.id.list);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
        adb.setTitle("Delete?");
        adb.setMessage("Are you sure you want to delete " + position);
        final int positionToRemove = position;
        adb.setNegativeButton("Cancel", null);
        adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                MyDataObject.remove(positionToRemove);
                adapter.notifyDataSetChanged();
            }});
        adb.show();
        }
    });
于 2013-08-22T13:41:09.980 回答
0

为了获得要删除的位置,您可以做的是将其传递给您的侦听器:

// inside custom adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
.....
deleteButton.setOnClickListener(new MyClickListener(position);
}
private class MyClickListener implements OnClickListener
{
int position = -1;
public MyClickListener(final int position)
{
     this.position = position;
}
@Override
public void onClick(View v) {
     // do your delete code here
     notifyDataSetChanged();
}
于 2013-08-22T13:52:35.573 回答