Reputation: 3428
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++)
{
}
}
}
Upvotes: 1
Views: 1077
Reputation: 5271
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++;
}
}
Upvotes: 0
Reputation: 14472
Here's the basics of how to do it.
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);
}
You will probably want to look at LayoutParams to understand how to size and position your EditTexts.
Good luck
Upvotes: 2