繁体   English   中英

如何在Android上用新的旧适配器替换旧的列表适配器? 它不断将新的附加到旧的

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

我调用一个函数createTheList来发送布局和数组适配器。 然后在该方法中,创建我的自定义setListAdapter。 好吧,我想根据程序中的过滤器更改列表。 即,如果一个人单击一个按钮,它将显示一个不同的列表。 因此,我想再次调用createTheList方法,并在替换旧列表的同时重新创建列表。 我很难解决这个问题。 如果我使用其他适配器再次调用createTheList,它将仅追加到旧列表中。 谁能指导我完成这个过程?

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;

        }
     });

从哪里调用createTheList:

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”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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