我看了很多例子,并设法做到这一点,但我仍然无法从按钮中删除我的数组中的项目。如何正确使用删除(位置)?
公共类适配器扩展ArrayAdapter {
public adapter(Context context, ArrayList<User> users) {
super(context, 0, users);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
User user = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
}
TextView question = (TextView) convertView.findViewById(R.id.question);
TextView answer = (TextView) convertView.findViewById(R.id.answer);
question.setText(user.name);
answer.setText(user.hometown);
ImageButton remove = (ImageButton) convertView.findViewById(R.id.removeButton);
remove.setTag(position);
remove.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
notifyDataSetChanged();}
});
return convertView;
}
}
用户列表
public class User {
public String name;
public String hometown;
public User(String name, String hometown) {
this.name = name;
this.hometown = hometown;
}
public static ArrayList<User> getUsers() {
ArrayList<User> users = new ArrayList<>();
users.add(new User("Hi", "hi"));
users.add(new User("Marla", "San Francisco"));
users.add(new User("Sarah", "San Marco"));
return users;
}
}
答案 0 :(得分:0)
这会对你有帮助, Delete a ListItem by clicking the ImageView within the ListItem
答案 1 :(得分:0)
在特定事件上删除用户的arrayList对象,然后简单地调用dapter.notifyDataSetChanged()方法。
答案 2 :(得分:0)