【发布时间】:2015-03-31 05:34:35
【问题描述】:
我有一个列表视图和 BaseAdapter 来添加一个图像和一个文本到行。我的问题是如何根据文本名称在列表视图中加载图像。我知道我是否根据文本数组将图像添加到数组 i可以做到这一点。但是我的文本数组是从服务调用中加载的。每次文本数组顺序更改时,我的图像加载顺序都不正确..所以任何人都可以给我指出完成此任务的方法。谢谢
BaseAdapter 类
public class MenuListAdapter extends BaseAdapter {
private Context context;
private List<String> mtitle;
private int[] micon;
private LayoutInflater inflater;
Typeface tf;
public MenuListAdapter(Context context, List<String> modules, int[] icon) {
this.context = context;
this.mtitle = modules;
this.micon = icon;
}
@Override
public int getCount() {
return mtitle.size();
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View arg1, ViewGroup parent) {
TextView title;
ImageView icon;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.nav_drawer_list_item, parent,
false);
title = (TextView) itemView.findViewById(R.id.title);
icon = (ImageView) itemView.findViewById(R.id.icon);
Typeface type = Typeface.createFromAsset(context.getAssets(),"fonts/font.ttf");
title.setText(mtitle.get(position));
title.setTypeface(type);
icon.setImageResource(micon[position]);
return itemView;
}
}
图像数组
private int[] photo;
photo = new int[]{R.drawable.btn_0011_home,R.drawable.btn_0010_accounts, R.drawable.btn_0009_contacts, R.drawable.btn_0008_opportunities,
R.drawable.btn_0007_leads,R.drawable.btn_0006_calendar,R.drawable.btn_0005_documents,R.drawable.btn_0004_emails,R.drawable.btn_0003_campaigns, R.drawable.btn_0002_calls, R.drawable.btn_0001_meeting,
R.drawable.btn_0000_tasks, R.drawable.ic_launcher}
MenuListAdapter menuAdapter = new MenuListAdapter(
getApplicationContext(), moduleName, photo);
【问题讨论】:
-
检查 moduleName , photo both r 等于
-
我如何检查..我将图像添加为 R.drawable.btn_001
-
尝试添加一个逻辑来动态地为标题找到合适的图像。这可能吗?
-
发送模块名数组