Android,它没有从 ListView 中删除项目
Android, It's not removing item from ListView
我在一个单独的文件中创建了一个 class 适配器,但在我的主文件中,我正在调用 remove 方法来删除一个项目或任何我 select 的项目,但是当我删除它时,它不会在我的应用程序上更新。
代码:
public static void populateChatListView(final Context context){
adapter = new myAdapter(context, R.layout.source_contact);
listv.setAdapter(adapter);
for (Contacts friend: friends){
adapter.add(friend);
}
listv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context, "DELETED", Toast.LENGTH_SHORT).show();
//supposed to remove it
adapter.remove(adapter.getItem(0));
//supposed to update it(notify)
adapter.notifyDataSetChanged();
return true;
}
});
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listv.setSelection(adapter.getCount() - 1);
}
});
}
它没有给我任何错误,也没有删除它。如果有任何帮助,我将不胜感激。
CLASS 适配器:
public class myAdapter extends ArrayAdapter<Contacts>{
private List<Contacts> Listc = new ArrayList<>();
TextView NameWindow, LastN;
ImageView contactProfile;
Context context;
public myAdapter(Context context, int resource) {
super(context, resource);
this.context = context;
}
@Override
public void add(Contacts object) {
Listc.add(object);
super.add(object);
}
@Override
public Contacts getItem(int position) {
return Listc.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.source_contact, parent, false);
}
NameWindow = (TextView) convertView.findViewById(R.id.contactN);
LastN = (TextView) convertView.findViewById(R.id.contactL);
contactProfile = (ImageView) convertView.findViewById(R.id.contactProfile);
String name, lastn;
Contacts provider = getItem(position);
name = provider.getName(); // name goes down
lastn = provider.getLastn(); // goes upper
NameWindow.setText(name);
LastN.setText(lastMessage);
return convertView;
}
...
已编辑。
我认为在你的适配器中你应该有一个像这样的数组变量。
class yourAdapter extends BaseAdapter {
private ArrayList<Object> your_list_variable;
....
}
您只需更新此列表,然后调用
adapter.notifyDataSetChanged();
试试吧。
// 在这里更新一个例子,你可以检查我的代码。
public class MainActivity extends ActionBarActivity {
private ListView myList;
private myAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new myAdapter(this, android.R.layout.simple_list_item_1);
myList = (ListView) findViewById(R.id.myList);
for(int i = 0; i < 100; i++) {
mAdapter.add("item : " + (i+1));
}
myList.setAdapter(mAdapter);
myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int index, long id) {
System.out.println("index : " + index);
System.out.println("item : " + mAdapter.getItem(index));
mAdapter.remove(mAdapter.getItem(index));
mAdapter.notifyDataSetChanged();
return false;
}
});
}
class myAdapter extends ArrayAdapter<String> {
private List<String> listData = new ArrayList<>();
public myAdapter(Context context, int resource) {
super(context, resource);
}
@Override
public void add(String object) {
super.add(object);
listData.add(object);
}
@Override
public void remove(String object) {
super.remove(object);
listData.remove(object);
}
@Override
public String getItem(int position) {
return super.getItem(position);
}
}
希望对您有所帮助。
将此添加到您的适配器中class
public void myRemove(int position){
Listc.remove(position);
myAdapter.super.notifyDataSetChanged();
}
并使用它来查看它是否会有所作为
我在一个单独的文件中创建了一个 class 适配器,但在我的主文件中,我正在调用 remove 方法来删除一个项目或任何我 select 的项目,但是当我删除它时,它不会在我的应用程序上更新。
代码:
public static void populateChatListView(final Context context){
adapter = new myAdapter(context, R.layout.source_contact);
listv.setAdapter(adapter);
for (Contacts friend: friends){
adapter.add(friend);
}
listv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context, "DELETED", Toast.LENGTH_SHORT).show();
//supposed to remove it
adapter.remove(adapter.getItem(0));
//supposed to update it(notify)
adapter.notifyDataSetChanged();
return true;
}
});
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listv.setSelection(adapter.getCount() - 1);
}
});
}
它没有给我任何错误,也没有删除它。如果有任何帮助,我将不胜感激。
CLASS 适配器:
public class myAdapter extends ArrayAdapter<Contacts>{
private List<Contacts> Listc = new ArrayList<>();
TextView NameWindow, LastN;
ImageView contactProfile;
Context context;
public myAdapter(Context context, int resource) {
super(context, resource);
this.context = context;
}
@Override
public void add(Contacts object) {
Listc.add(object);
super.add(object);
}
@Override
public Contacts getItem(int position) {
return Listc.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.source_contact, parent, false);
}
NameWindow = (TextView) convertView.findViewById(R.id.contactN);
LastN = (TextView) convertView.findViewById(R.id.contactL);
contactProfile = (ImageView) convertView.findViewById(R.id.contactProfile);
String name, lastn;
Contacts provider = getItem(position);
name = provider.getName(); // name goes down
lastn = provider.getLastn(); // goes upper
NameWindow.setText(name);
LastN.setText(lastMessage);
return convertView;
}
...
已编辑。
我认为在你的适配器中你应该有一个像这样的数组变量。
class yourAdapter extends BaseAdapter {
private ArrayList<Object> your_list_variable;
....
}
您只需更新此列表,然后调用
adapter.notifyDataSetChanged();
试试吧。
// 在这里更新一个例子,你可以检查我的代码。
public class MainActivity extends ActionBarActivity {
private ListView myList;
private myAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new myAdapter(this, android.R.layout.simple_list_item_1);
myList = (ListView) findViewById(R.id.myList);
for(int i = 0; i < 100; i++) {
mAdapter.add("item : " + (i+1));
}
myList.setAdapter(mAdapter);
myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int index, long id) {
System.out.println("index : " + index);
System.out.println("item : " + mAdapter.getItem(index));
mAdapter.remove(mAdapter.getItem(index));
mAdapter.notifyDataSetChanged();
return false;
}
});
}
class myAdapter extends ArrayAdapter<String> {
private List<String> listData = new ArrayList<>();
public myAdapter(Context context, int resource) {
super(context, resource);
}
@Override
public void add(String object) {
super.add(object);
listData.add(object);
}
@Override
public void remove(String object) {
super.remove(object);
listData.remove(object);
}
@Override
public String getItem(int position) {
return super.getItem(position);
}
}
希望对您有所帮助。
将此添加到您的适配器中class
public void myRemove(int position){
Listc.remove(position);
myAdapter.super.notifyDataSetChanged();
}
并使用它来查看它是否会有所作为