I have been trying to populate a listView in a fragment. I want it populated manually.
I have gone through so many other stackoverflow questions on the same topic but none of them have worked for me.
Here is my onCreateView in my fragment file 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;
}
Here is my adapter file. 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;
}
}
Here is my listview file. 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>
Here is my fragment layout. 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>
Edit: Here is the code for function getShortStories() from HomeActivity.
public ArrayList<ShortStories> getShortStoriesData(){
return shortStoryObject;
}
Here is an error logcat of the crash:
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
Never mind, I fixed it. I'm extremely dumb.
In the adapter file, StoryAdapter.java, I was setting the text from getAppCount() to a textview, while the function returns an integer and not a string. It worked when I converted it to string.
appCountTextView = convertView.findViewById(R.id.app_count_textview);
appCountTextView.setText("" + shortStories.getAppCount());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.