![](/img/trans.png)
[英]Populate a ListView from an ArrayList<HashMap<String, String>>
[英]Android Spreadsheet data into ArrayList HashMap populate ListView
我有一個公開的Google電子表格,我正在從ArrayList中提取數據並將其加載到ArrayList <HashMap <String,String>>
然后我嘗試將其填充到自定義ListView中。
基本上,我的電子表格只有3列“時間戳”,“ question1”和“ question2”。 我假設哈希圖的鍵都應該相同,只是值會改變?
我正在使用具有三個TextViews的自定義xml布局的自定義ArrayAdapter。 在我的getView()中,我一直試圖遍歷HashMap並填充listview。 我似乎無法使它正常工作。 當我跑步時,它只會顯示最后一個單元格。
這是我用來從電子表格中提取各個行並將其放入HashMap的代碼:
// Print entries
for(ListEntry row : feed.getEntries())
{
//Print the first column's cell value
System.out.println("new row");
for(String i : row.getCustomElements().getTags())
{
System.out.println(" "+i + ": " + row.getCustomElements().getValue(i));
rowMap.put(row.getCustomElements().getValue("question1"),
row.getCustomElements().getValue(i));
pulledData2.add( (HashMap<String, String>) rowMap );
}
}
列表適配器getView()
private class ListAdapter extends ArrayAdapter<HashMap<String, String>> {
private ArrayList<HashMap<String, String>> mItems = pulledData2;
public ListAdapter(DashboardActivity dashboardActivity, ArrayList<HashMap<String, String>> items, LayoutInflater inflater){
super(dashboardActivity, -1, items);
this.mItems = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
//inflate the custom view
if (convertView == null){
LayoutInflater inflater = (LayoutInflater)
DashboardActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_each_item, parent, false);
}
((TextView)convertView.findViewById(R.id.textViewTimeStamp))
.setText( mItems.get(position).get("question1"));
所以我的問題是我沒有在每次提取一行時實例化一個新的hashMap。 我添加了一個while循環,現在我得到了不重要的HashMaps
while( i <= pulledData.size()) {
HashMap<String, String> item = new HashMap<String, String>();
item.put(tag, row.getCustomElements().getValue(tag));
//pulledData.add(entry.getCustomElements().getValue(tag));
pulledData2.add(i, item);
i++;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.