0

我想从数据库中显示一个表,其中包含 5 列具有不同字符串长度的表。这是显示它的最佳方式。谢谢我提前。以前我尝试使用 listview,但各种字符串长度显示行不正确。

我的 layout.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">           

     <TextView android:id="@+id/text1"      
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 

     <TextView android:id="@+id/text3"          
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_alignParentRight="true"/>

     <TextView android:id="@+id/text5"          
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_alignParentRight="true"/>

     <TextView android:id="@+id/text7"          
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_alignParentRight="true"/>

     <TextView android:id="@+id/text9"          
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_alignParentRight="true"/>

     <TextView
        android:id="@+id/textView21"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/> 

</LinearLayout>

爪哇代码:

super.onCreate(savedInstanceState);
setContentView(R.layout.report);            
String[] from;
int[] to;
db.open();          
ListView lv=(ListView)findViewById(R.id.listView1);     
Cursor c = db.getEducationTitle();

while(c. moveToNext())
{
    // Cursor c = db.getEducationTitle();
    LinearLayout linearlayout=(LinearLayout)findViewById(R.id.Layout1);
    TextView edt=(TextView) new TextView(getApplicationContext());
    edt.setText(c.getString(c.getColumnIndex("col1"))+" "+c.getString(c.getColumnIndex("col2"))+" "+c.getString(c.getColumnIndex("col3"))+c.getString(c.getColumnIndex("col4"))+" "+c.getString(c.getColumnIndex("col5")));
    linearlayout.addView(edt);

}
4

1 回答 1

0

尝试这个....

 SQLiteDatabase db=openOrCreateDatabase("databasename", MODE_PRIVATE, null);
    Cursor c=db.rawQuery("SELECT * FROM tablename, null);
    c.moveToFirst();
    LinearLayout linearlayout=(LinearLayout)findViewById(R.id.linearlayout);

       while(cursor.moveToNext)
        {

                TextView edt=(TextView) new TextView(getApplicationContext());
                edt.setText(c.getString(c.getColumnIndex("col1"))+" "+c.getString(c.getColumnIndex("col2"))+" "+c.getString(c.getColumnIndex("col3"))+" "+c.getString(c.getColumnIndex("col4"))+" "+c.getString(c.getColumnIndex("col5")));
                linearlayout.addView(edt);

        }
于 2012-07-12T05:33:29.963 回答