美好的一天,希望标题不会令人困惑,我可以在这里说清楚。我有一个带有 TextViews 和 ImageViews 的自定义 ArrayAdapter。现在我希望其中一个 TextView 由 HashMap 中的值填充,其中第二列作为键。结构是这样的。。
image1 店铺 text2
image2 汽车 text2
image3 房子 text2
image4 船 text2
在此示例中,第 1 列和第 2 列已在 ArrayAdapter 中填充了数组。所以我希望 text2 填充一个 HashMap 值,其中 column2 是键。一个例子是,如果有 30 家商店,我们将有
image1 店铺 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);
我的适配器类:
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;
}
有任何想法吗?还是有更好的方法来做到这一点,比如将它们全部合二为一?...任何帮助都非常感谢。谢谢