如何在android中将硬编码数据添加到hashmap?

时间:2014-09-15 14:18:50

标签: android listview hashmap android-arrayadapter

您好我想在hashmap中添加一组硬编码值。在该hashmap中,将从Web服务添加5值,我想手动添加一个数据。如果我这样做,最后两个值在listview中显示相同的名称和详细信息。我刚接触到android,所以如果有人知道的话,请帮助我。

我尝试了以下代码:

for (int i = 0; i < length; i++) {
                        if (i != 5) {
                            JSONObject c = tsubaki_products.getJSONObject(i);
                            // url_index++;

                            // Storing each json item in variable
                            String name = c.getString(PRODUCT_NAME);
                            String image = c.getString(PRODUCT_TAB_IMAGE
                                    .replace(" ", "%20"));
                            System.out.println("PRODUCT_NAME-->>" + name);
                            String description = c
                                    .getString(PRODUCT_DESCRIPTION);
                            String short_name = c.getString(PRODUCT_SHORT_NAME);
                            short_url = c.getString(PRODUCT_SHORT_URL);

                            // result_url = "," + short_url;
                            /*
                             * category_urls = new
                             * ArrayList<String>(tsubaki_products.length());
                             * 
                             * //description ="";
                             * 
                             * category_urls.add(short_url);
                             * System.out.println("Category"+category_urls);
                             */
                            // array_j=new JSONArray(short_url);
                            // StringBuilder sb = new StringBuilder();
                            /*
                             * for(int j =0;j<short_url.size();i++) { String
                             * prefix = ""; for (String str : short_url) {
                             * sb.append(prefix); prefix = ",";
                             * sb.append(str.toString()); } }
                             */
                            // System.out.println("Short232"+category_urls);
                            // category_urls = new ArrayList<HashMap<String,
                            // String>>();
                            /*
                             * category_urls = new ArrayList<String>();
                             * category_urls.add(short_url);
                             */
                            // category_urls.add(category_map);
                            // category_urls =
                            /*
                             * StringBuffer string_buf = new
                             * StringBuffer(short_url);
                             * string_buf.append(short_url+",");
                             * System.out.println("Short232"+string_buf);
                             */
                            // .replaceAll("<(.|\n)*?>","")
                            // creating new HashMap
                            map = new HashMap<String, String>();
                            // adding each child node to HashMap key => value
                            map.put(PRODUCT_NAME, name);
                            if (i == 4) {
                                map.put(PRODUCT_NAME,
                                        getString(R.string.slide_head_text6));
                                map.put(PRODUCT_TAB_IMAGE, "cable");
                            } else {
                                map.put(PRODUCT_TAB_IMAGE, image);
                            }

                            map.put(PRODUCT_SHORT_URL, short_url);
                            map.put(PRODUCT_SHORT_NAME, short_name);

                            if (i == 4) { // System.out.println("sfdasgf"+"4");
                                map.put(PRODUCT_NAME,
                                        getString(R.string.slide_head_text6));
                            }
                            // System.out.println("Description" + description);
                            if (description == "null" || description == null) {
                                if (i == 0) {
                                    map.put(PRODUCT_DESCRIPTION,
                                            getString(R.string.slide_text1));
                                }
                                if (i == 2) {
                                    map.put(PRODUCT_DESCRIPTION,
                                            getString(R.string.slide_text3));
                                }
                                if (i == 3) {
                                    map.put(PRODUCT_DESCRIPTION,
                                            getString(R.string.slide_text4));
                                }
                                if (i == 4) {
                                    map.put(PRODUCT_DESCRIPTION,
                                            getString(R.string.slide_text5));
                                }

                                // Do any stuff
                            } else {
                                if (i == 1) {
                                    map.put(PRODUCT_DESCRIPTION,
                                            getString(R.string.slide_text2));
                                }
                                if (i == 4) {
                                    map.put(PRODUCT_DESCRIPTION,
                                            getString(R.string.slide_text5));
                                }
                                if (i == 0) {
                                    map.put(PRODUCT_DESCRIPTION,
                                            getString(R.string.slide_text1));
                                }
                                if (i == 2) {
                                    map.put(PRODUCT_DESCRIPTION,
                                            getString(R.string.slide_text3));
                                }
                                if (i == 3) {
                                    map.put(PRODUCT_DESCRIPTION,
                                            getString(R.string.slide_text4));
                                }/*
                                 * if(i == 5) { map.put(PRODUCT_DESCRIPTION,
                                 * getString(R.string.slide_text6)); }
                                 */
                                // description =
                                // c.getString(PRODUCT_DESCRIPTION).replaceAll("<(.|\n)*?>","");
                                // map.put(PRODUCT_DESCRIPTION, description);
                            }
                            // adding HashList to ArrayList

                             productList.add(map);
                        } /*else if (i == 5) {

                            map.put(PRODUCT_NAME,
                                    getString(R.string.slide_head_text7));
                            map.put(PRODUCT_TAB_IMAGE, "clutches");
                            map.put(PRODUCT_DESCRIPTION,
                                    getString(R.string.slide_text6));                           
                            // productList.add(map);

                        }*/                                                 

                        // }
                        // productList.add(map);
                        // System.out.println("List" +productList);
                    }

0 个答案:

没有答案