我的应用程序中有一个包含fieldname、fieldtype列的SQLite数据库。fieldname由EditText名称组成etc ...ex: etsearch,etsave,etcancel等。字段类型为EditText。
我动态添加fieldname值。我需要在java类中声明这些EditTexts,因为我正在使用java代码创建布局。
我创建了一个数组列表,并将fieldname值添加到其中。我对如何使用for循环声明EditTexts感到困惑。
我的代码:
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++)
{
}
}
}
发布于 2013-02-01 06:53:30
这里是如何做到这一点的基础。
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-01 06:56:12
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++;
}
}
https://stackoverflow.com/questions/14641119
复制