0

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();

    }

Image of dialog that opens up

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
archiver
  • 93
  • 9

2 Answers2

0

For API 17 and above you can generate an id for each View that you generate and set it with setId(). See generateViewId and setid.

Xiaochao Yang explains this and what to do before API 17 in an answer to this Stack Overflow question.

Once the generated id is set, you can use findViewById() to find the view within the parent layout.

Community
  • 1
  • 1
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
0

You could use tags.

On generating layout you can assign a tag (setTag()) to the items (1-x) and you could then do following:

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();
    int tagNumber = (int) view.getTag();
    switch (tagNumber ){
        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();

}

The text of the selected item could be retrieved by

TextView tv = (TextView) view.findViewByTag(x);
tv.getText();
ramden
  • 798
  • 1
  • 9
  • 25
  • I'm getting `NPE` with the solution offered – archiver Mar 23 '17 at 13:47
  • You also have to use a custom adapter for the spinner items (not arrayadapter) and in onBindView in this new adapter you would assign tags to the textviews – ramden Mar 23 '17 at 18:36