-2

I have collection of HashMap<String,Object> data as ArrayList.

I used following method to add HashMap data to ArrayList.

mylist = new ArrayList<HashMap<String, Object>>();
for (Data dt : data) {

    HashMap<String, Object> map = new HashMap<String, Object>();                
    map.put("id", dt.getWeekNo());
    map.put("description", dt.getDesc());
    map.put("Amount", dt.getAmount());
    mylist.add(map);
}

Data is a separate class which is used to Getters and Setters purpose only.

Now my output is like:

3 | Income  | 1000
5 | Income  | 1500
3 | Expense |  250
5 | Expense |  500

I want it as the following to my listview.

3 | Income : 1000 | Expense : 250
5 | Income : 1500 | Expense : 500

Any idea to achieve this. I dont know how to achieve and I dont know any idea too so I dont know where to start.

Updates : This is the way I am adapt my list to listview with getview() method

ListAdapter adapter
    = new SimpleAdapter(this, mylist, R.layout.txnlist,
                        new String[] {"id", "description", "Amount" },
                        new int[] { R.id.txntext1, R.id.txntext2, R.id.txntext3 }) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.txnlist, null);
        }
        Collection<Object> str = mylist.get(position).values();
        ArrayList<Object> al1 = new ArrayList<Object>(str);
        TextView amnt = (TextView) v.findViewById(R.id.txntext3);
        if (al1.get(2).toString().equals("Income")) 
            amnt.setTextColor(Color.GREEN);
        if (al1.get(2).toString().equals("Expense"))
            amnt.setTextColor(Color.RED);
        return super.getView(position, v, parent);
    }
};
ListView lv1 = (ListView) findViewById(R.id.repListView);

Help me friends.

Thanks in advance.

Marco Forberg
  • 2,634
  • 5
  • 22
  • 33
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128

2 Answers2

1

wow, I haven't got the slightest clue why you would do the variables to hold the data like that, but in any case, based on what I understood, you want something like this:

for (int i=0; i<(myList.getCount()/2); i++){
    Log.v("Line " + i, myList.getItem(i).getWeekNo() + "|" + 
        myList.getItem(i).getDesc() + ":" + myList.getItem(i).getAmount() + "|" + 
        myList.getItem(mylist.getCount()/2 + i).getDesc() + ":" + 
            mylist.getItem(myList.getCount()/2 + i).getAmount();
}

Let me know.

LuckyMe
  • 3,820
  • 2
  • 27
  • 35
0

You have to create an Adapter and override the method "getView(...)". Inside of getView you have to inflate your layout for each item of the listview and you have to assign your values.

Here an example http://www.ezzylearning.com/tutorial.aspx?tid=1763429

Bae
  • 892
  • 3
  • 12
  • 26