1

I have to get my data by running a query. So, I need a TextView that can add data dynamically.Here is my code to get and allocate data dynamically in a TextView

        do{
            int count = cursor.getCount();
            final TextView[] myTextViews = new TextView[count]; 

            for(int counter = 0; counter<count ; counter++){
                getName = cursor.getString(cursor.getColumnIndexOrThrow("NAME"));
                affiliation_ID = cursor.getString(cursor.getColumnIndexOrThrow("NUMBER"));
                final TextView rowTextView = new TextView(this);
                rowTextView.setText(Html.fromHtml(getName+"<sup><small>"+affiliation_ID+"</small></sup><br/>"));
                rel.addView(rowTextView);
                }          
        } while(cursor.moveToNext());

It gives me result like this.

enter image description here

As you can see One word overlapping with another words. Here is my xml file

<?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/ConTopic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/ConTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/ConTopic"
            android:layout_marginTop="22dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

       <RelativeLayout
            android:id="@+id/rel"
            android:layout_width="fill_parent"
            android:layout_height="match_parent" >
       </RelativeLayout>

    </RelativeLayout>

</ScrollView>

So, I need to know or my question is

1) If I want to show those text under ConTitle. What should I suppose to do ? 2) For case 1, How can I dynamically add desire position of a TextView and Size of a Text. 3)If I want to add another simple Texview(normal text) under dynamically added TextView.how can I do that ?

And the last thing, This is what I want achieve

enter image description here

4

1 回答 1

1

您可以使用附加方法。拥有单个文本视图并将文本视图添加到您的布局中。追加新数据并追加\n新行。

还有为什么你有一个for循环和一个while。一个while循环就足够了

 TextView tv = new TextView(ActivityName.this);
 rel.addView(tv);   
    if (cursor !=null && cursor.moveToFirst()) {
        do {
            getName = cursor.getString(cursor.getColumnIndexOrThrow("NAME"));
            affiliation_ID = cursor.getString(cursor.getColumnIndexOrThrow("NUMBER"));
            tv.append(Html.fromHtml(getName+"<sup><small>"+affiliation_ID+"</small></sup><br/>"));
            tv.append("\n"); 

        } while (cursor.moveToNext());
    }
于 2013-09-04T18:15:35.287 回答