Issue
I want to avoid set text on other edittext. I want to set text of alert list dialog's item on which I clicked editext.
You can undersand my problem from above images. I can't set text on which I have to. I want to set text on first edittext but value is setting on recently created view.
Here is my code
public class MainActivity2 extends AppCompatActivity
{
LinearLayout parent_linear_layout;
EditText editText;
String value;
Button add;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parent_linear_layout = findViewById(R.id.parent_linear_layout);
add = findViewById(R.id.button_add);
add.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
addView();
}
});
}
private void addView()
{
View view = getLayoutInflater().inflate(R.layout.row_add_language2, null, false);
parent_linear_layout.addView(view);
editText = view.findViewById(R.id.et_name2);
editText.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
simpleDialog();
}
});
}
public void simpleDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select names");
String[] array = getResources().getStringArray(R.array.language);
builder.setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
value = array[i];
}
});
builder.setPositiveButton("Add", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
editText.setText(value);
}
});
builder.create();
builder.show();
}
}
Solution
I haven't tested this code, but the idea is to always pass the clicked edittext to simpleDialog so it sets the text in the correct one and not the newest one.
private void addView(){
View view = getLayoutInflater().inflate(R.layout.row_add_language2, null, false);
parent_linear_layout.addView(view);
EditText editText = view.findViewById(R.id.et_name2);
editText.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
EditText et = (EditText) view
simpleDialog(et);
}
});
}
public void simpleDialog(EditText editText)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select names");
String[] array = getResources().getStringArray(R.array.language);
builder.setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
value = array[i];
}
});
builder.setPositiveButton("Add", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
editText.setText(value);
}
});
builder.create();
builder.show();
}
Answered By - just_user
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.