当前位置: 首页 > 知识库问答 >
问题:

如何为列表视图创建自定义适配器?正在获取RessourceNotFoundException

夏骞尧
2023-03-14

我正在尝试为我的列表视图制作一个自定义适配器,但我在同一行上不断得到一个 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,包含listview:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" 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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/trainingpass_item_relativelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal">

        <TextView
            android:id="@+id/trainingpass_headline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            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:height="50dp"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Reps"
            android:padding="15dp"
            android:width="160dp"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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﹕ 致命html" target="_blank">异常:主android.content.res.Resources$NotFoundException:android.view.LayoutInflationer.inflation上android.coontent.res.Resources.getValue(Resources.java:1019)上android.res.Resources.loadXmlResourceParser(Resources.java.2107)上androx.content/res.Resources.getLayout(Resources.java:858)上andriod.view.Layoutinflationer.inflate(LayoutInflationer.java:394)上的资源ID#0x7f04001a位于android.widget.AbsListView.AbsListView.getView(AbsListView.java:2466),位于android.vidget.ListView.measureHeightOfChildren(ListView.java:11244),位于android.widgetandroid.widget.RelativeLayout.measureChildHorizontal(RelativeLayut.java:642)位于android.wodget.Relative Layout.onMeasure(RelativeLayout.java:424)位于android.view.view.measure(view.java:12807android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)位于android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlaylay.java:453)位于andry.view.view.measure(view.java:12807)位于android.wiew.ViewGroup。measureChildWithMargin(ViewGroup.java:4698android.widget.LinearLayout.measureChildWithMargins(ViewGroup.java:4698)位于android.wodget.Linear Layout.measure ChildBeforeLayout(LinearLayout.java:1399)位于andrio.widget.LinearLayout.measureVertical(LinearLayout.java:690)位于andreo.widget.RinearLayout.onMeasure(Linear Layout.java:583)位于androl.view.view.measure(view.java:12807)位于android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2218)位于android.view.view.measure(view.java:12807)位于androm.view.Vindow.view.view.measure.performTraversals(ViewRootImpl.java:1068)位于androl.view.VViewRootImpl.handleMessage(ViewRootImpl.java:2452)位于android.app.ActivityThread.main(ActivityThread.java:4476)上的android.os.Looper.loop(Looper.java:137)位于java.lang.reflect.Method.invokeNative(Native Method)位于java.lang.reflect.Method.inoke(Method.java:511)位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(Zygotenit.java:816)位于com.android.internal.os.ZygateInit.main(ZygeteInit.java:583)位于dalvik.system.NativeStart.main(本机方法)

该错误指的是我的adapterclass中的第56行,该行如下:

   convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, null);

我似乎真的无法找出代码可能出了什么问题,所以我希望有人可以帮助我。

谢谢。

共有1个答案

宓文斌
2023-03-14

多亏了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_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/trainingpass_headline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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:height="30dp"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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:height="30dp"
        android:minWidth="50dp"
        android:layout_alignBottom="@+id/trainingpass_headline" />

    <EditText
        android:id="@+id/trainingpass_exercise_searchfield"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Reps"
        android:padding="15dp"
        android:width="160dp"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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>
 类似资料:
  • 问题内容: 嗨,您想创建一个自定义光标适配器,以便显示2行文本的图像。我在理解自定义光标适配器时遇到了一些麻烦,但是我不明白如何添加要从数据库路径填充的imageview。 问题答案: 将, 实际上,我已经实现了与您正在寻找的东西非常相似的东西。这是我的实现。 XML只是万一… 根据特定条件,这将在文本的两行显示最多2幅图像。 希望这可以为您的工作打下基础! 祝好运 :]

  • 我在android应用程序上使用自定义适配器查看列表。我不知道如何设置项目单击的侦听器。我的应用程序使用Kotlin。 这是我的适配器布局。 有人可以帮助我吗?

  • 本文向大家介绍Android 创建自定义视图,包括了Android 创建自定义视图的使用技巧和注意事项,需要的朋友参考一下 示例 如果需要完全自定义的视图,则需要子类View(所有Android视图的超类),并提供自定义的sizing(onMeasure(...))和drawing(onDraw(...))方法: 创建您的自定义视图框架:每个自定义视图的基本相同。在这里,我们为自定义视图创建框架,

  • 我正在寻找在Android中使用列表适配器和Recyview适配器的区别。关于性能的任何不同,使用它们的利弊。

  • 我再次发布这个,试图使问题/问题更清楚: 到目前为止,代码填充了ArrayList项目,创建了一个适配器并将项目添加到其中,然后在ListView lv中显示它。到目前为止,一切都很好。 但是,getShoppingList()方法会运行多次,更新ArrayList项。而不是每次发生这种情况时都重新创建适配器,我只想更新它并在ListView lv中显示更新的内容。 我想说的是: “更新”按钮的O

  • 我有一个带有几个IBOutlet的自定义单元格类。我已经将类添加到故事板。我已经连接了所有的出口。我的cellForRowAtIndexPath函数如下所示: 这是我的自定义单元格类: 当我运行应用程序时,我的所有手机都是空的。我已注销,它包含所有正确的字符串。我还尝试了将一个实际字符串与标签相等,这会产生相同的结果。 我错过了什么?感谢任何帮助。