1

in my application i have simple layout to filled by any data with class structure. after add items into that i want to add this layout into scroll view. adding layout to scrollview is successfull but i can not set data to custom layout. for example this is my custom layout to set data :

tile.xml:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="@drawable/drop_shadow">
    </LinearLayout>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="30dp" android:minHeight="100dp" android:background="#eee">
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="New Text"
                android:id="@+id/username"/>
    </LinearLayout>
</LinearLayout>

Class data structure:

public class SubjectStructure {
    public String    username;
    public String    topic;
    public String    description;
    public String    avatar;
    public String    sender;
    public Integer   grade;
    public Integer   type;
    public String    date;
}

now i want to fill tile.xml and add that into this below layout:

fragment_home.xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#F1F1F1">
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:id="@+id/scrollView1">
    </LinearLayout>
</ScrollView>

my code is this : public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate( R.layout.fragment_home, container, false);
    scrollView = (LinearLayout) rootView.findViewById ( R.id.scrollView1 );

    for(SubjectStructure SS: G.subject_items){
        LinearLayout newLL = new LinearLayout( G.context );
        TextView newTV = new TextView( G.context );
        newTV.setText ( SS.topic );
        newLL.addView(newTV);

        LayoutInflater in =  (LayoutInflater) G.context.getSystemService(G.context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.tile, null);
        scrollView.addView(view);
    }
    return rootView;
}

in this code i can add tile.xml to fragment_home.xml . but i can not setText SS.topic to tile.xml. SS.topic is not null and have data. how to resolve this problem?

2 Answers 2

1

what you are missing for sure is

 scrollView.addView(newLL); 

to each iteration. About

LayoutInflater in =  (LayoutInflater) G.context.getSystemService(G.context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.tile, null);
 scrollView.addView(view);

I am not sure its purpose. You create a new instance of tile at each iteration, but you do nothing on its content. On the minor side, you have already a LayoutInflater. You don't need to ask for one to the system at each iteration, use the one you get as parameter.

Sign up to request clarification or add additional context in comments.

Comments

0

Change the code as follows:

for(SubjectStructure SS: G.subject_items){
    LayoutInflater in =  (LayoutInflater) G.context.getSystemService(G.context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.tile, null);

    TextView textUserName=(TextView) view.findViewById(R.id.username);
    textUserName.setText(SS.topic);

    scrollView.addView(view);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.