【问题标题】:adding data to a listview from string array从字符串数组将数据添加到列表视图
【发布时间】:2013-02-06 16:12:52
【问题描述】:

我正在尝试使用 arrayAdapter 创建一个 4 列列表视图,并在 populateList() 中手动添加值

                      private void populateList()
                         {

              list = new ArrayList<HashMap<String,String>>();

        HashMap<String,String> temp1 = new HashMap<String,String>();
            temp1.put(FIRST_COLUMN,"Diaries");
            temp1.put(SECOND_COLUMN, "Products");
            temp1.put(THIRD_COLUMN, "Rs. 400");
            temp1.put(FOURTH_COLUMN, "ggg Unit");
        list.add(temp1);

        HashMap<String,String> temp2 = new HashMap<String,String>();
            temp2.put(FIRST_COLUMN,"Note Books");
            temp2.put(SECOND_COLUMN, "Products");
            temp2.put(THIRD_COLUMN, "Rs. 600");
            temp2.put(FOURTH_COLUMN, "hhh Unit");
        list.add(temp2);

                    }

我的问题是我想从二维数组中动态添加列表项 怎么可能???项目计数与字符串数组的大小相同?? IE;插入

                          HashMap<String,String> temp1 = new HashMap<String,String>();

            temp1.put(FIRST_COLUMN,"Diaries");
            temp1.put(SECOND_COLUMN, "Products");
            temp1.put(THIRD_COLUMN, "Rs. 400");
            temp1.put(FOURTH_COLUMN, "ggg Unit");

我要显示

                            temp1.put(FIRST_COLUMN,"myArry[i][j]");
            temp1.put(SECOND_COLUMN, "myArry[i][j]");
            temp1.put(THIRD_COLUMN, "myArry[i][j]");
            temp1.put(FOURTH_COLUMN, "myArry[i][j]");

在 for 循环中 我的问题也是如何动态创建哈希图

【问题讨论】:

    标签: android android-listview arrays dynamic-list


    【解决方案1】:

    在您的代码中,我看不到列表视图的创建。您需要在 xml 中创建一个列表视图并使用适配器填充数据

    【讨论】:

    • 我已经创建了一个 4 列自定义列表视图,它工作正常,但问题是从 2D 字符串数组动态添加它,如果你能解决这个问题,请发表你的回复
    • 总体思路是,如果需要在运行时添加列表项,则更新适配器中的数据集并在适配器上调用 notifydatasetchanged。这将通知列表视图,并为所有项目调用获取视图,包括新添加的项目。
    【解决方案2】:

    尝试这样的事情。

    HashMap<String,String> temp1 = null;
    for (int i = 0; i < myArry.length; i++) {
    temp1 = new HashMap<String, String>();
    temp1.put(FIRST_COLUMN, myArry[i][0]);
    temp1.put(SECOND_COLUMN, myArry[i][1]);
    temp1.put(THIRD_COLUMN, myArry[i][2]);
    temp1.put(FOURTH_COLUMN, myArry[i][3]);
    list.add(temp1);
    }
    

    【讨论】:

    • 嗨 Raj,在发布此问题之前也尝试了此代码..请理解我的确切问题..我的要求是,不要只添加一个名为 temp1 的哈希图,我想创建具有相同大小的哈希图动态数组
    • 您好 Raj,我得到了解决方案..thanx 为您的支持,请为我的问题投票
    【解决方案3】:

    我不太明白你到底想要什么,但你可以试试这个:-

    List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
    HashMap<String,String> temp = null;
    
    for(int i=0;i<2;i++){
        temp = new HashMap<String,String>();
        for(int j=0;j<4;j++){
        temp.put(String.valueOf(j), myArr[i][j]);
        }
    list.add(temp);
    }
    

    注意:-您必须使用1,2,3.. 作为HashMap 的密钥,而不是FIRST_COLUMNSECOND_COLUMN

    【讨论】:

    • 嗨,R.Jthanx 为您的回复,我的确切问题是我有一个 2D 字符串数组,我想将这些项目从字符串数组添加到 listview..
    • 我认为您在动态创建HashMap 时遇到了问题,对吧?我的帖子会解决这个问题,您可以使用 List 填充您的 ListView
    • 如果这是我的字符串数组,String[][] mDialogStrings= new String[10][10]={{"a","b","c"},{"d ","e","f"},{"g","h","i"}} 如何将这些项目添加到列表视图
    • 为什么你们不赞成我的问题????????我是android开发的新手..请支持我....
    • 查看这些链接123
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多