i want to implement textwatcher for dynamically generated edittexts and want to get text in those edittexts

ε祈祈猫儿з 提交于 2021-01-29 08:09:31

问题


protected void updateTable() {
    // TODO Auto-generated method stub

    final TableLayout tl = (TableLayout) findViewById(R.id.settingtable);
    tl.removeAllViews();

    for(int i=0; i<10; i++) {


        final TableRow tablerow = new TableRow(this);
        tablerow.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        final TextView deviceedit = new TextView(this);

        deviceedit.setText(i+1);
        deviceedit.setTextColor(Color.BLACK);
        deviceedit.setGravity(Gravity.CENTER);
        deviceedit.setTextSize(20);
        deviceedit.setBackgroundResource(R.drawable.lighter_cell_shape);

        tablerow.addView(deviceedit);


        final EditText pondedit = new EditText(this);
        pondedit.setText("");
        pondedit.setTextColor(Color.BLACK);
        pondedit.setGravity(Gravity.CENTER);
        pondedit.setBackgroundResource(R.drawable.lighter_cell_shape);
        tablerow.addView(pondedit);
        pondedit.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub


            }
        });

        tl.addView(tablerow);
    }

}

When I entered text in generated edittexts, how can I get those texts from edittexts with references to those textViews i.e, 1-> hai , 2-> bye like this. Is TextWatcher useful for this context or not? I want to get data from edittexts and to save the data in database with reference to TextView.


回答1:


probably you will want to do this in separated for loops.

and to get the text entered in each EditText you need something like this:

ArrayList<String> textFromEditText = new ArrayList<>();

textFromEditText.add(pondedit.getText().toString());

dont forget to call the .toString(); method since the from EditText you get a Editable type object.

once you have your data ready in the ArrayList just call this in another for loop:

for(i = 0;i < textFromEditText.size();i++){   
     final TextView deviceedit = new TextView(this);

     //other code of your textView


     deviceedit.setText(textFromEditText.get(i));

}

maybe somebody can complement my answer.



来源:https://stackoverflow.com/questions/14983122/i-want-to-implement-textwatcher-for-dynamically-generated-edittexts-and-want-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!