-1

I have a button that when a user clicks, it dynamically adds editTexts' and a clickable TextView ("X") which should delete its corresponding editText, to its layout. The number of editTexts' and TextView created are based on the number of times the button is clicked. But I now want to remove the editText when I click on the clickable TextView ("X")

View.OnClickListener generate = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        for (int i = 0; i < count; i++) {
            addEdits = new EditText(getSymptomsActivity.this);
            cancel=new TextView(getSymptomsActivity.this);
            addEdits.setLayoutParams(new ActionBar.LayoutParams(400, 79));
            addEdits.setText("");
            cancel.setText("X");
            platform.addView(addEdits);
            platform.addView(cancel);
            values.add(addEdits);
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                   for(int j = count; j>0 ; j--){
                       platform.removeView(addEdits);
                       //values.remove(addEdits);
                       cancel.setVisibility(TextView.GONE);
                   }
                }
            });

The issue at the moment is that, when the clickable TextView("X") is clicked it only deletes the last created editText and only that.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
mcprilla79
  • 99
  • 1
  • 11
  • 5
    Pro-tips for posting: we like to trim standard boilerplate text from questions, and "please help me" is one of them. Useful reading: [here](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) and [here](https://meta.stackoverflow.com/questions/255196/remove-help-please-from-titles). Additionally, since this is a help site, adding "please help" is somewhat redundant, and may be read as a form of begging. – halfer Jan 23 '17 at 00:11

1 Answers1

0

You should call setId() to give EditText and TextView unique ids and call findViewById() when you want to remove them.

WenChao
  • 3,186
  • 5
  • 26
  • 42
  • but the Views are not created through xml to have unique Ids, so wnt they conflict when the Views are created through the loop – mcprilla79 Jan 22 '17 at 22:08
  • Hi, you can have a look at this thread 'http://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts', you can use `setTag()` in stead, it takes a string, so you can put your own stuff. – WenChao Jan 22 '17 at 22:29
  • that dint help. any other ideas please? – mcprilla79 Jan 22 '17 at 23:18
  • i added `addEdits.setId(View.generateViewId()); cancel.setId(View.generateViewId());` and `platform.removeView(addEdits.findViewById(View.generateViewId()));` – mcprilla79 Jan 22 '17 at 23:34
  • View.generateViewI‌​d() is out of your control, you can do something like 'setTag("myeditview" + i)' and use `findViewWithTag()` – WenChao Jan 22 '17 at 23:51
  • still the same result – mcprilla79 Jan 23 '17 at 00:27