java - TableRow 在 TableLayout 中返回空值?

标签 java android android-tablelayout

我有一个没有任何布局文件的 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 事件中提到了一个待办事项。

最佳答案

btAcceptonClick() 内部,您需要为每个 EditText 调用类似以下内容:

@SuppressLint("ResourceType") EditText nameEditText = findViewById(10000);
String nameEntered = nameEditText.getText().toString();
.
.
.

如果您对所有 EditTexts 执行了此操作,则可以将它们存储在 User 对象中。

关于java - TableRow 在 TableLayout 中返回空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55136598/

相关文章:

java - 如何使用反射从静态方法中获取类名

java - 如何在java中获取给定的日期字符串格式(模式)?

java - 安卓应用注册验证

android-layout - Android 表格布局适配器建议?

android - 如何制作结束学分页面 - 动画 TableLayout 而不会在显示尺寸处被切割

java - 使用 Jsoup 选择没有类的 HTML 元素

java - ANT Jenkins 单元测试 - 计算失败的测试

android - 安装错误 : Unknown failure

Android Studio,无法迁移到 AndroidX

Android 复杂表格布局