如何为列表视图创建自定义适配器?获取 ResourceNotFoundException
Posted
技术标签:
【中文标题】如何为列表视图创建自定义适配器?获取 ResourceNotFoundException【英文标题】:How to create a custom adapter for a listview? Getting RessourceNotFoundException 【发布时间】:2015-06-27 01:37:39 【问题描述】:我正在尝试为我的列表视图制作一个自定义适配器,但我一直在同一行收到 RessourceNotFoundException。
首先,这是我的适配器类代码:
public class CreateProgramAdapter extends BaseAdapter
private final LayoutInflater inflater;
private ArrayList<TrainingPass> trainingPasses;
private List<TrainingPass> items;
private int layoutResourceId;
private Context context;
public CreateProgramAdapter(Context context, List<TrainingPass> items)
this.context = context;
this.items = items;
inflater = LayoutInflater.from(this.context);
@Override
public int getCount()
return items.size();
@Override
public Object getItem(int position)
return items.get(position);
@Override
public long getItemId(int position)
return 0;
@Override
public View getView(int position, View convertView, ViewGroup parent)
View row = convertView;
TrainingPassHolder holder;
if(convertView == null)
convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, null);
holder = new TrainingPassHolder();
convertView.setTag(holder);
else
holder = (TrainingPassHolder) convertView.getTag();
holder.trainingPass = this.items.get(position);
holder.trainingPassRelativeLayout = (RelativeLayout) row.findViewById(R.id.trainingpass_item_relativelayout);
holder.headlineTxt = (TextView) row.findViewById(R.id.trainingpass_headline);
holder.setNoTxt = (TextView) row.findViewById(R.id.txt_trainingpass_setno);
holder.showHideIcon = (ImageView) row.findViewById(R.id.trainingpass_showhide_imageview);
holder.exerciseSearchField = (EditText) row.findViewById(R.id.trainingpass_exercise_searchfield);
holder.repsTextField = (EditText) row.findViewById(R.id.trainingpass_reps_txtfield);
holder.addSetButton = (Button) row.findViewById(R.id.add_set_button);
holder.addExerciseButton = (Button) row.findViewById(R.id.trainingpass_addexercise_button);
holder.setsListView = (ListView) row.findViewById(R.id.trainingpass_sets_listview);
holder.exercisesListView = (ListView) row.findViewById(R.id.trainingpass_exercises_listview);
holder.trainingPassRelativeLayout.setTag(holder.trainingPass);
holder.headlineTxt.setTag(holder.trainingPass);
holder.setNoTxt.setTag(holder.trainingPass);
holder.showHideIcon.setTag(holder.trainingPass);
holder.exerciseSearchField.setTag(holder.trainingPass);
holder.repsTextField.setTag(holder.trainingPass);
holder.addSetButton.setTag(holder.trainingPass);
holder.addExerciseButton.setTag(holder.trainingPass);
holder.setsListView.setTag(holder.trainingPass);
holder.exercisesListView.setTag(holder.trainingPass);
setupItems(holder);
return convertView;
private void setupItems(TrainingPassHolder trainingPassHolder)
// TODO: set default values for items
public static class TrainingPassHolder
TrainingPass trainingPass;
RelativeLayout trainingPassRelativeLayout;
TextView headlineTxt;
TextView setNoTxt;
ImageView showHideIcon;
EditText exerciseSearchField;
EditText repsTextField;
Button addSetButton;
Button addExerciseButton;
ListView setsListView;
ListView exercisesListView;
这是视图的 xml,包含列表视图:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_
android:layout_ android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.mobileplatformexam.activities.CreateProgramActivity"
android:id="@+id/relativeLayoutCreateProgram">
<Button
android:layout_
android:layout_
android:text="Add training pass"
android:id="@+id/btn_add_trainingpass"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/btn_add_trainingpass_help"
android:layout_toStartOf="@+id/btn_add_trainingpass_help"
android:onClick="addTrainingPass"/>
<Button
android:layout_
android:layout_
android:text="\?"
android:id="@+id/btn_add_trainingpass_help"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="addTrainingPassHelp"/>
<Button
android:layout_
android:layout_
android:text="Create program"
android:id="@+id/btn_create_program"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/btn_add_trainingpass_help"
android:layout_alignEnd="@+id/btn_add_trainingpass_help"
android:onClick="createProgram"/>
<ListView
android:layout_
android:layout_
android:id="@+id/listView_trainingpasses"
android:layout_above="@+id/btn_create_program"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/btn_add_trainingpass" />
</RelativeLayout>
以及上面列表视图的项目布局的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_>
<RelativeLayout
android:id="@+id/trainingpass_item_relativelayout"
android:layout_
android:layout_
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/trainingpass_headline"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:text="Trainingpass"
android:textSize="26dp"
android:textColor="@android:color/white"
android:background="@android:color/holo_blue_dark"
android:
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_toLeftOf="@+id/trainingpass_showhide_imageview"
android:layout_toStartOf="@+id/trainingpass_showhide_imageview"
android:textIsSelectable="true"
android:textStyle="bold" />
<ImageView
android:id="@+id/trainingpass_showhide_imageview"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="@drawable/abc_spinner_mtrl_am_alpha"
android:layout_alignParentTop="true"
android:background="@android:color/holo_blue_dark"
android:minWidth="50dp"
android:minHeight="50dp"
android:layout_alignBottom="@+id/trainingpass_headline" />
<EditText
android:id="@+id/trainingpass_exercise_searchfield"
android:layout_
android:layout_
android:layout_below="@+id/trainingpass_headline"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/trainingpass_showhide_imageview"
android:layout_alignEnd="@+id/trainingpass_showhide_imageview"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="Name of exercise"
android:padding="15dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/txt_trainingpass_setno"
android:layout_
android:layout_
android:text="Set 1"
android:textSize="20dp"
android:textColor="@android:color/black"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:layout_below="@+id/trainingpass_exercise_searchfield"
android:layout_toLeftOf="@+id/trainingpass_reps_txtfield"
android:layout_toStartOf="@+id/trainingpass_reps_txtfield" />
<EditText
android:id="@+id/trainingpass_reps_txtfield"
android:layout_
android:layout_
android:hint="Reps"
android:padding="15dp"
android:
android:layout_alignBottom="@+id/txt_trainingpass_setno"
android:layout_toLeftOf="@+id/add_set_button"
android:layout_toStartOf="@+id/add_set_button"
android:layout_marginRight="10dp" />
<Button
android:layout_
android:layout_
android:id="@+id/add_set_button"
android:text="Add set"
android:layout_alignBottom="@+id/trainingpass_reps_txtfield"
android:layout_alignRight="@+id/trainingpass_exercise_searchfield"
android:layout_alignEnd="@+id/trainingpass_exercise_searchfield"
android:layout_below="@+id/trainingpass_exercise_searchfield" />
<ListView
android:layout_
android:layout_
android:id="@+id/trainingpass_sets_listview"
android:layout_alignLeft="@+id/trainingpass_exercise_searchfield"
android:layout_alignStart="@+id/trainingpass_exercise_searchfield"
android:layout_below="@+id/txt_trainingpass_setno" />
<Button
android:text="Add exercise"
android:layout_
android:layout_
android:id="@+id/trainingpass_addexercise_button"
android:layout_below="@+id/trainingpass_sets_listview"
android:layout_alignRight="@+id/add_set_button"
android:layout_alignEnd="@+id/add_set_button"
android:layout_alignLeft="@+id/trainingpass_sets_listview"
android:layout_alignStart="@+id/trainingpass_sets_listview" />
<ListView
android:layout_
android:layout_
android:id="@+id/trainingpass_exercises_listview"
android:layout_below="@+id/trainingpass_addexercise_button"
android:layout_alignLeft="@+id/trainingpass_addexercise_button"
android:layout_alignStart="@+id/trainingpass_addexercise_button" />
</RelativeLayout>
</LinearLayout>
这是在我的活动中设置适配器的代码(扩展活动)
ArrayList<TrainingPass> trainingPasses = new ArrayList<TrainingPass>();
TrainingPass trainingPass = new TrainingPass();
trainingPass.setTrainingPassName("wefwef");
TrainingPass trainingPass2 = new TrainingPass();
trainingPass.setTrainingPassName("rwefhtr");
trainingPasses.add(trainingPass);
trainingPasses.add(trainingPass2);
ListView trainingPassesListView = (ListView)findViewById(R.id.listView_trainingpasses);
CreateProgramAdapter createProgramAdapter = new CreateProgramAdapter(CreateProgramActivity.this, trainingPasses);
trainingPassesListView.setAdapter(createProgramAdapter);
我得到的错误是:
12661-12661/com.mobileplatformexam E/AndroidRuntime: FATAL EXCEPTION: main android.content.res.Resources$NotFoundException:资源 ID #0x7f04001a 在 android.content.res.Resources.getValue(Resources.java:1019) 在 android.content.res.Resources.loadXmlResourceParser(Resources.java:2107) 在 android.content.res.Resources.getLayout(Resources.java:858) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:394) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:352) 在 com.mobileplatformexam.Adapters.CreateProgramAdapter.getView(CreateProgramAdapter.java:56) 在 android.widget.AbsListView.obtainView(AbsListView.java:2466) 在 android.widget.ListView.measureHeightOfChildren(ListView.java:1244) 在 android.widget.ListView.onMeasure(ListView.java:1155) 在 android.view.View.measure(View.java:12807) 在 android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:642) 在 android.widget.RelativeLayout.onMeasure(RelativeLayout.java:424) 在 android.view.View.measure(View.java:12807) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 在 android.view.View.measure(View.java:12807) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 在 android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453) 在 android.view.View.measure(View.java:12807) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 在 android.view.View.measure(View.java:12807) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1399) 在 android.widget.LinearLayout.measureVertical(LinearLayout.java:690) 在 android.widget.LinearLayout.onMeasure(LinearLayout.java:583) 在 android.view.View.measure(View.java:12807) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 在 com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2218) 在 android.view.View.measure(View.java:12807) 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1068) 在 android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2452) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:137) 在 android.app.ActivityThread.main(ActivityThread.java:4476) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583) 在 dalvik.system.NativeStart.main(Native Method)
错误是指我的适配器类中的第 56 行,即以下行:
convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, null);
我似乎真的无法找出代码可能有什么问题,所以我希望有人能帮助我。
谢谢
【问题讨论】:
将convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, null);
更改为convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, parent , false);
谢谢,但不幸的是我仍然得到完全相同的错误。
“adapter_createprogram_trainingpasses_item.xml”在哪个目录下?
adapter_createprogram_trainingpasses_item.xml 当前与其余布局 xml 文件位于同一目录中。
最后一次尝试。到您的项目布局并尝试通过删除所有 textCorlor/bgColor ... 来简化它,甚至删除布局子项(TextView Buttons listView ??)只需保留在 FreamLyout
【参考方案1】:
感谢 JafarKhQ,我解决了这个问题。 通过将布局“adapter_createprogram_trainingpasses_item .xml”更改为以下内容,它起作用了:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_
android:layout_
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/trainingpass_headline"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:text="Trainingpass"
android:textSize="20dp"
android:textColor="@android:color/white"
android:background="@android:color/holo_blue_dark"
android:
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_toLeftOf="@+id/trainingpass_showhide_imageview"
android:layout_toStartOf="@+id/trainingpass_showhide_imageview"
android:textIsSelectable="true"
android:textStyle="bold" />
<ImageView
android:id="@+id/trainingpass_showhide_imageview"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="@drawable/abc_spinner_mtrl_am_alpha"
android:layout_alignParentTop="true"
android:background="@android:color/holo_blue_dark"
android:
android:minWidth="50dp"
android:layout_alignBottom="@+id/trainingpass_headline" />
<EditText
android:id="@+id/trainingpass_exercise_searchfield"
android:layout_
android:layout_
android:layout_below="@+id/trainingpass_headline"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/trainingpass_showhide_imageview"
android:layout_alignEnd="@+id/trainingpass_showhide_imageview"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="Name of exercise"
android:padding="15dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/txt_trainingpass_setno"
android:layout_
android:layout_
android:text="Set 1"
android:textSize="20dp"
android:textColor="@android:color/black"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:layout_below="@+id/trainingpass_exercise_searchfield"
android:layout_toLeftOf="@+id/trainingpass_reps_txtfield"
android:layout_toStartOf="@+id/trainingpass_reps_txtfield" />
<EditText
android:id="@+id/trainingpass_reps_txtfield"
android:layout_
android:layout_
android:hint="Reps"
android:padding="15dp"
android:
android:layout_alignBottom="@+id/txt_trainingpass_setno"
android:layout_toLeftOf="@+id/add_set_button"
android:layout_toStartOf="@+id/add_set_button"
android:layout_marginRight="10dp" />
<Button
android:layout_
android:layout_
android:id="@+id/add_set_button"
android:text="Add set"
android:layout_alignBottom="@+id/trainingpass_reps_txtfield"
android:layout_alignRight="@+id/trainingpass_exercise_searchfield"
android:layout_alignEnd="@+id/trainingpass_exercise_searchfield"
android:layout_below="@+id/trainingpass_exercise_searchfield" />
<ListView
android:layout_
android:layout_
android:id="@+id/trainingpass_sets_listview"
android:layout_alignLeft="@+id/trainingpass_exercise_searchfield"
android:layout_alignStart="@+id/trainingpass_exercise_searchfield"
android:layout_below="@+id/txt_trainingpass_setno" />
<Button
android:text="Add exercise"
android:layout_
android:layout_
android:id="@+id/trainingpass_addexercise_button"
android:layout_below="@+id/trainingpass_sets_listview"
android:layout_alignRight="@+id/add_set_button"
android:layout_alignEnd="@+id/add_set_button"
android:layout_alignLeft="@+id/trainingpass_sets_listview"
android:layout_alignStart="@+id/trainingpass_sets_listview" />
<ListView
android:layout_
android:layout_
android:id="@+id/trainingpass_exercises_listview"
android:layout_below="@+id/trainingpass_addexercise_button"
android:layout_alignLeft="@+id/trainingpass_addexercise_button"
android:layout_alignStart="@+id/trainingpass_addexercise_button" />
</RelativeLayout>
【讨论】:
以上是关于如何为列表视图创建自定义适配器?获取 ResourceNotFoundException的主要内容,如果未能解决你的问题,请参考以下文章
如何为随机自定义 ListView 适配器设置按钮单击事件?