I have a spinner in which when a user picks a value, the onItemSelectedListener opens up a dialog to put additional details
private AdapterView.OnItemSelectedListener spinnerListeners=new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()){
case R.id.puttSpinner:
// open dialog to enter Additional putt Stats
if(position > 0 ) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
LinearLayout layout = new LinearLayout(v.getContext());
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);layout.setOrientation(LinearLayout.VERTICAL);layout.setLayoutParams(parms);
layout.setGravity(Gravity.CLIP_VERTICAL);
layout.setPadding(2, 2, 2, 2);
for (int i=1;i<position+1;i++){
LinearLayout ll = new LinearLayout(v.getContext());
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setLayoutParams(llp);
TextView tv = new TextView(v.getContext());
tv.setText("Putt "+String.valueOf(i)+":");
tv.setPadding(40, 40, 40, 40);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(20);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tvParams.bottomMargin = 5;
//ll.addView(tv,tvParams);
ll.addView(tv);
Spinner s = new Spinner(v.getContext());
String[] puttDistList = getResources().getStringArray(R.array.putt_dist_list);
s.setAdapter(new ArrayAdapter<String>(v.getContext(),R.layout.putting_practice, puttDistList));
s.setOnItemSelectedListener(puttDistSelectListener);
LinearLayout.LayoutParams sParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.addView(s,sParams);
layout.addView(ll);
}
alertDialog.setView(layout);
alertDialog.setTitle("Putting Stats - Distance to Pin");
alertDialog.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
Now, when a user picks the value from Dialog Spinner s
, it's listener updates the database with user input, but how do I get the textvalue of TextView tv
that is associated with this spinner
Both the TextView and Spinner are contained in dynamically created Linear Layout "ll" that is in parent layout "layout". Both are created dynamically
Thanks in advance
final AdapterView.OnItemSelectedListener puttDistSelectListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//add the putt# and distance to temp_putt_stats table
DbAdapter adapter = new DbAdapter(view.getContext());
String distValue = parent.getItemAtPosition(position).toString();
switch (position){
case 0: //Inches
break;
case 1: //<3ft
break;
case 2: //<6 ft
break;
case 3: //<10 ft
break;
case 4: //10-20
break;
case 5: //20-30
break;
case 6: //30-40
break;
case 7: //> 40
break;
}
Toast.makeText(view.getContext(), "Position :"+position+" ListItem : " +distValue ,Toast.LENGTH_SHORT).show();
}