[英]fetching value from database and setting it in android spinner
您好如何從數據庫中獲取數據並將其設置在android app的spinner中。如何開發dis。下面的代碼是將數據從spinner更新為android mysql database。但是我希望需要從數據庫中獲取數據,並且獲取的值是在Android應用程式上顯示在我的Spinner值上。我該如何開發呢?
這是我的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.
}
}
請幫助我..我該如何發展呢。
您是否嘗試過在微調器上使用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;
}
}
然后,我用它來用我的ColorSpinner
加載微調器:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.