如何从listview中选择旋转值?

时间:2013-08-27 04:43:05

标签: java android android-listview android-spinner

我在列表视图中获取旋转值时遇到了一些问题..在列表视图中我有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;     
    }
}

}

My 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

}

}

我想获得每个微调器的旋转值。我可以这样做吗?

2 个答案:

答案 0 :(得分:0)

我不明白为什么你不能通过

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

您可以创建ArrayList<String>并将所选值添加到ArrayList。它会是这样的。

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

    }

您可以传递ArrayList,因为存储了所选的值。

答案 1 :(得分:0)

// sp.setOnItemSelectedListener(包装);

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

You call different listener for different spinner.