如何从 Android 的列表视图中删除行项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26032540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:56:38  来源:igfitidea点击:

How To Delete row item from List view in Android?

androidlistview

提问by user3905549

I have created a list view demo.list is created successfully and image are also added in right hand side properly. now i want when i click on a cross image in right side of row in list view,then that row will deleted.how to do this.

我创建了一个列表视图 demo.list 已成功创建,并且图像也正确添加到右侧。现在我想要当我在列表视图中单击行右侧的十字图像时,该行将被删除。如何执行此操作。

 public class MainActivity extends Activity {

            EditText editText;
            Button Button,Button1;
            ListView listView;
            ArrayList<String> listItems;
            BaseAdapter adapter;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);


                editText = (EditText) findViewById(R.id.editText);
              //  int pos = editText.getText().length();
                //editText.setSelection(pos);
                Button = (Button) findViewById(R.id.Button);
                listView = (ListView) findViewById(R.id.listview);
                //listView.setSelection(listView.getAdapter().getCount()-1);


                listItems = new ArrayList<String>();
                //listItems.add("First item");

                //code to only set the simple text in the listview
                //adapter = new ArrayAdapter<String>(this, R.layout., listItems);


                //Here is the creation of new anonymous class,because we can not create subclass inside any method so we create anonymous class inside the on create method
                adapter =new BaseAdapter() 
                {

                    @Override
                    public View getView(int arg0, View arg1, ViewGroup arg2)
                    {
                        // arg1 is the particular row position in list view weather the arg0 is the whole list view
                        // layout inflater for setting the text or an image on row in list view
                        LayoutInflater inflater = getLayoutInflater();
                        arg1 = inflater.inflate(R.layout.custom, null);
                        TextView textview = (TextView)arg1. findViewById(R.id.textView1);
                        textview.setText(listItems.get(arg0));
                        return arg1;
                    }

                    @Override
                    public long getItemId(int arg0) {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public Object getItem(int arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public int getCount() {
                        // TODO Auto-generated method stub
                        return listItems.size();
                    }
                };

                listView.setAdapter(adapter);


                Button.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        listItems.add(editText.getText().toString());
                        adapter.notifyDataSetChanged();

                        // code to display the previous added text in front through edit text in
                        listView.setSelection(listView.getAdapter().getCount()-1);
                        //code to clear edit text after enter the text in list view
                        editText.getText().clear();
                          }
                         });

                listView.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> a, View v, int position,
                            long id) {
                        Toast.makeText(MainActivity.this, "Item has been added into your list", Toast.LENGTH_LONG)
                                .show();
                    }
                });


            }
        }

回答by Sandeep Kumar

listItems.remove(position);

here position is your list row id

这里的位置是您的列表行 ID

notifyDataSetChanged();

回答by Kostya Khuta

Add delete button to your listItem, or add onClickListner to your image

将删除按钮添加到您的列表项,或将 onClickListner 添加到您的图像

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View row = null;
    LayoutInflater inflater = getLayoutInflater();
    row = inflater.inflate(R.layout.one_result_details_row, parent, false);
    ImageView image= (ImageView) row.findViewById(R.id.your_image);
     image.setTag(position);

    image.setOnClickListener(
        new OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer index = (Integer) view.getTag();
                listItems.remove(index.intValue());  
                notifyDataSetChanged();
            }
        }
    );

回答by Adithiyan

close.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            values.remove(position);
            SaleProductListAdapter.this.notifyDataSetChanged();
        }
    });