我已经基于按钮的点击动态创建了EditText
s,并使用List从EditText
s中获取了输入。我试图在内部将其存储为字符串,并且只保存了一部分值。到目前为止,这是我的代码。
这是添加新的EditText
:
List<EditText> allElem = new ArrayList<EditText>();//utk save
public void addLine() {
LinearLayout ll = (LinearLayout) findViewById(R.id.layout_dynamic);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//add text view desc
TextView tv=new TextView(this);
tv.setLayoutParams(p);
tv.setText("Description");
ll.addView(tv);
// add edittext
EditText et = new EditText(this);
et.setLayoutParams(p);
ll.addView(et);
et.setHint("Enter expenses description");
allElem.add(et);//add dlm list allElem
//add text view spend
TextView tv1=new TextView(this);
tv1.setLayoutParams(p);
tv1.setText("Spending (RM)");
ll.addView(tv1);
// add edittext
EditText et1 = new EditText(this);
et1.setLayoutParams(p);
ll.addView(et1);
et1.setHint("Enter your spending in RM");
et1.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
allEd.add(et1);//add dlm list allEd
allElem.add(et1);//add dlm list allElem
//add text view spend
TextView tv2=new TextView(this);
tv2.setLayoutParams(p);
tv2.setText("Extra Note");
ll.addView(tv2);
// add edittext
EditText et2 = new EditText(this);
et2.setLayoutParams(p);
ll.addView(et2);
et2.setHint("Enter your note (optional)");
allElem.add(et2);//add dlm list allElem
}
我无法从列表中检索所有值。列表中仅保存了最后3个值。
String data, dataText;
public String file = "expensesrecord";
public void save(){
for (int i=0;i<allElem.size();i++){
EditText descr = allElem.get(i);
dataText="Description: " + descr.getText().toString() + "\n";
i++;
EditText spendi = allElem.get(i);
dataText +="Spending: RM " + spendi.getText().toString() + "\n";
i++;
EditText note = allElem.get(i);
dataText +="Extra Note: " + note.getText().toString() + "\n\n";
}
data="Date: " + tvDate.getText().toString() + "\n" + "Total Expenses: " + tvDisplayResult.getText().toString() + "\n";
data+=dataText;
try {
FileOutputStream fOut = openFileOutput(file, Context.MODE_APPEND);
fOut.write(data.getBytes());
fOut.close();
Toast.makeText(getBaseContext(),"Record saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
预期结果应该是:
日期:2019/12/1 总费用:30
描述:午餐 消费:15 注意:2人
Desc:晚餐 消费:30 注意:3人
但是,实际结果仅显示:
日期:2019/12/1 总费用:30
Desc:晚餐 消费:30 注意:3人