片段中 ListView 的自定义适配器不起作用

Posted

技术标签:

【中文标题】片段中 ListView 的自定义适配器不起作用【英文标题】:Custom Adapter for ListView in Fragment not working 【发布时间】:2021-07-01 11:01:40 【问题描述】:

我一直在尝试在片段中填充 listView。我希望它手动填充。

我已经解决了很多关于同一主题的其他 *** 问题,但没有一个对我有用。

这是我的片段文件中的 onCreateView StoriesFragment.java

    @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) 
    final View rootView = inflater.inflate(R.layout.fragment_stories, container, false);

    textView = rootView.findViewById(R.id.short_data_textview);
    search = rootView.findViewById(R.id.search_edit_text);

    listView = (ListView) rootView.findViewById(R.id.short_story_list_view);
    username_textview = rootView.findViewById(R.id.username_textview);

    setHomeActivity();
        try 
        setUsername();
     catch (JSONException e) 
        e.printStackTrace();
    

    shortStoryObject = homeActivity.getShortStoriesData();
    StoryAdapter adapter = new StoryAdapter(getActivity(), shortStoryObject);

    listView.setAdapter(adapter);


    return rootView;

这是我的适配器文件。 StoryAdapter.java

public class StoryAdapter extends ArrayAdapter<ShortStories> 

    private Context context;
    private ArrayList<ShortStories> shortStories;

    public StoryAdapter (Context mcontext, ArrayList<ShortStories> shortStories)
        super(mcontext, 0, shortStories);
        this.context = mcontext;
        this.shortStories = shortStories;
    


@NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) 

        // Check if the existing view is being reused, otherwise inflate the view
        if(convertView == null) 
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        

        TextView titleTextView, descriptionTextView, genreTextView, appCountTextView;

        ShortStories shortStories = getItem(position);


        titleTextView = convertView.findViewById(R.id.short_title_textview);
        titleTextView.setText(shortStories.getShortTitle());

        descriptionTextView = convertView.findViewById(R.id.short_description_textview);
        descriptionTextView.setText(shortStories.getShortDescription());

        genreTextView = convertView.findViewById(R.id.short_genre_textview);
        genreTextView.setText(shortStories.getShortGenre());

        appCountTextView = convertView.findViewById(R.id.app_count_textview);
        appCountTextView.setText(shortStories.getAppCount());


        return convertView;
    


这是我的列表视图文件。 list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_
        android:layout_
        android:layout_margin="16dp"
        android:background="@color/white"
        android:minHeight="170dp">

        <ImageView
            android:id="@+id/image"
            android:backgroundTint="@color/lighterGrey"
            android:layout_
            android:layout_
            android:src="@drawable/clown"
            android:scaleType="fitXY"/>

        <LinearLayout
            android:id="@+id/text_container"
            android:layout_
            android:layout_
            android:orientation="vertical"
            android:paddingLeft="16dp"
            android:gravity="center_vertical"
            android:layout_toRightOf="@id/image">

            <TextView
                android:id="@+id/short_title_textview"
                android:layout_
                android:layout_
                android:layout_weight="3"
                android:textAppearance="?android:textAppearanceMedium"
                android:layout_marginTop="4dp"
                android:textStyle="bold"
                android:textSize="18sp"
                android:layout_alignParentTop="true"
                android:textColor="@android:color/black"
                android:hint="Jack The Ripper ooga booga boogaa" />

            <TextView
                android:id="@+id/short_description_textview"
                android:layout_alignBottom="@id/short_title_textview"
                android:layout_
                android:layout_
                android:layout_weight="3"
                android:layout_marginTop="4dp"
                android:textAppearance="?android:textAppearanceMedium"
                android:textSize="16sp"
                android:textColor="@android:color/black"
                android:hint="Jack The Ripper is totally badass amirite or amirite."/>


            <RelativeLayout
                android:layout_
                android:layout_
                android:layout_weight="2"
                android:layout_marginTop="4dp">

                <TextView
                    android:id="@+id/genre"
                    android:layout_
                    android:layout_
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textSize="16sp"
                    android:textColor="@android:color/black"
                    android:text="Genre: " />

                <TextView
                    android:id="@+id/short_genre_textview"
                    android:layout_
                    android:layout_
                    android:layout_toRightOf="@id/genre"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textSize="16sp"
                    android:textColor="@android:color/black"
                    tools:text="Komedi" />


            </RelativeLayout>


            <RelativeLayout
                android:layout_
                android:layout_
                android:layout_weight="2"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp">
                <ImageView
                    android:layout_centerVertical="true"
                    android:layout_
                    android:layout_
                    android:id="@+id/app_count_image"
                    android:src="@drawable/ic_baseline_thumb_up_24"/>
                <TextView
                    android:id="@+id/app_count_textview"
                    android:layout_toRightOf="@id/app_count_image"
                    android:layout_
                    android:layout_
                    android:text="100"
                    android:paddingLeft="2dp"
                    android:textColor="@color/black" />

                <TextView
                    android:layout_toLeftOf="@id/status_text_view"
                    android:layout_
                    android:layout_
                    android:text="Status: "
                    android:textColor="@color/black"/>
                <TextView
                    android:id="@+id/status_text_view"
                    android:layout_
                    android:layout_
                    android:layout_alignParentRight="true"
                    android:text="Completed"
                    android:textColor="@color/black"/>

            </RelativeLayout>

        </LinearLayout>
