1

I have an SQLite Database in my application with fieldname,fieldtype columns. fieldname consists of EditText namimg ...ex: etsearch,etsave,etcancel etc.. fieldtype is EditText.

I add fieldname values dynamically. I need to declare these EditTexts in my java class, as i'am creating my layout using java code.

I created an arraylist and added the fieldname values into it. I am confused how to declare the EditTexts using for loop.

My code :

public class MainActivity extends Activity 
{
LinearLayout ll;
LinearLayout llgrower;
Cursor cursor;
ArrayList<String> edittexts;


@Override
protected void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    ll = new LinearLayout(this);
    llgrower= new LinearLayout(this);

    ll.addView(llgrower);
    this.setContentView(ll);

    cursor = DataBase.getdata("query to get fieldname,fieldtype");

    try 
    {
        if (cursor.moveToFirst())
        {
            do
            {
                edittexts.add(cursor.getString(0));

            } while (cursor.moveToNext());
        }

    } 
    catch (Exception e) {}

    for (int i=0;i<edittexts.size();i++)
    {

    }

}

}

4

2 回答 2

2

这是如何做到这一点的基础知识。

LinearLayout layout = (LinearLayout) findViewById(R.id.layout); // this is whatever view you want to add them to
EditText editText;

for(int i=0;i<nunberOfEditTexts;i++){

    editText = new EditText(this);

    ...

    // set the properties of the EditText

    ...

    // add to the view
    layout.addView(editText);


}

您可能需要查看 LayoutParams 以了解如何调整 EditTexts 的大小和位置。

祝你好运

于 2013-02-01T06:53:30.280 回答
0
 private void addEditText(int count) {
    int id = 999;

    EditText et;        
    RelativeLayout.LayoutParams params;             

    for(int i=0; i<count; i++) {
        et= new EditText(this);
        et.setId(id);

        params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 10, 0, 0);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

        if(i > 0) {
            params.addRule(RelativeLayout.BELOW, id - i);
        }
        //ASSIGN VALUE FOR YOUR EDITTEXT FROM db HERE.
        layoutSpinnerContainer.addView(spinner, params);

        id++;
    }
}
于 2013-02-01T06:56:12.433 回答