我有ListView
。我的ListView
中的问题是,一旦我从ListView
删除项目并再次在ListView
中添加新项目而不退出应用程序,它会在我的{{1}中显示已删除的项目而不是显示新添加的项目,一旦我退出应用程序并打开应用程序它显示新添加的项目,并不显示已删除的项目。它与刷新问题有关,尽管我使用了notifyDataSetChanged()。
为什么会发生这种情况,请帮助我解决这个问题。
ListView
答案 0 :(得分:1)
您需要使用新值更新listItem,然后调用notifyDataSetChanged();如下
listItem.clear();
listItem.addAll(ArrayList<String>);// put update ArrayList Value addAll() method
notifyDataSetChanged();
答案 1 :(得分:0)
这里的m_ItemCount是什么???
此外,您只是从列表中删除数据。实际上,也将其从数据的实际位置中删除。因为每次列表无效时,它都会存储先前的数据,因此也将其从实际位置中删除。
答案 2 :(得分:0)
根据您的要求使用此代码,因为它适用于我:
int m_ItemCount=10;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
ListView listView;
ArrayList<String> fileStrings = new ArrayList<String>(m_ItemCount);
for (int i = 0; i < m_ItemCount; i++)
{
fileStrings.add(i+"");
/*if(NoteManager.getSingletonObject().getStatusArray(i) == 1)
{
m_CompletedItemCount++;
}*/
}
// Call ListViewAdapter class that extend BaseAdapter class inside the method setListAdapter
// Display all the items in the list view
ListViewAdapter m_Adapter = new ListViewAdapter(this, fileStrings);
setListAdapter(m_Adapter);
listView = getListView();
// Adapter Class
}
class ListViewAdapter extends BaseAdapter
{
private Activity activity;
private ArrayList<String> listItem;
private LayoutInflater inflater=null;
public ListViewAdapter(Activity a, ArrayList<String> d)
{
activity = a;
listItem = d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
return listItem.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public class ViewHolder
{
Button butDelete, butAdd;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.item, null);
holder.butDelete = (Button)convertView.findViewById(R.id.but_delete);
holder.butAdd = (Button)convertView.findViewById(R.id.but_add);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
// Delete item on click of delete item
holder.butDelete.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
deleteItem(position);
}
});
holder.butAdd.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
addItem("AA");
}
});
return convertView;
}
// Add item in listView
public void addItem(String stringName)
{
listItem.add(0, stringName);
notifyDataSetChanged();
}
// delete item in listView
public void deleteItem(int position)
{
listItem.remove(position);
notifyDataSetChanged();
}
}