0

我有一个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();
    }
}
4

3 回答 3

1

您需要使用新值更新 listItem 然后调用 notifyDataSetChanged(); 如下

listItem.clear();           
listItem.addAll(ArrayList<String>);// put update ArrayList Value addAll() method
notifyDataSetChanged();
于 2012-06-18T06:54:08.107 回答
0

这里的 m_ItemCount 是什么???

此外,您只是从列表中删除数据..实际上,也将其从数据的实际位置中删除。因为每次您的列表失效时,它都会存储以前的数据,因此也将其从实际位置中删除。

于 2012-06-18T08:51:03.293 回答
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();
        }


    }
于 2012-06-18T09:47:52.017 回答