For this case is much better to use a ListView in my opinion, create a custom Adapter for your need where you populate and inflate your custom view for each item. You need a view consisting of and textview for example and inflate it for each position. populate a ArrayList with your data from the file and use it for your adapter. Example below
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textView"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textColor="@color/black"/>
</LinearLayout>
I am using ArrayList for all my adapters and create a class with getter and setter functions for my needs. Example below
public class DataSet {
private String data;
public DataSet(String data){
this.data = data;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
Now you need to create an adapter for your ListView. Example Below.
public class LetsMakeAnAdapter extends ArrayAdapter<DataSet> {
private ArrayList<DataSet> strings;
private Activity activity;
private static LayoutInflater inflater = null;
public LetsMakeAnAdapter(Context context, ArrayList<DataSet> item) {
super(context,R.layout.list_element, item);
this.strings = item;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View customViewToInflate = convertView;
if(customViewToInflate == null){
inflater = LayoutInflater.from(getContext());
customViewToInflate = inflater.inflate(R.layout.list_element, null);
}
TextView textViewExample = (TextView) customViewToInflate.findViewById(R.id.textView);
textViewExample.setText(strings.get(position).getData());
return customViewToInflate;
}
}
Now simply populate in your activity a ArrayList with your data from the json file and set the adapter to ListView. Example below.
DataSet objectOne = new DataSet("Value1");
DataSet objectTwo = new DataSet("Value2");
ArrayList<DataSet> dataSetValues = new ArrayList<>();
dataSetValues.add(objectOne);
dataSetValues.add(objectTwo);
LetsMakeAnAdapter adapter = new LetsMakeAnAdapter(getActivity(),dataSetValues);
listView.setAdapter(adapter);