0

I have an app that talks to a service which returns a JSON with a X number of strings.

I want each of these strings to have their own Edit Text or Text View inside a ScrollView, but have no clue on how to do this.

Could someone show me the way of it? I don't want the code done, just how to achieve it.

Guigs
  • 25
  • 6

3 Answers3

0

Instead of using ScrollView, you should use ListView with ArrayAdapter or more efficiently RecyclerView. Some tutorials how to use this widgets:

Hope this will help!

Macimi
  • 1
  • 1
0

Basically in order to have a dynamic scroll view, it's recommended to utilize a RecyclerView like what @Macimi has said. To further provide you a thorough walkthrough on that, here's the code to use:

<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"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView
        android:id="@+id/main_RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:fastScrollAutoHide="true"
        app:fastScrollPopupBgColor="@color/colorAccent"
        app:fastScrollPopupTextSize="56sp"
        app:fastScrollPopupBackgroundSize="88dp"
        app:fastScrollPopupTextColor="@android:color/primary_text_dark"
        app:fastScrollThumbColor="@color/colorAccent" />

</RelativeLayout>

This chunk of code uses timusus' recyclerview that integrate a fastscroller within.

However, if you're not interested in integrating a fastscroller, simple change the widget to this:

<android.support.v7.widget.RecyclerView
        android:id="@+id/main_RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

Finally, here are a few links to further strengthen the reasons to use a RecyclerView:

Community
  • 1
  • 1
Nicholas
  • 1,678
  • 13
  • 31
  • Thanks a lot! For now I don't see why to use the RecyclerView. Decided to create my own CustomAdapter and use the ListView – Guigs Dec 06 '16 at 16:11
0

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);