我使用编辑文本字段和按钮创建了自定义视图。我想通过单击按钮从列表视图中删除项目。 自定义视图的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/delete_btn"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/player_name_txt"
android:textColor="#ffffff"
android:editable="false"
android:background="#5eb8ed"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/delete_btn"
android:layout_toLeftOf="@+id/delete_btn"
android:layout_toStartOf="@+id/delete_btn" />
</RelativeLayout>
适配器和列表视图的代码:
inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
playerListView = (ListView)this.findViewById(android.R.id.content).getRootView().findViewById(R.id.player_list);
playerListAdapter = new ArrayAdapter(this,R.layout.player_item,R.id.player_name_txt, new ArrayList<String>(){});
playerListView.setAdapter(playerListAdapter);
我试过的守则:
public void removePlayer(View v)
{
EditText pairedEdit = (EditText)findViewById(R.id.player_name_txt);
String name = pairedEdit.getText().toString();
playerListAdapter.remove(name);
playerListAdapter.notifyDataSetChanged();
}
现在它删除了列表视图中的第一个项目。
AlertDialog.Builder addPlayerBuilder = new AlertDialog.Builder(this);
final View customView = inflater.inflate(R.layout.add_player,null);
final EditText usernameEdit = (EditText)customView.findViewById(R.id.username_edit);
addPlayerBuilder.setView(customView);
addPlayerBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String name = usernameEdit.getText().toString();
playerListAdapter.add(name);
playerListAdapter.notifyDataSetChanged();
dialog.dismiss();
}
});
addPlayerBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog dialog = addPlayerBuilder.create();
dialog.show();
答案 0 :(得分:1)
在自定义布局的按钮中放置一个 onclicklistener ,它会根据其位置通过删除()函数从arraylist中删除该元素,然后调用 notifyDataSetInvalidated 强>()
答案 1 :(得分:0)
使用此:
Button b2 = (Button) row.findViewById(R.id.button1);
b2.setTag(arg0);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int pos = (int)arg0.getTag();
lista.remove(pos);
SunetePreferateAdaptor.this.notifyDataSetChanged(); }
});
答案 2 :(得分:0)
在delete_btn上的适配器类中,单击你必须调用remove方法:
public class yourAdapter extends BaseAdapter {
private static LayoutInflater inflater = null;
private Activity context;
private ArrayList<yourBean> arrList;
public yourAdapter(Activity context,
ArrayList<yourBean> arrList) {
this.context = context;
this.arrList = arrList;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return arrList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row,
null);
viewHolder.delete_btn= (Button) convertView
.findViewById(R.id.delete_btn);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.delete_btn.setTag(position);
viewHolder.delete_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
arrayList.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
static class ViewHolder {
Button delete_btn;
}
}