以动态创建的布局显示数据

时间:2014-01-29 15:00:16

标签: android layout dynamic

Hy Guy's!

我的应用应该在多个时间戳显示路线。 我的问题是我的程序没有创建任何布局。但我认为现在有问题。当然我的sql语句是真的,我在scrollview中创建了一个布局,在scrollview中创建了多个布局。

现在这是我的功能:

public void getRoute() {

        SQLiteDatabase db = myDbHelper.getReadableDatabase();
        LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View insertPoint = findViewById(R.id.layout);
        View v = inflater.inflate(R.layout.layoutausgabe,null);

        txtAbfahrt = (TextView)findViewById(R.id.txtAbfahrt);
        txtAnkunft = (TextView)findViewById(R.id.txtAbfahrt);
        txtDauer = (TextView)findViewById(R.id.txtAbfahrt);
        txtRoute = (TextView)findViewById(R.id.txtAbfahrt);
        txtUmstiege = (TextView)findViewById(R.id.txtAbfahrt);

        db = myDbHelper.getReadableDatabase();
        // Alle Daten der Datenbank abrufen mithilfe eines Cursors
        Cursor cursor = db.rawQuery("SELECT strftime('%H:%M', f.abfahrt) AS Abfahrt, strftime('%H:%M', f.ankunft) AS Ankunft, " +
                " strftime('%H:%M', strftime('%s',f.ankunft)- strftime('%s',f.abfahrt), 'unixepoch') AS Dauer, r.name AS Route, " +
                " count(u.fahrt_id) AS Umstiege FROM scotty_fahrt f " +
                " JOIN scotty_haltestelle start ON f.start_id = start.id " +
                " JOIN scotty_haltestelle ziel ON f.ziel_id = ziel.id " +
                " JOIN scotty_route r ON f.route_id = r.id" +
                " LEFT OUTER JOIN  scotty_umstiegsstelle u ON f.id = u.fahrt_id " +
                " WHERE start.name = 'HTL Neufelden'" +
                " AND ziel.name = 'Eferding'" +
                " GROUP BY u.fahrt_id",null);

        cursor.moveToFirst();

        while (cursor.moveToNext()){
            //in this string we get the record for each row from the column "name"
            int i =0;
            String abfahrtszeit = cursor.getString(0);
            String ankuftszeit = cursor.getString(1);
            String dauer = cursor.getString(2);
            String route = cursor.getString(3);
            String umstiege = cursor.getString(4);

            ((ViewGroup) insertPoint).addView(v, i, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
            txtAbfahrt.setText(abfahrtszeit);
            txtAnkunft.setText(ankuftszeit);
            txtDauer.setText(dauer);
            txtRoute.setText(route);
            txtUmstiege.setText(umstiege);
            i++;
        }

        //here we close the cursor because we do not longer need it
        //}
        cursor.close();
        myDbHelper.close();
    }

这是我应该动态插入的布局代码:

?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Abfahrt - Ankunft |"
        android:id="@+id/textView"
        android:textColor="@color/black"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text=" Dauer |"
        android:id="@+id/textView2"
        android:textColor="@color/black"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text=" Umstieg |"
        android:id="@+id/textView2"
        android:textColor="@color/black"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text=" Route"
        android:id="@+id/textView2"
        android:textColor="@color/black"/>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

View v = inflater.inflate(R.layout.layoutausgabe,null);

您只需将此视图实例化一次,并尝试在每个循环中添加相同的视图。您应该在每个循环中创建一个新视图。

while (cursor.moveToNext()){
    View v = inflater.inflate(R.layout.layoutausgabe,null);
    v.findViewById(...)
    // ...
    // ...
    ((ViewGroup) insertPoint).addView(v, i, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

您还尝试在两个维度中添加带有FILL_PARENT的视图,但正如我在您的布局中看到的,所有孩子都有WRAP_CONTENT。

答案 1 :(得分:0)

像这样使用兄弟:

This is your main layout where you want to inflate:
LinearLayout layout=(LinearLayout)findViewById(R.id.mainlinearlayout);

LayoutInflater inflater= (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflating xml which you want to inflate
View view=inflater.inflate(R.layout.inflatingxml,null);

Textview txt = (TextView)view.findviewById(R.id.textView);
// do like that for all widget 
layout.addView(view);