从列表视图中的微调器获取选定值?

时间:2013-08-25 03:52:57

标签: java android android-listview android-spinner

您好我正在创建将从微调器中选择数据的应用程序(在列表视图中)并希望选择值。所以这是我的日志猫信息

 08-25 09:13:28.786: W/dalvikvm(580): threadid=1: thread exiting with uncaught exception (group=0x40015560)
 08-25 09:13:28.892: E/AndroidRuntime(580): FATAL EXCEPTION: main
 08-25 09:13:28.892: E/AndroidRuntime(580): java.lang.NullPointerException
 08-25 09:13:28.892: E/AndroidRuntime(580):     at com.example.dcsd.Result$1.onClick(Result.java:44)
 08-25 09:13:28.892: E/AndroidRuntime(580):     at android.view.View.performClick(View.java:2485)
 08-25 09:13:28.892: E/AndroidRuntime(580):     at android.view.View$PerformClick.run(View.java:9080)
 08-25 09:13:28.892: E/AndroidRuntime(580):     at android.os.Handler.handleCallback(Handler.java:587)
 08-25 09:13:28.892: E/AndroidRuntime(580):     at android.os.Handler.dispatchMessage(Handler.java:92)
 08-25 09:13:28.892: E/AndroidRuntime(580):     at android.os.Looper.loop(Looper.java:123)
 08-25 09:13:28.892: E/AndroidRuntime(580):     at android.app.ActivityThread.main(ActivityThread.java:3683)
 08-25 09:13:28.892: E/AndroidRuntime(580):     at java.lang.reflect.Method.invokeNative(Native Method)
 08-25 09:13:28.892: E/AndroidRuntime(580):     at java.lang.reflect.Method.invoke(Method.java:507)
 08-25 09:13:28.892: E/AndroidRuntime(580):     at        com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 08-25 09:13:28.892: E/AndroidRuntime(580):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 08-25 09:13:28.892: E/AndroidRuntime(580):     at dalvik.system.NativeStart.main(Native Method)

这是我的Result.java类

 package com.example.dcsd;

 import java.util.ArrayList;
 import java.util.List;

 import android.app.ListActivity;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.View.OnClickListener;
 import android.widget.AdapterView.OnItemSelectedListener;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.ListView;
 import android.widget.Spinner;
 import android.widget.TextView;

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


@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));
    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= sp.getSelectedItem().toString();
                //Log.d("This", "huu");
            }
            sp.setOnItemSelectedListener(wrapper);
            //intent.putExtra("values", sp.getSelectedItem().toString());
            //intent.putExtra("values", sp.setOnItemSelectedListener(wrapper));
            startActivity(intent);              
        }
    });
}

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]);
        sp=(Spinner)row.findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(Result.this, R.array.grades, android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adapter);
        //sp.setOnItemSelectedListener(wrapper);
        //int dd=row.getId();
        return row;     
    }
}
 }

我的SpinnerWrapper.java类就像这样lokks

 package com.example.dcsd;

 import android.util.Log;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemSelectedListener;

 public class SpinnerWrapper implements OnItemSelectedListener {

public SpinnerWrapper() {

}

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

}

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

}

 }

我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

试试这个..

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

    ArrayList<String> spinner_items;

    Spinner spinner_from;
        Button btn;     


    ArrayAdapter<String> from_adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner_items = new ArrayList<String>();
        spinner_items.add("Select");
        spinner_items.add("One");
        spinner_items.add("Two");
        spinner_items.add("Three");
        spinner_items.add("Four");      

        spinner_from = (Spinner) findViewById(R.id.spinner_from);
                btn = (Button) findViewById(R.id.btn);


        from_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, spinner_items);
        from_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_from.setAdapter(from_adapter);

 btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

  Toast.makeText(getBaseContext(), ""+spinner_from.getSelectedItem().toString().trim(), Toast.LENGTH_SHORT).show();

            }

        }
    });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}