-3

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

Sibin Francis
  • 581
  • 2
  • 12
  • 30

3 Answers3

1

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.

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • HI, R.J thanx for your reply, my exact problem is i have a 2D String array, i want to add these items from the string array to listview.. – Sibin Francis Feb 21 '13 at 11:13
  • I think you had problem in creating the `HashMap` dynamically right? My post will solve that and you can use that `List` to populate your `ListView`. – Rahul Feb 21 '13 at 11:14
  • if this is my array of strings, String[][] mDialogStrings= new String[10][10]={{"a","b","c"},{"d","e","f"},{"g","h","i"}} how to add these items to the listview – Sibin Francis Feb 21 '13 at 11:18
  • why you people downvote my question???????? iam new in android development..kindly please support me.... – Sibin Francis Feb 21 '13 at 11:26
  • check out these links [1](http://stackoverflow.com/questions/5070830/populating-a-listview-using-arraylist), [2](http://stackoverflow.com/questions/2394176/android-populating-a-listview-with-array-items), [3](http://stackoverflow.com/questions/7151590/populate-listview-from-liststring). – Rahul Feb 21 '13 at 11:27
1

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);
}
Raj
  • 1,843
  • 2
  • 21
  • 47
  • Hi Raj, also itried this code before posting this question..please understand my exact problem.. my requirment is , not toadd only one hashmap named temp1, i want to create hashmap with the same size of array dynamically – Sibin Francis Feb 21 '13 at 11:23
  • HI Raj, i got the solution..thanx for your support, and please please upvote my questionn – Sibin Francis Feb 23 '13 at 07:43
1

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

Ajit Pratap Singh
  • 1,299
  • 12
  • 24
  • i already created a 4 column custom listview and its working fine but problem is to add itms dynamically from a 2D string array, if you can solve this problem , please post your reply – Sibin Francis Feb 21 '13 at 11:32
  • The general idea is that if you need to add list items at runtime you update the data set in the adapter and call notifydatasetchanged on the adapter. This will inform the list view and get view will get called for all the items including the newly added items. – Ajit Pratap Singh Feb 21 '13 at 11:39