简体   繁体   中英

How can I replace my old list adapter with a new one on Android? It keeps appending new to old one

I call a function createTheList that sends my layout, and array adapter. Then within the method I create my custom setListAdapter. Well, I want to change my list based on filters within my program. ie, if a person clicks a button, it shows a different list. So, I want to call the createTheList method again, and re-create the list while replacing my old list. I am having a real tough time figuring this out. If I call createTheList again with a different adapter, it just appends to the old list. Can anyone please walk me through this?

public void createTheList(final int num, String[] Array ){


      setListAdapter(new ArrayAdapter<String>(getActivity(), layoutName, R.id.myText, Array){         

        @Override

        public View getView(int position, View convertView, ViewGroup parent){



           final View row = super.getView(position, convertView, parent);
           Button commentButton = (Button) row.findViewById(R.id.button1);
           Button missingButton = (Button) row.findViewById(R.id.button2);
           Button rideAlongButton = (Button) row.findViewById(R.id.button3);


            if (arrayOfShifts[position].getRideAlongCount()>0 && arrayOfShifts[position].positionOnList == position){
                rideAlongButton.setVisibility(row.VISIBLE);
            }
            else{
                rideAlongButton.setVisibility(row.GONE);
            }

            if (arrayOfShifts[position].getMissingCnt() > 0 && arrayOfShifts[position].positionOnList == position){
                missingButton.setVisibility(row.VISIBLE);
            }
            else{
                missingButton.setVisibility(row.GONE);
            }

            if (arrayOfShifts[position].getHasComment() > 0 && arrayOfShifts[position].positionOnList == position){
                commentButton.setVisibility(row.VISIBLE);
            }
            else{
                commentButton.setVisibility(row.GONE);
            }


            return row;

        }
     });

Where createTheList is being called from:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    layoutName = R.layout.fragtest1;
    //My values to call a function
    cache.put("Value1", "02-12-2014");
    cache.put("Value2", 1);

    //getShifts method returns an array.
    String[] adapterArray = getShifts("GetShifts3" , cache);

    createTheList(layoutName, adapterArray);

    //If I add the following code, which is just a sample to replace my old list
    //instead of removing the old list, it appends to the old list.

    cache.clear();
    //New Data
    cache.put("Value1", "02-12-2014");
    cache.put("Value2", 2);

    String[] adapterArray2= getShifts("GetShifts3" , cache);

    createTheList(layoutName, adapterArray2);
}

notifyDataSetCHanged没有帮助吗?

您是否考虑过使用List对象的RemoveAll()或Clear()方法。

尝试在调用另一个列表之前清除String数组变量“ Array”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM