简体   繁体   English

如何将删除按钮 (x) 附加到动态创建的 ListView 的每个成员?

[英]How can I attach a delete button (x) to each member of a dynamically created ListView?

I have a dynamically created list.我有一个动态创建的列表。 It's fragment-layout is as follows:它的片段布局如下:

<ListView
android:id="@+id/colorsList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

When I add a row to the listview, I want an "X" mark to appear at the end of the row so that when the user clicks on the "X", the row gets deleted.当我向列表视图添加一行时,我希望在行的末尾出现一个“X”标记,以便当用户单击“X”时,该行被删除。

Not sure, if its relevant but, the way I am adding and removing rows to the listview is as follows.不确定,如果它相关,但我向列表视图添加和删除行的方式如下。

Logic to add a row tot he listView:在 listView 中添加一行的逻辑:

@BindView(R.id.understood_languages_list)
View mColorsList;

*********
*********

ListView listFavoriteColors;
listFavoriteColors = (ListView) mColorsList;
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, getColorsList());
listFavoriteColors.setAdapter(arrayAdapter);

Logic to remove a row from the ListView.从 ListView 中删除一行的逻辑。

listFavoriteColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        initializeView();
        arrayAdapter.remove(((TextView)view).getText().toString());
    }
});

Any helpful suggestions on how this can be achieved is much appreciated.非常感谢任何有关如何实现这一点的有用建议。

User Custom Adapter with custom Layout to attach delete button to each item in listview具有自定义布局的用户自定义适配器将删除按钮附加到列表视图中的每个项目

try this code:试试这个代码:

CustomAdapter自定义适配器

public class ListAdapter extends ArrayAdapter<String> {

private int resourceLayout;
private Context mContext;
ListInterFace interface;

public ListAdapter(Context context, int resource, List<String> 
   items,ListInterFace interface) {
    super(context, resource, items);
    this.resourceLayout = resource;
    this.mContext = context;
    this.interface=interface;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    if (v == null) {
        LayoutInflater vi;
        vi = LayoutInflater.from(mContext);
        v = vi.inflate(resourceLayout, null);
    }

    String value= getItem(position);

    if (value!= null) {
        TextView tt1 = (TextView) v.findViewById(R.id.dummytext);
        Button cancel_click=v.findViewById(R.id.cancel_click);

        if (tt1 != null) {
            tt1.setText(value);
        }
       //remove item on button click
        cancel_click.setOnClickListener(new  View.OnClickListener(){
        @Override
        public void onClick(View view){
            interface.removeItem(position);
        }
    })
    }

    return v;
}

} }

R.layout.itemlistrow defines the row of the ListView. R.layout.itemlistrow 定义了 ListView 的行。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
     <TextView
         android:id="@+id/dummytext"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Dummy"
         android:textColor="@android:color/black"
         android:textSize="18sp"
         android:padding="5dp"/>
     <ImageView 
          android:id="@+id/cancel_click"
         android:layout_width="20dp"
         android:layout_height="20dp"
         android:src="@mipmap/cancel"
       android:layout_alignParentRight="true"
         android:layout_margin="5dp"/>
  </RelativeLayout>
</LinearLayout>

In the MainActivity define ListViewlike this,在MainActivity中定义ListView这样,

ListView yourListView = (ListView) findViewById(R.id.itemListView);

// get data from the table by the ListAdapter
 ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, myList,  new ListInterFace () {
                    @Override
                      public void removeItem( int position) {
                      myList.remove(position);
                    customAdapter .notifyDataSetChanged();
                 });

 yourListView .setAdapter(customAdapter);

InterFace界面

public interface ListInterFace  {
   void removeItem(int position);
}

try this link to remove item from list view on button click Remove Item From List View On Button Click尝试此链接以从按钮单击的列表视图中删除项目单击从列表视图中删除项目单击按钮

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在动态创建的卡片视图上,如果按下特定于卡片的删除按钮,如何删除单个卡片? - On a card view created dynamically, how can I remove individual cards if a delete button specific to a card is pressed? 如何在ListView中实现删除按钮并从数据库中删除? - How can I implement a delete button in a ListView and delete from database? 如何管理动态创建的按钮的 onClickListener? - How can I manage onClickListener of a dynamically created button? 删除一个动态创建的按钮android - delete a dynamically created button android 如何使用在项目内创建的按钮删除ListView项目。 - how to delete a ListView item using a button created inside that item. 删除其中一项后,如何更新listView中每项显示的总价? - How can I update the total price shown for each item in the listView once I delete one of the items? 我还如何使enter将edittext中的输入文本添加到列表视图? 在创建的添加按钮之上 - How can I also make enter add input text in edittext to my listview? On top of the created add button 如何永久删除列表视图项? - How can I delete a listview item permanently? 删除其中一个项目后,如何立即更新listView中每个项目显示的总价? - How can I update the total price shown for each item right away in the listView once I delete one of the items? 如何在Vaadin 7框架中将FileDownloader附加到按钮渲染器 - How can I attach a fileDownloader to a button renderer in Vaadin 7 framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM