1

Recently I asked a question on how to have one style for a TextView and display it multiple times for other texts as well. The solution was, to create an XML-Layout where I design the textview and then use an ArrayAdapter to fill it with contents. I'm using the ArrayAdapter in a fragment because I have multiple fragments that replace the main fragment dependent on the menu click.

But I'm stuck. I don't really know how to achieve that. I'm writing all my values into an array and then I'm assigning them to my ArrayAdapter. I have seen plenty solutions, but none of them fixed my problem and they all used an other way.

This is the xml of the fragment

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

<ScrollView 
    android:id="@+id/mainScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.03" >

            <TextView
        android:id="@+id/sources"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
    </TextView>
    </ScrollView>
</RelativeLayout>

The TextView is the textview I want to populate. There is no style yet, I'm doing it just for test purposes. Now I did this in the Fragment.

This is my onCreateView method

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        StrictMode.setThreadPolicy(policy);
        View rootView = inflater.inflate(R.layout.main_fragment, null);
        RelativeLayout ll = new RelativeLayout(getActivity());
        ScrollView sv = new ScrollView(getActivity());
        ArrayAdapter<String> sourceAdapter = new ArrayAdapter<String>(getActivity(), R.id.mainScrollView, R.id.sources, sourceArr);
        return rootView;        
    }

sourceArr is the array with the contents. How exactly do I assign all values to the TextView so it gets displayed multiple times? Thanks

Musterknabe
  • 5,763
  • 14
  • 61
  • 117

1 Answers1

1

You are having textview in scrollview. Either loop through the array and append the data to textview.

Or

Just Have a listview in main_fragment.xml initialize listview and adapter and set the adapter to listview.

  View rootView = inflater.inflate(R.layout.main_fragment, null);
  ListView lv = (ListView) rootView.findViewById(R.id.lv);
  ArrayAdapter<String> sourceAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item1, sourceArr);
  lv.setAdapter(sourceAdapter);
  return rootView;

Also remove ScrollView sv = new ScrollView(getActivity()) and RelativeLayout ll = new RelativeLayout(getActivity()).

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • The looping is exactly what I do not want to do, because I have much data to display and the app takes 2-3 seconds to load when chaning the fragments. I'm trying your second approach now. – Musterknabe Oct 15 '13 at 15:03
  • 1
    @Musterknabe second option is better. Also you can have custom adapter and customize the view the way you want – Raghunandan Oct 15 '13 at 15:04
  • Just a quick question. What exactly is this for `android.R.layout.simple_list_item1` ? Ah ok, understood. ;) Thank you really much. It's working now. What is your recommended way when I want to the same layout with multliple different TextView styles and also some imageviews ? – Musterknabe Oct 15 '13 at 15:05
  • 1
    @Musterknabe use a custom adapter. Inflate a custom layout with textview and imageview. Set the custom adapter to listview and if it helps accept and upvote the answer – Raghunandan Oct 15 '13 at 15:08