【问题标题】:TableRow is returning empty value in TableLayout?TableRow 在 TableLayout 中返回空值?
【发布时间】:2019-03-13 17:08:35
【问题描述】:

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

【问题讨论】:

    标签: java android android-tablelayout


    【解决方案1】:

    在onClick() 内部,对于btAccept,您需要为每个EditText 调用类似的内容:

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

    您已经为您的所有EditTexts 完成此操作,您可以将它们存储在User 对象中。

    【讨论】:

    • 感谢您的建议,但这是一项任务。我会通过在 xml 文件中添加表格布局和表格行来做到这一点,但我需要务实地这样做。谢谢。
    • 这不会发生。编译器显示错误。你在你的编译器上检查了吗?我正在使用安卓工作室。
    • 错误是什么?我不会为了测试而重新创建整个项目。
    • 它显示“类型 id 的预期资源”。编译时错误。
    • 这不是编译时错误。这是一个 Lint 错误,因为这是一种愚蠢的做事方式。请参阅我的编辑以了解如何抑制错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 2022-12-14
    • 2011-03-20
    相关资源
    最近更新 更多