Question

I am trying to create a 4 column listview using arrayAdapter, and add values manually in the 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);

                    }

My problem is i want to add list items dynamically from a two dimensional array How it is possible??? the item count have the same size of the string array?? ie; insted of

                          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");

i want to display

                            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]");

in a for loop also my doubut is how to create hashmap dynamically

No correct solution

OTHER TIPS

I didn't quite understand what you want exactly, but may you can try this:-

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);
}

Note:- Instead of FIRST_COLUMN, SECOND_COLUMN, you'll have to use 1,2,3.. as your keys for HashMap.

try some thing like this.

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);
}

In your code I no where see a list view creation. You need to create a list view in your xml and populate the data using an adapter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top