为什么变量会根据Android版本而变化?

时间:2016-03-30 08:32:29

标签: android macos listview

我使用simpleAdapter获取了ListView。如果我点击列表视图中的某个项目,它会显示2个不同的变量。 一些代码:

// Listview Data
        ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(Invoice_numberArr.length);
        Map<String, Object> m;
        for(int i = 0; i<Invoice_numberArr.length; i++){
            int str = Integer.parseInt(Invoice_numberArr[i]);
            int str2 = Integer.parseInt(InvoiceNumberForDetails);
            if(str == str2){
                m = new HashMap<String, Object>();
                m.put(ATT_INU, Invoice_numberArr[i]);
                m.put(ATT_ANU, Article_numberArr[i]);
                m.put(ATT_AMO, AmountArr[i]);
                m.put(ATT_PRE, PriceArr[i]);
                m.put(ATT_TPR, Total_priceArr[i]);
                m.put(ATT_SDE, Sale_designationArr[i]);
                data.add(m);
            }
        }

        String[] from = {ATT_INU, ATT_ANU, ATT_AMO, ATT_PRE, ATT_TPR, ATT_SDE};
        int[] till = {R.id.txtInvoice_number, R.id.txtArticle_number, R.id.txtAmount, R.id.txtPrice, R.id.txtTotalPrice, R.id.txtSale_designation};

        //activities elements
        invoices_list_details_view = (ListView)findViewById(R.id.invoices_list_details_view);

        invoicesDetailsSAdapter = new SimpleAdapter(InvoicesDetails.this, data, R.layout.invoices_list_item_details_view, from , till);
        invoices_list_details_view.setAdapter(invoicesDetailsSAdapter);

        Utility Utility = new Utility();
        Utility.setListViewHeightBasedOnChildren(invoices_list_details_view);

        //if item clicked
        invoices_list_details_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String positions = parent.getAdapter().getItem(position).toString();

                //int positions = parent.getAdapter().;
                parent.getAdapter();
                int indexPos = -1; // if doesnt exist
                for (int i = 0; i < Invoice_numberArr.length; i++) {
                    if (positions.contains("article_numbers="+Article_numberArr[i]+", "+"sale_designations="+Sale_designationArr[i]+", "+"amounts="+AmountArr[i]+", "+"invoice_numbers=" + Invoice_numberArr[i]+", "+"prices="+PriceArr[i]+", "+"total_prices="+Total_priceArr[i])) {
                        indexPos = i;
                        break;
                    } else if (positions.contains("prices="+PriceArr[i]+", "+"article_numbers="+Article_numberArr[i]+", "+"amounts="+AmountArr[i]+", "+"sale_designations="+Sale_designationArr[i]+", "+"invoice_numbers=" + Invoice_numberArr[i]+", "+"total_prices="+Total_priceArr[i])){
                        indexPos = i;
                        break;
                    }
                }
                //some int to transfer it to position class
                int integerr = indexPos;

                Intent invoices_details_position = new Intent(InvoicesDetails.this, InvoicesDetailsPosition.class);
                invoices_details_position.putExtra("saleDesignationForPosition", Sale_designationArr[indexPos]);
                invoices_details_position.putExtra("articleNumberForPosition", Article_numberArr[indexPos]);
                invoices_details_position.putExtra("sequenceNumberForPosition", Sequence_numberArr[indexPos]);
                invoices_details_position.putExtra("amountForPosition", AmountArr[indexPos]);
                invoices_details_position.putExtra("warehouseForPosition", WarehouseArr[indexPos]);
                invoices_details_position.putExtra("containerDescriptionForPosition", Container_descriptionArr[indexPos]);
                invoices_details_position.putExtra("priceForPosition", PriceArr[indexPos]);
                invoices_details_position.putExtra("discountForPosition", DiscountArr[indexPos]);
                invoices_details_position.putExtra("totalPriceForPosition", Total_priceArr[indexPos]);

                startActivity(invoices_details_position);
            }
        });

因此,如果从Android 4.3下的ListView点击我的项目直到5.0 varible

    String positions = parent.getAdapter().getItem(position).toString();

告诉我

string " {prices=10.8, article_numbers=0736, amounts=1, sale_designations=Pfirsichlikör, invoice_numbers=1, total_prices=10.8} "

如果我点击Android版本5.1,它会显示我

string " {article_numbers=0736, sale_designations=Pfirsichlikör, amounts=1, invoice_numbers=1, prices=10.8, total_prices=10.8} "

因为我需要用它检查两次

for (int i = 0; i < Invoice_numberArr.length; i++) {
            if (positions.contains("article_numbers="+Article_numberArr[i]+", "+"sale_designations="+Sale_designationArr[i]+", "+"amounts="+AmountArr[i]+", "+"invoice_numbers=" + Invoice_numberArr[i]+", "+"prices="+PriceArr[i]+", "+"total_prices="+Total_priceArr[i])) {
                indexPos = i;
                break;
            } else if (positions.contains("prices="+PriceArr[i]+", "+"article_numbers="+Article_numberArr[i]+", "+"amounts="+AmountArr[i]+", "+"sale_designations="+Sale_designationArr[i]+", "+"invoice_numbers=" + Invoice_numberArr[i]+", "+"total_prices="+Total_priceArr[i])){
                indexPos = i;
                break;
            }
        }

所以我的问题现在为什么它会根据Android版本而改变? 求助。

1 个答案:

答案 0 :(得分:0)

感谢@Mike M. LinkedHashMap sHashMap s适用于所有Android