0

我在从列表视图中获取旋转值时遇到了一些问题。在列表视图中我有 textview 并且与 spinner 相关..

这是我的 Result.java

public class Result extends ListActivity{
SpinnerWrapper wrapper=new SpinnerWrapper();
Spinner sp;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result);
    MyAdapter adap=new MyAdapter(this, android.R.layout.simple_list_item_1,R.id.textView1, getResources().getStringArray(R.array.modules));
    adap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    setListAdapter(adap);
    Button b=(Button) findViewById(R.id.button12);
    //final Spinner sp=(Spinner) findViewById(R.id.spinner1);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent=new Intent(Result.this,Cal.class);
            for(int i=0;i<15;i++){
                //String text= sp1.getSelectedItem().toString();
                //Log.d("This", text);
            }
            //Spinner sp1=og.getSp();
            ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(Result.this, R.array.grades, android.R.layout.simple_spinner_dropdown_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            sp.setAdapter(adapter);
            sp.setOnItemSelectedListener(wrapper);
            intent.putExtra("values", sp.getSelectedItem().toString());
            //intent.putExtra("values", sp.setOnItemSelectedListener(wrapper));
            startActivity(intent);              
        }
    });
}
public void getObject(Spinner sp){
    this.sp=sp;
}

private class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings) {
        super(context, resource, textViewResourceId, strings);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row=inflater.inflate(R.layout.item_layout, parent,false);
        String[] items=getResources().getStringArray(R.array.modules);
        TextView tv=(TextView) row.findViewById(R.id.textView1);        
        tv.setText(items[position]);
        Spinner sp=(Spinner)row.findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(Result.this, R.array.grades, android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adapter);
        getObject(sp);
        //og.setSp(sp);
        //sp.setOnItemSelectedListener(wrapper);
        //int dd=row.getId();
        return row;     
    }
}

}

我的 Spinner Wrapper 类看起来像这样..

public class SpinnerWrapper implements OnItemSelectedListener {

public SpinnerWrapper() {

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
     String text=parent.getSelectedItem().toString();
     int intee=parent.getSelectedItemPosition();
     String pos=Integer.toString(intee);
     String tex=parent.getItemAtPosition(position).toString();
     Log.d("Selected", text);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

我想获得每个微调器的旋转值..我该怎么做?

4

2 回答 2

0

I don't see why you can't get the value by

 String tex=parent.getItemAtPosition(position).toString();
 Log.d("You have selected", tex);

You can create an ArrayList<String> and add the selected value to that ArrayList. It will be something like this.

static ArrayList<String> sth = new ArrayList<String>();

    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        Toast.makeText(
                parent.getContext(),
                "OnItemSelectedListener : "
                        + parent.getItemAtPosition(pos).toString(),
                Toast.LENGTH_SHORT).show();

        for (int i = 0; i < parent.getItemIdAtPosition(pos); i++) {
            sth.add(parent.getItemAtPosition(pos).toString());
        }

        Log.w(getClass().getSimpleName(), "sth size " + sth.size());

    }

You can pass that ArrayList around since the selected values are stored.

于 2013-08-27T04:55:23.880 回答
0

//sp.setOnItemSelectedListener(wrapper);

I see your code is commented. so the OnItemSelectedListener  is not called.

You call different listener for different spinner.
于 2013-08-27T05:00:40.383 回答