Fragment 中 ListView 的自定义适配器不起作用
Custom Adapter for ListView in Fragment not working
我一直在尝试在片段中填充 listView。我想手动填充它。
我已经解决了很多关于同一主题的其他 Whosebug 问题,但 none 对我有用。
这是我的片段文件中的 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_width="match_parent"
android:layout_height="170dp"
android:layout_margin="16dp"
android:background="@color/white"
android:minHeight="170dp">
<ImageView
android:id="@+id/image"
android:backgroundTint="@color/lighterGrey"
android:layout_width="100dp"
android:layout_height="match_parent"
android:src="@drawable/clown"
android:scaleType="fitXY"/>
<LinearLayout
android:id="@+id/text_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp"
android:gravity="center_vertical"
android:layout_toRightOf="@id/image">
<TextView
android:id="@+id/short_title_textview"
android:layout_width="match_parent"
android:layout_height="0dp"
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_width="match_parent"
android:layout_height="0dp"
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_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:layout_marginTop="4dp">
<TextView
android:id="@+id/genre"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:textAppearanceMedium"
android:textSize="16sp"
android:textColor="@android:color/black"
android:text="Genre: " />
<TextView
android:id="@+id/short_genre_textview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@id/genre"
android:textAppearance="?android:textAppearanceMedium"
android:textSize="16sp"
android:textColor="@android:color/black"
tools:text="Komedi" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp">
<ImageView
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
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_width="wrap_content"
android:layout_height="match_parent"
android:text="100"
android:paddingLeft="2dp"
android:textColor="@color/black" />
<TextView
android:layout_toLeftOf="@id/status_text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Status: "
android:textColor="@color/black"/>
<TextView
android:id="@+id/status_text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:text="Completed"
android:textColor="@color/black"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
这是我的片段布局。 fragment_stories.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_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/logo"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_alignParentLeft="true"
android:src="@drawable/clown"/>
<TextView
android:layout_toRightOf="@id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<com.google.android.material.textfield.TextInputLayout
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_edit_text"
android:inputType="text"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/lighterGrey"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search"
android:id="@+id/short_data_textview"/>
<ListView
android:id="@+id/short_story_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:orientation="vertical"/>
</LinearLayout>
</LinearLayout>
编辑:这是 HomeActivity 中函数 getShortStories() 的代码。
public ArrayList<ShortStories> getShortStoriesData(){
return shortStoryObject;
}
这里是崩溃的错误logcat:
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
没关系,我修好了。我特别笨
在适配器文件 StoryAdapter.java 中,我将 getAppCount() 中的文本设置为文本视图,而函数 returns 是一个整数而不是字符串。当我将它转换为字符串时它起作用了。
appCountTextView = convertView.findViewById(R.id.app_count_textview);
appCountTextView.setText("" + shortStories.getAppCount());
我一直在尝试在片段中填充 listView。我想手动填充它。
我已经解决了很多关于同一主题的其他 Whosebug 问题,但 none 对我有用。
这是我的片段文件中的 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_width="match_parent"
android:layout_height="170dp"
android:layout_margin="16dp"
android:background="@color/white"
android:minHeight="170dp">
<ImageView
android:id="@+id/image"
android:backgroundTint="@color/lighterGrey"
android:layout_width="100dp"
android:layout_height="match_parent"
android:src="@drawable/clown"
android:scaleType="fitXY"/>
<LinearLayout
android:id="@+id/text_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp"
android:gravity="center_vertical"
android:layout_toRightOf="@id/image">
<TextView
android:id="@+id/short_title_textview"
android:layout_width="match_parent"
android:layout_height="0dp"
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_width="match_parent"
android:layout_height="0dp"
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_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:layout_marginTop="4dp">
<TextView
android:id="@+id/genre"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:textAppearanceMedium"
android:textSize="16sp"
android:textColor="@android:color/black"
android:text="Genre: " />
<TextView
android:id="@+id/short_genre_textview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@id/genre"
android:textAppearance="?android:textAppearanceMedium"
android:textSize="16sp"
android:textColor="@android:color/black"
tools:text="Komedi" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp">
<ImageView
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
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_width="wrap_content"
android:layout_height="match_parent"
android:text="100"
android:paddingLeft="2dp"
android:textColor="@color/black" />
<TextView
android:layout_toLeftOf="@id/status_text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Status: "
android:textColor="@color/black"/>
<TextView
android:id="@+id/status_text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:text="Completed"
android:textColor="@color/black"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
这是我的片段布局。 fragment_stories.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_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/logo"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_alignParentLeft="true"
android:src="@drawable/clown"/>
<TextView
android:layout_toRightOf="@id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<com.google.android.material.textfield.TextInputLayout
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_edit_text"
android:inputType="text"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/lighterGrey"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search"
android:id="@+id/short_data_textview"/>
<ListView
android:id="@+id/short_story_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:orientation="vertical"/>
</LinearLayout>
</LinearLayout>
编辑:这是 HomeActivity 中函数 getShortStories() 的代码。
public ArrayList<ShortStories> getShortStoriesData(){
return shortStoryObject;
}
这里是崩溃的错误logcat:
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
没关系,我修好了。我特别笨
在适配器文件 StoryAdapter.java 中,我将 getAppCount() 中的文本设置为文本视图,而函数 returns 是一个整数而不是字符串。当我将它转换为字符串时它起作用了。
appCountTextView = convertView.findViewById(R.id.app_count_textview);
appCountTextView.setText("" + shortStories.getAppCount());