以编程方式将带有textview和spinner的相对布局数组添加到线性布局中

时间:2014-08-16 12:45:25

标签: android android-layout

我正在尝试将一个RelativeLayouts数组添加到我在布局xml文件中定义的一个LinearLayout(ll)。我需要填充的相对布局元素的数量完全取决于我在数据库中的数据数量。我只想在左侧看一系列textview,在右边看一个相应的微调器。我想要的输出如下所示:

enter image description here

但是下面的代码并没有为我完成这项工作。我得到一个空屏幕,虽然正在显示Toast消息,确认循环正在完全执行。我还是Android新手,我确信我错过了这里的一些东西。任何帮助将非常感激。提前谢谢!

    package com.gk.srmacads;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class EditGrade extends Activity {

    dbOpenHelper dbHelper;
    SQLiteDatabase db;
    Cursor c;
    String[] column ={dbOpenHelper.COLUMN_ID,dbOpenHelper.COLUMN_SEM,dbOpenHelper.COLUMN_SUBNAME,dbOpenHelper.COLUMN_GRADE};
    int len;
    RelativeLayout elems[];
    Spinner gradeSpin[];
    TextView tv[];

    LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_edit_grade);

        ll= (LinearLayout) findViewById(R.id.egSem1);

        dbHelper=new dbOpenHelper(this);
        db=dbHelper.getReadableDatabase();

        c=db.query("ACAD_TABLE", column, null, null, null, null, null);

        len=c.getCount();
        c.moveToFirst();
        Toast.makeText(this, String.valueOf(c.getCount()), Toast.LENGTH_SHORT).show();

        elems=new RelativeLayout[len];
        gradeSpin= new Spinner[len];
        tv=new TextView[len];
        RelativeLayout.LayoutParams rlParam= new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams tvParam= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams spinnerParam= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        spinnerParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        tvParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        ArrayAdapter<CharSequence> gradeAdap = ArrayAdapter.createFromResource(this, R.array.grades, R.layout.spinnerlayout);

        int spinPos;
        for(int i=0; i<len ;i++)
        {
            elems[i]= new RelativeLayout(this);
            tv[i]= new TextView(this);
            gradeSpin[i]= new Spinner(this);

            tv[i].setId(1);
            gradeSpin[i].setId(2);

            spinnerParam.addRule(RelativeLayout.RIGHT_OF,tv[i].getId());

            elems[i].setLayoutParams(rlParam);
            tv[i].setLayoutParams(tvParam);
            gradeSpin[i].setLayoutParams(spinnerParam);

            tv[i].setText(c.getString(c.getColumnIndex(dbOpenHelper.COLUMN_SUBNAME)).toString());
            Toast.makeText(this, c.getString(c.getColumnIndex(dbOpenHelper.COLUMN_SUBNAME)), Toast.LENGTH_SHORT).show();
            gradeSpin[i].setAdapter(gradeAdap);
            //spinPos=getGradePosition(c.getString(c.getColumnIndex(dbOpenHelper.COLUMN_GRADE)));
            //gradeSpin[i].setSelection(spinPos);

            elems[i].addView(tv[i]);
            elems[i].addView(gradeSpin[i]);

            ll.addView(elems[i]);
            c.moveToNext();         
        }
    }
}

activity_edit_grade.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" 
    android:id="@+id/egmainLayout">    

   <LinearLayout 
       android:id="@+id/egSem1"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">    
   </LinearLayout>    

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

这里是我编译的代码,基本上我猜你确实从cursor属性中检索了数据,下面是附加的代码:

public class MainActivity extends Activity {

    String[] column = { "Lorem", "Ipsum", "Dolor", "Sit", "Amet" };
    int len;
    RelativeLayout elems[];
    Spinner gradeSpin[];
    TextView tv[];

    LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ll = (LinearLayout) findViewById(R.id.egSem1);

        len = 10;

        elems = new RelativeLayout[len];
        gradeSpin = new Spinner[len];
        tv = new TextView[len];

        RelativeLayout.LayoutParams rlParam = new RelativeLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams tvParam = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams spinnerParam = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        spinnerParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        tvParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        ArrayAdapter<String> arrayAdap = new ArrayAdapter(this,
                android.R.layout.simple_spinner_dropdown_item, column);

        int spinPos;
        for (int i = 0; i < len - 1; i++) {
            elems[i] = new RelativeLayout(this);
            tv[i] = new TextView(this);
            gradeSpin[i] = new Spinner(this);

            tv[i].setId(i + 1);

            spinnerParam.addRule(RelativeLayout.RIGHT_OF, tv[i].getId());

            elems[i].setLayoutParams(rlParam);
            tv[i].setLayoutParams(tvParam);
            gradeSpin[i].setLayoutParams(spinnerParam);

            tv[i].setText("Lorem Ipsum");
            gradeSpin[i].setAdapter(arrayAdap);

            elems[i].addView(tv[i]);
            elems[i].addView(gradeSpin[i]);

            ll.addView(elems[i]);
            //c.moveToNext();
        }

    }

}

SOURCE CODE