适配器addAll替换上一页android的项目

时间:2019-11-21 09:53:04

标签: android android-adapter

当用户点击我的列表视图的底部时,将加载并显示第二页。问题在于,发生这种情况时,第一页的项目将替换为第二页的项目。该视频显示了以下行为:https://www.ubris.design/video.mp4 这是我的代码,位于我的Web服务的响应回调中:

try {

        JSONObject obj = new JSONObject(result);

        JSONObject response = obj.getJSONObject("response");
        Integer count = Integer.parseInt(response.getString("count"));

        //Toast.makeText(getApplicationContext(),count+"", Toast.LENGTH_LONG).show();


        inputItems.clear();

        for(int i = 0; i < count; i++){
            JSONObject tmp = response.getJSONObject(i+"");

            id = tmp.getString("id");
            materia = tmp.getString("materia");
            adsce = tmp.getString("adsce");
            nomeCompleto = tmp.getString("nomeCompleto");
            matricola = tmp.getString("matricola");

            username = tmp.getString("username");
            password = tmp.getString("password");
            uni = tmp.getString("uni");
            descrizione = tmp.getString("descrizione");

            type = tmp.getString("type");
            voto = tmp.getString("voto");
            votanti = tmp.getString("votanti");
            ts = tmp.getString("ts");

            //url
            siteTitle = tmp.getString("siteTitle");
            siteDescription = tmp.getString("siteDescription");
            siteImage = tmp.getString("siteImage");
            address = tmp.getString("address");

            //file
            filename = tmp.getString("filename");
            filesize = tmp.getString("filesize");

            combinedSize = tmp.getString("combinedSize");

            //Toast.makeText(getApplicationContext(), filename+address, Toast.LENGTH_LONG).show();

            inputItems.add(new DTODocSelected(id, materia, adsce, nomeCompleto, matricola, null, null, uni, descrizione,type, voto, votanti, ts,
                    siteTitle, siteDescription, siteImage, address,
                    filename, filesize, combinedSize
                    ));
        }





    } catch (Exception e){

        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
        e.printStackTrace();

    }



    if(inputItems.size() > 0){


        if(page == 0){

            adapter = new AdapterDocMateriaDocsSelect(getApplicationContext(),
                    R.id.activity_docs_doc_selected, inputItems);


            listView.setAdapter(adapter);
            listView.setSmoothScrollbarEnabled(true);

            listView.setScrollingCacheEnabled(false);
            listView.setDrawingCacheEnabled(false);
            listView.destroyDrawingCache();
            listView.buildDrawingCache(false);
            listView.getDrawingCache(false);


            double vv = Integer.valueOf(counterCC);
            double pagineMax = Math.ceil( vv / 5);
            int pagineMaxInt = (int) pagineMax;
            if(page == (pagineMaxInt - 1)){
                pagination = false;
                Toast.makeText(getApplicationContext(), "pagination false", Toast.LENGTH_SHORT).show();
            }


        } else {

            //Toast.makeText(getApplicationContext(), ""+inputItems.size(), Toast.LENGTH_SHORT).show();


            double vv = Integer.valueOf(counterCC);
            double pagineMax = Math.ceil( vv / 5);
            int pagineMaxInt = (int) pagineMax;
            if(page == (pagineMaxInt - 1)){
                pagination = false;
                Toast.makeText(getApplicationContext(), "pagination false", Toast.LENGTH_SHORT).show();
            }





            adapter.addAll(inputItems);

            //inputItems.clear();
            //listView.setSmoothScrollbarEnabled(true);
            //listView.smoothScrollToPosition(page * 5);


        }



    } else {

        pagination = false;
        //Toast.makeText(getApplicationContext(), "zero", Toast.LENGTH_LONG).show();
    }

1 个答案:

答案 0 :(得分:0)

解决我刚刚替换了adapter.addAll(inputItems);使用adapter.notifyDataSetChanged();并删除了inputItems.clear();