2016-03-22 59 views
0

我爲HashMap製作了自定義適配器,問題是我無法使用HashMap設置CustomAdapter的TextView。 倒數第二行得到語法錯誤獲取HashMap的位置。 請引導我如何解決語法錯誤並設置CustomAdapter。無法在CustomAdapter中獲取HashMap位置

CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<HashMap<String, Object>> { 

     private SparseBooleanArray mSelectedItemsIds; 
     private LayoutInflater inflater; 
     private Context mContext; 
     private List<HashMap<String, Object>> list; 
     Blocklist blocklist; 

     public CustomAdapter (Context context, int resourceId, List<HashMap<String, Object>> list) { 
      super(context, resourceId, list); 
      mSelectedItemsIds = new SparseBooleanArray(); 
      mContext = context; 
      inflater = LayoutInflater.from(mContext); 
      this.list = list; 
     } 

     private static class ViewHolder { 
      TextView itemName; 
     } 

     public View getView(int position, View view, ViewGroup parent) { 
      final ViewHolder holder; 
      if (view == null) { 
       holder = new ViewHolder(); 
       view = inflater.inflate(R.layout.custom_textview, null); 
       view = inflater.inflate(R.layout.custom_textview, null); 
       holder.itemName = (TextView) view.findViewById(R.id.custom_tv); 
       view.setTag(holder); 
      } else { 
       holder = (ViewHolder) view.getTag(); 
      } 

      holder.itemName.setText(list.get(position)); 
      return view; 
     } 

Blocklist.java

HashMap<String,Object> hm = new HashMap<String,Object>(); 
       hm.put(ID, cursor.getLong(0)); 
       hm.put(ORIGINATING_ADDRESS, cursor.getString(1)); 
       hm.put(MESSAGE_BODY, cursor.getString(2)); 
       arrayList.add(hm); 
       cursor.moveToNext(); 
+0

添加錯誤日誌。 –

+0

爲什麼2倍在getView中膨脹。 –

+0

錯誤地輸入@jaydroider –

回答

2

您有:

holder.itemName.setText(list.get(position)); 

上面的行不會爲list.get(position)導致HashMap對象的工作,而不是一個字符串對象。如果你想往返爲例,說明了新的訊息,您必須使用這樣

holder.itemName.setText((String)list.get(position).get(MESSAGE_BODY)); 

你也不必誇大

view = inflater.inflate(R.layout.custom_textview, null); 

twice`

+0

感謝它的工作 –

0
list.get(position) 

返回對象HashMapposition,而不是String