</RelativeLayout>

这是我的片段布局。片段故事.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical">


        <RelativeLayout
            android:layout_marginTop="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="0dp"
            android:layout_
            android:layout_>

            <ImageView
                android:id="@+id/logo"
                android:layout_
                android:layout_
                android:layout_alignParentLeft="true"
                android:src="@drawable/clown"/>

            <TextView
                android:layout_toRightOf="@id/logo"
                android:layout_
                android:layout_
                android:text="Search"
                android:paddingLeft="16dp"
                android:textSize="18sp"
                android:textStyle="bold"
                android:textColor="@color/black"
                android:layout_centerVertical="true"/>

            <TextView
                android:id="@+id/username_textview"
                android:layout_
                android:layout_
                android:textSize="18sp"
                android:textStyle="bold"
                android:textColor="@color/black"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_
            android:layout_>

            <com.google.android.material.textfield.TextInputLayout
                android:layout_alignParentTop="true"
                android:layout_
                android:layout_
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp"
                android:layout_marginTop="8dp"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                app:startIconDrawable="@drawable/ic_baseline_search_24"
                android:hint="Search">

                <com.google.android.material.textfield.TextInputEditText
                    android:layout_
                    android:layout_
                    android:id="@+id/search_edit_text"
                    android:inputType="text"
                    android:maxLines="1" />
            </com.google.android.material.textfield.TextInputLayout>

            <View
                android:layout_
                android:layout_
                android:background="@color/lighterGrey"
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>

        <TextView
            android:layout_
            android:layout_
            android:text="Search"
            android:id="@+id/short_data_textview"/>

        <ListView
            android:id="@+id/short_story_list_view"
            android:layout_
            android:layout_
            android:drawSelectorOnTop="true"
            android:orientation="vertical"/>

    </LinearLayout>

</LinearLayout>

编辑:这是 HomeActivity 中函数 getShortStories() 的代码。

    public ArrayList<ShortStories> getShortStoriesData()
    return shortStoryObject;

这是崩溃的错误日志:

2021-04-06 12:09:17.442 29043-29043/? E/oid.air_storie: Unknown bits set in runtime_flags: 0x28000
2021-04-06 12:09:17.516 29043-29076/com.example.android.air_stories E/Perf: Fail to get file list com.example.android.air_stories
2021-04-06 12:09:17.516 29043-29076/com.example.android.air_stories E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2021-04-06 12:09:17.516 29043-29076/com.example.android.air_stories E/Perf: Fail to get file list com.example.android.air_stories
2021-04-06 12:09:17.516 29043-29076/com.example.android.air_stories E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2021-04-06 12:09:17.518 29043-29076/com.example.android.air_stories E/Perf: Fail to get file list oat
2021-04-06 12:09:17.518 29043-29076/com.example.android.air_stories E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array

【问题讨论】:

那么你得到了什么错误?是否有任何异常或只显示一个空列表? 请添加此功能的代码getShortStoriesData() @MayurGajra,应用程序在 listvew.setAdapter(adapter); 时崩溃;运行。如果我评论它,它会起作用,但是列表视图当然不会显示在应用程序中。 @IvanWooll 我添加了 getShortStoriesData() 的代码。 HomeActivity 只是在我将它调用到片段中的任何位置返回该对象。 可能shortStoryObject 为空 【参考方案1】:

没关系,我修好了。我太笨了。

在适配器文件 StoryAdapter.java 中,我将文本从 getAppCount() 设置为 textview,而函数返回一个整数而不是字符串。当我将它转换为字符串时它起作用了。

        appCountTextView = convertView.findViewById(R.id.app_count_textview);
    appCountTextView.setText("" + shortStories.getAppCount());

【讨论】:

以上是关于片段中 ListView 的自定义适配器不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Onclicklistener 在片段列表视图中不起作用

如何从 ListView 中提取对象 - getItemAtPosition 不起作用

getView() 在我的自定义列表视图中不起作用

片段内的自定义列表不起作用

片段中ListView的android自定义适配器

ListView 和自定义适配器在 Kotlin 中不起作用