带有添加到收藏夹列表中的商品的订单列表视图

时间:2019-05-22 08:15:56

标签: java android android-listview

在列表视图中,当项目将标签设置为like时,我想在列表视图的顶部添加项目。我的意思是,如果用户将项目添加到收藏夹列表中,则主列表视图更新将显示在列表视图的顶部。

我的问题是:如何将列表视图更改为基于标签订购?

我从xml这样的资产中读取数据:

listView = (ListView) findViewById(R.id.listview);
try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(getAssets().open("info.xml"));
        DataCollection = new ArrayList<HashMap<String, String>>();

        doc.getDocumentElement().normalize();
        NodeList placeList = doc.getElementsByTagName("info");
        HashMap<String, String> map = null;
        for (int i = 0; i < placeList.getLength(); i++) {
            map = new HashMap<String, String>();
            Node firstplaceNode = placeList.item(i);
            if (firstplaceNode.getNodeType() == Node.ELEMENT_NODE) {
                Element firstplaceElement = (Element) firstplaceNode;

                NodeList idList = firstplaceElement.getElementsByTagName(KEY_ID);
                Element firstIdElement = (Element) idList.item(0);
                NodeList textIdList = firstIdElement.getChildNodes();
                map.put(KEY_ID, ((Node) textIdList.item(0)).getNodeValue().trim());

                NodeList name = firstplaceElement.getElementsByTagName(KEY_NAME);
                Element nameElement = (Element) name.item(0);
                NodeList name_ = nameElement.getChildNodes();
                map.put(KEY_NAME, ((Node) name_.item(0)).getNodeValue().trim());

                NodeList pdf = firstplaceElement.getElementsByTagName(KEY_PDF);
                Element pdfElement = (Element) pdf.item(0);
                NodeList pdf_ = pdfElement.getChildNodes();
                map.put(KEY_PDF, ((Node) pdf_.item(0)).getNodeValue().trim());

                DataCollection.add(map);
            }
        }

    PdfAdapter bindingData = new PdfAdapter(this, DataCollection);
    listView.setAdapter(bindingData);

然后在我的适配器中,我使用settag将它们添加到收藏夹列表中:

  fav_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (fav_btn.getTag().equals("unlike")) {


                PdfInfo.Name_Display.add(DataCollection.get(position).get(KEY_NAME));
                PdfInfo.Pdf_Show.add(DataCollection.get(position).get(KEY_PDF));
                fav_btn.setImageResource(R.drawable.like);
                fav_btn.setTag("like");
                notifyDataSetChanged();

                SharedPreferences appSharedPrefs = PreferenceManager
                        .getDefaultSharedPreferences(context.getApplicationContext());
                SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
                Gson gson = new Gson();
                String json1 = gson.toJson(PdfInfo.Name_Display);
                String json2 = gson.toJson(PdfInfo.Pdf_Show);
                prefsEditor.putString("name", json1);
                prefsEditor.putString("pdf", json2);
                prefsEditor.commit();


            } else {

                PdfInfo.Name_Display.remove(DataCollection.get(position).get(KEY_NAME));
                PdfInfo.Pdf_Show.remove(DataCollection.get(position).get(KEY_PDF));
                fav_btn.setImageResource(R.drawable.unlike);
                fav_btn.setTag("unlike");
                notifyDataSetChanged();
                SharedPreferences appSharedPrefs = PreferenceManager
                        .getDefaultSharedPreferences(context.getApplicationContext());
                SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
                Gson gson = new Gson();
                String json1 = gson.toJson(PdfInfo.Name_Display);
                String json2 = gson.toJson(PdfInfo.Pdf_Show);
                prefsEditor.putString("name", json1);
                prefsEditor.putString("pdf", json2);
                prefsEditor.commit();
            }

            bindingData = new PdfAdapter(activity, DataCollection);
            bindingData.notifyDataSetChanged();
        }

0 个答案:

没有答案