[英]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.