Java 从数据库获取值并在android spinner中进行设置

Java 从数据库获取值并在android spinner中进行设置,java,android,mysql,android-spinner,Java,Android,Mysql,Android Spinner,您好,如何从数据库中获取数据并在android应用程序的微调器中进行设置。我如何开发dis。下面的代码是将数据从微调器更新到android mysql数据库。但我希望需要从数据库中获取数据,并且获取的值显示在android应用程序的微调器值上。我如何开发此 这是我的android代码: public class InsertionExample extends Activity { private final String NAMESPACE = "http://xcart.com"; priv

您好,如何从数据库中获取数据并在android应用程序的微调器中进行设置。我如何开发dis。下面的代码是将数据从微调器更新到android mysql数据库。但我希望需要从数据库中获取数据,并且获取的值显示在android应用程序的微调器值上。我如何开发此

这是我的android代码:

public class InsertionExample extends Activity {
private final String NAMESPACE = "http://xcart.com";
private final String URL = "http://192.168.1.168:8089/XcartLogin/services/update?wsdl";
private final String SOAP_ACTION = "http://xcart.com/insertData";
private final String METHOD_NAME = "insertData";
Button btninsert;
String selectedItem;

static final String KEY_NAME = "orderid";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.change_status);
  /*  Intent in = getIntent();

    // Get XML values from previous intent
    String orderid = in.getStringExtra(KEY_NAME);

    // Displaying all values on the screen
    TextView lblName = (TextView) findViewById(R.id.textView1);


    lblName.setText(orderid); */


    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    btninsert = (Button)findViewById(R.id.btn_insert1);
    btninsert.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             Intent in = getIntent();
             String orderid = in.getStringExtra(KEY_NAME);

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            PropertyInfo unameProp =new PropertyInfo();
            unameProp.setName("Status");//Define the variable name in the web service method
            unameProp.setValue(selectedItem);//Define value for fname variable
            unameProp.setType(String.class);//Define the type of the variable

            request.addProperty(unameProp);
            PropertyInfo idProp =new PropertyInfo();
            idProp.setName("Orderid");//Define the variable name in the web service method
            idProp.setValue(orderid);//Define value for fname variable
            idProp.setType(String.class);//Define the type of the variable
            request.addProperty(idProp);



              SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
              envelope.setOutputSoapObject(request);
              HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

              try{
               androidHttpTransport.call(SOAP_ACTION, envelope);
                  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

                 TextView result = (TextView) findViewById(R.id.textView2);
                  result.setText(response.toString());
             }
           catch(Exception e){

           }
              }
    });

    //attach the listener to the spinner
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    //Dynamically generate a spinner data 
    createSpinnerDropDown();

}

//Add animals into spinner dynamically
private void createSpinnerDropDown() {

    //get reference to the spinner from the XML layout
    Spinner spinner = (Spinner) findViewById(R.id.spinner1);

    //Array list of animals to display in the spinner
    List<String> list = new ArrayList<String>();

    list.add("Q");
    list.add("P");
    list.add("F");
    list.add("I");
    list.add("C");

    //create an ArrayAdaptar from the String Array
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    //set the view for the Drop down list
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //set the ArrayAdapter to the spinner
    spinner.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    //attach the listener to the spinner
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

}






public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

         selectedItem = parent.getItemAtPosition(pos).toString();

       }


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

    }



    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Do nothing.
    }
}
public类InsertionExample扩展活动{
私有最终字符串命名空间=”http://xcart.com";
私有最终字符串URL=”http://192.168.1.168:8089/XcartLogin/services/update?wsdl";
私有最终字符串SOAP_ACTION=”http://xcart.com/insertData";
私有最终字符串方法\u NAME=“insertData”;
按钮BTN插入;
字符串selectedItem;
静态最终字符串键\u NAME=“orderid”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.change_状态);
/*Intent in=getIntent();
//从以前的意图中获取XML值
字符串orderid=in.getStringExtra(键名称);
//在屏幕上显示所有值
TextView lblName=(TextView)findViewById(R.id.textView1);
lblName.setText(orderid)*/
微调器微调器=(微调器)findViewById(R.id.spinner1);
btninsert=(按钮)findViewById(R.id.btn\u insert1);
btninsert.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
Intent in=getIntent();
字符串orderid=in.getStringExtra(键名称);
SoapObject请求=新的SoapObject(名称空间、方法名称);
PropertyInfo unameProp=新的PropertyInfo();
unameProp.setName(“Status”);//在web服务方法中定义变量名
unameProp.setValue(selectedItem);//为fname变量定义值
unameProp.setType(String.class);//定义变量的类型
request.addProperty(unameProp);
PropertyInfo idProp=新的PropertyInfo();
idProp.setName(“Orderid”);//在web服务方法中定义变量名
idProp.setValue(orderid);//定义fname变量的值
idProp.setType(String.class);//定义变量的类型
请求。添加属性(idProp);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(请求);
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
试一试{
调用(SOAP_操作,信封);
SoapPrimitive响应=(SoapPrimitive)信封.getResponse();
TextView结果=(TextView)findViewById(R.id.textView2);
result.setText(response.toString());
}
捕获(例外e){
}
}
});
//将侦听器附加到微调器
spinner.setOnItemSelectedListener(新的MyOnItemSelectedListener());
//动态生成微调器数据
createSpinnerDropDown();
}
//将动物动态添加到微调器中
私有void createSpinnerDropDown(){
//从XML布局获取对微调器的引用
微调器微调器=(微调器)findViewById(R.id.spinner1);
//要在微调器中显示的动物数组列表
列表=新的ArrayList();
列表。添加(“Q”);
列表。添加(“P”);
列表。添加(“F”);
列表。添加(“I”);
列表。添加(“C”);
//从字符串数组创建ArrayAdaptar
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple\u微调器\u项目,列表);
//设置下拉列表的视图
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
//将阵列适配器设置为微调器
旋转器。设置适配器(适配器);
adapter.notifyDataSetChanged();
//将侦听器附加到微调器
spinner.setOnItemSelectedListener(新的MyOnItemSelectedListener());
}
公共类MyOnItemSelectedListener实现OnItemSelectedListener{
已选择公共位置(AdapterView父项、视图、整数位置、长id){
selectedItem=parent.getItemAtPosition(pos.toString();
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
}
未选择公共无效(AdapterView父级){
//什么也不做。
}
}

请帮助我。我如何开发此功能。

您是否尝试在微调器上使用ArrayAdapter?我用过一次

    private class ColorSpinner extends ArrayAdapter<Integer> {

    public ColorSpinner(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return this.getView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Here can I set a custom layout or view of each item in the spinner.
        TextView textView = new TextView(PincodeTextView.this.context);
        String message = Integer.toHexString(this.getItem(position)).toUpperCase();
        textView.setBackgroundColor(this.getItem(position));
        textView.setText("0x" + message);
        return textView;
    }
}
final View colorbox = this.findViewById(R.id.edit_cell_dialog_color);
        if (colorbox instanceof Spinner) {
            ColorSpinner list = new ColorSpinner(context, android.R.layout.simple_spinner_item);

            // Load from my database the need items in my case a list of color codes.
            int[] colors = PincodeTextView.this.pincodeDB.getColors();

            int indexOfColor = 0;
            // Add each item into the Spinner.
            for (int i = 0; i < colors.length; i++) {
                list.add(colors[i]);
            }

            ((Spinner) colorbox).setAdapter(list);

        }