Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android上用新的列表适配器替换旧的列表适配器?它不断地在旧的基础上增加新的_Java_Android_Android Listview_Android Arrayadapter_Listadapter - Fatal编程技术网

Java 如何在Android上用新的列表适配器替换旧的列表适配器?它不断地在旧的基础上增加新的

Java 如何在Android上用新的列表适配器替换旧的列表适配器?它不断地在旧的基础上增加新的,java,android,android-listview,android-arrayadapter,listadapter,Java,Android,Android Listview,Android Arrayadapter,Listadapter,我调用一个函数createList来发送布局和数组适配器。然后在该方法中创建自定义setListAdapter。嗯,我想根据我程序中的过滤器更改我的列表。也就是说,如果一个人点击一个按钮,它会显示一个不同的列表。因此,我想再次调用createTheList方法,并在替换旧列表的同时重新创建列表。我真的很难弄明白这一点。如果我使用不同的适配器再次调用createTheList,它只会附加到旧列表中。有人能带我看一下吗 public void createTheList(final int num,

我调用一个函数createList来发送布局和数组适配器。然后在该方法中创建自定义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;

        }
     });

notifyDataSetCHanged没有帮助?

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

在调用另一个List之前,尝试清除字符串数组变量“array”。

您能提供再次调用CreateTList的代码吗?好的,我编辑了它。它是主java文件中的代码段。如果需要,我可以发送或发布整个文件,但它非常大。我调用getShifts()方法,它会返回一个数据数组。我想将该数据转换为listview。因此,我使用createTheList()方法,创建自定义setListAdapter,以便基于数据创建唯一的行。例如,在某些行上显示一些按钮等等。不,我尝试了各种实现。我不确定我是做错了,还是没有帮助。我认为这是我的代码的问题之一。我不知道我的列表对象是什么?我只是随机调用setListAdapter,它没有设置为变量或任何我可以修改的内容。请将列表设置为强类型。你有权访问它吗?什么意思?使它成为强类型?我相信我能找到它,是的。我想我明白你的问题了。如果我正确理解了您的代码,那么您正在将adapterArray传递到方法中并对列表进行更改。但是,一旦createTheList()完成执行,其所有变量都将超出范围。换句话说,您似乎需要让createTheList()返回新列表并分配它。(即adapterArray=createTheList())这有帮助吗…?我根本没有创建adapterArray。我正在调用函数,并且在setListAdapter(新的ArrayAdapter…)中,我认为这就是我创建适配器的时候。我认为您对范围的想法是正确的,我将尝试让createTheList返回一个数组适配器。一旦我做了adapaterArray=createTheList(),我该怎么做才能用新列表替换我的旧列表?我也不允许。我试图向setListAdapter发送一个完全不同的数组,以便内容是新的和新鲜的,但没有帮助。如果您能提供一个输出的小屏幕截图,我想知道下一个列表是如何附加到上一个列表的。这些列表被合并到一个列表中。没有区别。我真的不能发布屏幕截图。但它像data1,data2,data3,differentdata1,differentdata2
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);
}