java - TableRow 在 TableLayout 中返回空值?
问题描述
我有一个没有任何布局文件的活动。这里有一个 TableLayout,它有 5 个输入行。我想从行中获取那些 edittext 值。我已经尝试了很多,但仍然无法从行中获取数据。这是我的activity.java 文件。还有其他方法可以从edittext 获取数据吗?我试过这个。重复的问题,但我的应用程序崩溃并显示错误“无法将 TableRow 转换为 EditText”。
public class MainActivity extends AppCompatActivity {
ScrollView sv;
TableLayout myTableLayout;
String userName,
firstName,
city,
zipCode,
age;
UserListAdapter adapter;
RecyclerView recyclerView;
List <User> userList;
TableRow inputRow;
String temp = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("TurnToTech", "Project Name - Dynamic table view");
sv = new ScrollView(this);
userList = new ArrayList <>();
myTableLayout = new TableLayout(this);
drawScreen();
recyclerView = new RecyclerView(this);
}
public void drawScreen() {
// this might be a redraw, so we clean the view's container
myTableLayout.removeAllViews();
sv.removeAllViews();
// special rows
TableRow buttonRow = new TableRow(this);
TableRow titleRow = new TableRow(this);
//Alerts
final AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Saveing");
alertDialog.setMessage("Saved..");
final AlertDialog alertDialog1;
alertDialog1 = new AlertDialog.Builder(this).create();
alertDialog1.setTitle("Cancel");
alertDialog1.setMessage("Rejected");
// margins
buttonRow.setPadding(20, 10, 20, 10);
titleRow.setPadding(20, 10, 20, 10);
// the title
TextView title = new TextView(this);
title.setText("Data input form");
title.setTextSize(14);
title.setTextColor(Color.BLACK);
title.setGravity(Gravity.CENTER_HORIZONTAL);
titleRow.addView(title);
titleRow.setGravity(Gravity.CENTER);
// the title tablelayout
TableLayout titleTableLayout = new TableLayout(this);
titleTableLayout.addView(titleRow);
// we add the title layout to the main one
myTableLayout.addView(titleTableLayout);
/// the 5 input rows
inputRow(myTableLayout, "Name", 30, 10000); //returning ""
inputRow(myTableLayout, "First Name", 30, 10001); //returning ""
inputRow(myTableLayout, "Age", 3, 10002); //returning ""
inputRow(myTableLayout, "City", 20, 10003); //returning ""
inputRow(myTableLayout, "Zip Code", 6, 10004); //returning ""
// the buttons table layout
// purpose : right align for both buttons
TableLayout buttonsLayout = new TableLayout(this);
buttonRow.setPadding(20, 50, 40, 0);
// the accept and cancel buttons
Button btAccept = new Button(this);
btAccept.setText("Save");
btAccept.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
//TODO here I want to get all the row value that I filled from the table and store the value inside the user object.
User user = new User(userName, firstName, age, city, zipCode);
userList.add(user);
adapter = new UserListAdapter(userList, MainActivity.this);
recyclerView.setAdapter(adapter);
}
});
Button btCancel = new Button(this);
btCancel.setText("Cancel");
btCancel.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
alertDialog1.show();
}
});
buttonRow.addView(btAccept);
buttonRow.addView(btCancel);
buttonRow.setGravity(Gravity.RIGHT);
buttonsLayout.addView(buttonRow);
myTableLayout.addView(buttonsLayout);
sv.addView(myTableLayout);
setContentView(sv);
}
public void inputRow(TableLayout tl, String label, int inputSize, int inputID) {
inputRow = new TableRow(this);
TextView tv = new TextView(this);
EditText edit = new EditText(this);
// some margin
inputRow.setPadding(20, 10, 20, 0);
tv.setText(label);
edit.setMaxWidth(inputSize * 7);
edit.setMinimumWidth(inputSize * 7);
edit.setId(inputID);
edit.setGravity(Gravity.RIGHT);
inputRow.addView(tv);
inputRow.addView(edit);
tl.addView(inputRow);
}
}
我在 onClick 事件中提到了一个待办事项。
解决方案
在onClick()
for内部btAccept
,您需要为 each 调用类似的内容EditText
:
@SuppressLint("ResourceType") EditText nameEditText = findViewById(10000);
String nameEntered = nameEditText.getText().toString();
.
.
.
您已经为所有的 . 完成了此操作EditTexts
,您可以将它们存储在一个User
对象中。
推荐阅读
- reactjs - setInterval 中的函数在 ReactJs 中无法正常工作
- c++ - 定义类模板构造函数的两种方式之间的区别
- c# - 批量添加具有多个外键的对象时,实体框架的奇怪行为
- python - Python cmd自动完成:在单独的行上显示选项
- java - 休眠期望时间戳数据库列的日期
- python - 强制 Dataflow 工作人员使用 Python 3?
- python - Jupyter Notebook 和 Conda 命令行之间的包不一致
- electron - 电子 ipcRenderer 一次传递一个字母的字符串
- tensorflow - Tensorflow TensorBoard 不显示 acc、loss、acc_val 和 loss_val 只显示 epoch_accuracy 和 epoch_loss
- c# - Office 365 中的租户是什么?