2013-05-07 41 views
-2

我收集的HashMap<String,Object>數據爲ArrayListArrayList數據到ListView

我使用以下方法將HashMap數據添加到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是用於僅GettersSetters目的的單獨的類。

現在我的輸出是這樣的:

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

我想把它當作下到我的列表視圖。

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

任何想法來實現這一點。我不知道如何實現,我也不知道任何想法,所以我不知道從哪裏開始。

更新: 這是我適應我的名單與getview()方法

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

幫助我的朋友ListView控件的方式。

在此先感謝。

+0

創建自定義adpter並覆蓋getView() – 2013-05-07 06:58:01

+0

您的代碼在哪裏,您將在其中添加數據到listView。在這裏也加上 – AAnkit 2013-05-07 06:59:01

+0

@NiravRanpara我已經過分了。我不知道如何將一週的「收入」和「費用」放在一起。 – Gunaseelan 2013-05-07 07:00:54

回答

1

哇,我沒有絲毫的線索,爲什麼你會做的變量來保存這樣的數據,但在任何情況下,根據我的理解,你想是這樣這個:

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

讓我知道。