我有一个没有任何布局文件的Activity。这里有一个TableLayout具有5个输入行。我想从行中获取那些edittext值。我已经尝试了很多,但仍然无法从行中获取数据。 这是我的activity.java文件,还有其他方法可以从edittext获取数据吗?我已经尝试过。Duplicate Question,但是我的应用程序崩溃并显示错误“无法将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事件中提到了一个待办事项。