2012-09-14 64 views
1

美好的一天,希望標題不會令人困惑,我可以在這裏清楚。我有一個自定義ArrayAdapter與TextViews和ImageViews。現在我想讓其中一個TextView由HashMap中的第2列作爲Keys的值填充。結構就是這樣的..使用散列圖填充已經膨脹的佈局的文本視圖

此搜索店鋪文本2

圖像2輛汽車文本2

圖像3樓文本2

圖像4船文本2

在這個例子中,第一和第二列有已經在ArrayAdapter中填充了數組。所以我想要text2,填充其中column2是鍵的值的HashMap。一個例子是,如果有30家商店,我們將有

此搜索店鋪30

代碼:

Resources rsc = getActivity().getResources(); 
     String[] options = rsc.getStringArray(R.array.item_title); 
     TypedArray icons = rsc.obtainTypedArray(R.array.item_title_color); 
    HashMap<String,String> map = new HashMap<String,String>(); 

    MyAdapter adapter = new MyAdapter(getActivity(), R.layout.listview_custom_item, options, icons, map); 
     list.setAdapter(adapter); 

MyAdapter類:

class MyAccountsAdapter extends ArrayAdapter<Object> { 
     private LayoutInflater mInflater; 
     private int ViewResourceId; 
     private String[] mstring; 
     TypedArray icons; 
     HashMap<String, String> infomap = new HashMap<String,String>(); 

    public MyAdapter(Context context, int textViewResourceId, String[] string, TypedArray icons, HashMap<String, String> map) { 
      super(context, textViewResourceId, string); 

     mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       mstring = string; 
       this.icons = icons; 
       ViewResourceId = textViewResourceId; 
       infomap.putAll(map); //another hashmap here 
     } 

    @Override 
     public int getCount(){ 
      return mstring.length; 
     } 

      @Override 
     public View getView(int position, View convertView, ViewGroup parent){ 
      convertView = mInflater.inflate(ViewResourceId, null); 

      ImageView imageview = (ImageView)convertView.findViewById(R.id.list_image_id); 
      TextView text_title = (TextView)convertView.findViewById(R.id.list_text_id); 
      TextView text_info = (TextView)convertView.findViewById(R.id.list_textinfo_id); 

      imageview.setImageDrawable(icons.getDrawable(position)); 
      text_title.setText(mstring[position]); 
      Log.d(TAG, "strings are" + text_title.getText().toString()); 

// so how do i populate text_info here to correspond with text_title; 

      return convertView; 
     } 

什麼想法?還是有更好的方式來做到這一點,就像把它們合併成一個一樣?任何幫助都是非常感謝的。謝謝

+0

如果在地圖上的鍵是什麼是使用'text_info.setText(infomap.get阻止你的第一個'TextView'值(mstring [位置]))'在'getView'方法中? – Luksprog

回答

0

試試這個下面的代碼:

text_info.setText(infomap.get(mstring[position])); 
+0

很抱歉,很遲纔回復。但顯然當我這樣做時,它返回空值。 – irobotxxx