我有一个ListView
. 我的问题ListView
是,一旦我从我的项目中删除一个项目ListView
并再次在其中添加一个新项目ListView
而不退出应用程序,它会在我的应用程序中显示已删除的项目ListView
而不是显示新添加的项目,一旦我退出应用程序并打开应用程序它显示新添加的项目,而不显示已删除的项目。它与刷新问题有关,尽管我使用过notifyDataSetChanged()
.
为什么会这样,请帮我解决这个问题。
// This is on create of Activity Class
ArrayList<String> fileStrings = new ArrayList<String>(m_ItemCount);
for (int i = 0; i < m_ItemCount; i++)
{
fileStrings.add(NoteManager.getSingletonObject().getImageName(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
m_Adapter = new ListViewAdapter(this, fileStrings);
setListAdapter(m_Adapter);
listView = getListView();
// Adapter Class
public 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.list_item, null);
holder.butDelete = (Button)convertView.findViewById(R.id.but_delete);
holder.butAdd = (Button)convertView.findViewById(R.id.but_delete);
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(name);
}
});
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();
}
}