【发布时间】:2016-12-28 05:02:27
【问题描述】:
所以我正在创建一个自定义适配器,其中每一行都有一个ImageView 和一个TextView。 TextView 工作正常,但我在使用 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>
感谢您的帮助
【问题讨论】:
-
使用 TypedArray 试试这个developer.android.com/reference/android/content/res/…
标签: android listview custom-adapter