Reputation:
I wanted to know if we can assign any unique number to elements inside an inflated view.
My view looks like this and this image might give you an idea what I'm trying to do:
What I'm trying to do is I'm changing the values of the edit text boxes based on the spinner item selected like this:
//spinnerPlanItinerary is the first spinner
spinnerPlanItinerary.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
List<Town> listOfTowns = new ArrayList<Town>();
ArrayAdapter<Town> townplanitineraryadapter2 = new ArrayAdapter<Town>(this,
android.R.layout.simple_dropdown_item_1line, listOfTowns);
townplanitineraryadapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTown.setAdapter(townplanitineraryadapter2);
spinnerTown.setText(""); //second spinner in the view
edBelt.setText(""); //first edit text in the view
edStrata.setText(""); //second edit text in the view
townplanitineraryadapter2.notifyDataSetChanged();
}
});
I can't give any unique IDs to the element since they already have IDs in my layout file. I've also been using setTag()
and getTag()
but getTag()
doesnt give me anything.
Also note that the card views are inflated onclick of a button like this:
final LinearLayout itineraryDetailLL = (LinearLayout) findViewById(R.id.itineraryDetailLinearlayout);
final View childView = getLayoutInflater().inflate(R.layout.cardview, null);
itineraryDetailLL.addView(childView);
Upvotes: 2
Views: 238
Reputation: 3253
Thanks for clarifying in the comments.
From what I understand now is, that you dynamically inflate and add additional text fields. Then they all have the same ID, correct.
So, how can you distinguish them then? Easy solution:
final View childView = getLayoutInflater().inflate(R.layout.cardview, null);
itineraryDetailLL.addView(childView);
You should not make only one childView
member, but create a list of them (or Dictionary, or whatever fits best for your next actions).
In each of them you can do a .findViewById
for the ID of the edit-text contained in that view.
So, maybe it could look like this:
Map<Integer, View> myChilds = new ....
final View childView = getLayoutInflater().inflate(R.layout.cardview, null);
itineraryDetailLL.addView(childView);
// Here is your "make it unique" part:
myChilds.put(your_next_unique_id, childView);
...
// When you want to access them
EditText txt = myChilds.get(my_unique_id).findViewById(xml_id_of_edittext);
txt.setText("...");
I hope you get the idea where this can lead you to. Cheers, Gris
Upvotes: 2
Reputation: 437
if you want to change the value of the EditText you do not need to a unique identifier for that. You can do this:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if (spinner.getSelectedItem().toString().equals("someSpinnerData")) {
editText.setText("yourDesiredText");
} else if (spinner.getSelectedItem().toString().equals("someSpinnerData2")) {
editText.setText("someSpinnerData2");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Upvotes: 0