TableRow 在 TableLayout 中返回空值?
TableRow is returning empty value in TableLayout?
我有一个 Activity 没有任何布局文件。这里有一个 TableLayout,它有 5 个输入行。我想从行中获取那些 edittext 值。我已经尝试了很多但仍然无法从行中获取数据。
这是我的 activity.java file.Is 还有其他方法可以从 edittext 获取数据吗?我试过了。Duplicate Question 但我的应用程序崩溃并显示错误 "TableRow can not be cast into 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()
中 btAccept
,您需要为每个 EditText
:
调用类似的方法
@SuppressLint("ResourceType") EditText nameEditText = findViewById(10000);
String nameEntered = nameEditText.getText().toString();
.
.
.
如果您已为所有 EditTexts
完成此操作,您可以将它们存储在 User
对象中。
我有一个 Activity 没有任何布局文件。这里有一个 TableLayout,它有 5 个输入行。我想从行中获取那些 edittext 值。我已经尝试了很多但仍然无法从行中获取数据。 这是我的 activity.java file.Is 还有其他方法可以从 edittext 获取数据吗?我试过了。Duplicate Question 但我的应用程序崩溃并显示错误 "TableRow can not be cast into 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()
中 btAccept
,您需要为每个 EditText
:
@SuppressLint("ResourceType") EditText nameEditText = findViewById(10000);
String nameEntered = nameEditText.getText().toString();
.
.
.
如果您已为所有 EditTexts
完成此操作,您可以将它们存储在 User
对象中。