【问题标题】:Custom listview adapter -Cannot figure out how to set the image resource自定义列表视图适配器 - 无法弄清楚如何设置图像资源
【发布时间】:2016-12-28 05:02:27
【问题描述】:

所以我正在创建一个自定义适配器,其中每一行都有一个ImageView 和一个TextViewTextView 工作正常,但我在使用 ImageView 时遇到问题。我在 xml 文件中有一个类型化的图像资源数组。当我尝试在适配器类中设置图像资源时,我得到:

setImageResource 不能应用于 android.content.res.typedarray

我该如何解决这个问题?我一直在试图解决这个问题。

这是我的适配器类

public class CustomAdapter extends BaseAdapter{
CharSequence [] result;
Context context;
TypedArray[] imageId;
MyDBHandler dbHandler;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, CharSequence[] routes, TypedArray[] routeNum) {

    //dbHandler = new MyDBHandler(null, null, null, 1);
    // TODO Auto-generated constructor stub
    result=routes;
    context=mainActivity;
    imageId=routeNum;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView routeText;
    ImageView routeImg;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View customView;
    customView = inflater.inflate(R.layout.custom_row, null);
    holder.routeText=(TextView) customView.findViewById(R.id.routeText);
    holder.routeImg=(ImageView) customView.findViewById(R.id.routeImage);
    holder.routeText.setText(result[position]);
    holder.routeImg.setImageResource(imageId[position]);
     return customView;
}

我的主要活动

//fill list view with xml array of routes
    final CharSequence[] routeListViewItems = getResources().getTextArray(R.array.routeList);
    //fills route detail image view with xml array of images
    final TypedArray image = getResources().obtainTypedArray(R.array.routeImages);


    ListView routeListView;
    routeListView=(ListView) findViewById(R.id.routeListView);
    routeListView.setAdapter(new CustomAdapter(this,routeListViewItems, image);

以及我的自定义行的 xml 布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:id="@+id/routeImage"
    android:layout_gravity="center_vertical"/>
<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="51dp"
    android:id="@+id/routeText"
    android:layout_weight="0.62"
    android:textSize="18sp"
    android:gravity="center_vertical"
    android:textColor="@android:color/black"
    android:typeface="normal"
     />

<ImageView
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:id="@+id/checkImageView"
    android:layout_gravity="center_vertical"/>

</LinearLayout>

感谢您的帮助

【问题讨论】:

标签: android listview custom-adapter


【解决方案1】:

试试这个,

holder.routeImg.setImageResource(imageId.getDrawable(position));

【讨论】:

  • .setImageResource(imageMain.getResourceId(position, -1));
  • 你为什么使用 typedarray,有什么具体原因吗?我建议将所有图像添加到列表中,然后您可以像之前一样继续
  • 如果有帮助请关注此链接stackoverflow.com/a/20398556/5722385
  • 谢谢,如果我能正常工作,我会查看该链接并报告。我有一个包含大约 200 个图像的列表,它们在 XML 文件中比在代码中添加它们更容易管理。
【解决方案2】:

使用下面的代码从 TypeArray 中设置 Image -

holder.routeImg.setImageResource(imageId.getResourceId(i, defaultValue));

【讨论】:

    【解决方案3】:

    您的代码示例不一致,但您不应将任何[]TypedArray 一起使用。您只有一个 TypedArray,而不是 TypedArray 的数组。

    给定一个TypedArray,拉胡尔的回答应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2015-12-21
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多