【问题标题】:How to load Images in the list view according to Text name如何根据文本名称在列表视图中加载图像
【发布时间】: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
  • 尝试添加一个逻辑来动态地为标题找到合适的图像。这可能吗?
  • 发送模块名数组

标签: android arrays


【解决方案1】:

您可以为此目的使用 HashMap。我假设文本和图像的数量是相等的。您将文本作为键,图像名称作为值。

HashMap<String, Integer> imageData = new HashMap<String, Integer>();
imageData.put("Accounts", Integer.valueOf(R.drawable.btn_0010_accounts));

构造函数将改为:

private HashMap<String, Integer> imageData;
public MenuListAdapter(Context context, List<String> modules, HashMap<String, Integer> imageData) {
this.context = context;
this.mtitle = modules;
this.imageData = imageData;

}

getView()方法中,你可以将图片作为

@Override
public View getView(int position, View arg1, ViewGroup parent) {
  ........
  String textKey = mtitle.get(position);
  Integer imageResource = imageData.get(textKey);
  icon.setImageResource(imageResource.intValue());
  .........
}

【讨论】:

    【解决方案2】:

    通常你应该做的是将图像上传到与数据库中同名的服务器,这样你就可以根据imageLoader.setImageresource(url+textview.getText.toString()+".png",imageview,options...);这样的名称进行匹配

    在您的情况下,请尝试以下步骤 1)更改类似于数据库中公司名称的可绘制名称,例如 R.drawable.companyname1 ,并保留一个字符串数组,这些名称具有相同的位置,例如 int array.String array={"R.drawable.companyname1",...等等}; 2) 在getview方法中

     holder.text.setTag(position);
      holder.text.setText( tempValues.getCompanyName() );
         if(holder.text.getText().toString().equalsIgnorecase(array[Integer.parseInt(textview.GetTag().toString())){
         holder.image.setImageResource(                             array[Integer.parseInt(textview.GetTag().toString()));
        }
    

    【讨论】:

    • 嘿,我很抱歉第一次我用错误的类上传问题。现在我编辑了它。请检查 this.my bad.sorry.
    【解决方案3】:

    您可以使用Map&lt;Integer, ObjectPojo&gt;(ObjectPojo 可以包含您需要的任何属性)而不是List。可以根据需要修改getItem, getCount, getView等方法。

    图片 - icon.setImageResource(map.get(integerId));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 2012-07-25
      相关资源
      最近更新 更多