好了,所以我有我的微调一个问题。 它被填充有来自web服务的数据拉。 遇到的问题IM是,当旋转器不代替点击表示在旋转器的第一个项目的字符串其示出,而不是为它的对象引用。
我看过相关的问题,但还是没能看到我在想念,是化繁为简缺少一个toString参考?
下面是用于填充微调代码
private void buildDrinkDropDown() {
List<NameValuePair> apiParams = new ArrayList<NameValuePair>(1);
apiParams.add(new BasicNameValuePair("call", "drinkList"));
bgt = new BackGroundTask(MAP_API_URL, "GET", apiParams);
try {
JSONArray drinks = bgt.execute().get();
for (int i = 0; i < drinks.length(); i++) {
JSONObject d = drinks.getJSONObject(i);
String id = d.getString(TAG_ID_DRINK);
String createdAt = d.getString(TAG_CREATED_AT);
String updatedAt = d.getString(TAG_UPDATED_AT);
String price = d.getString(TAG_PRICE);
String name = d.getString(TAG_NAME);
drinkList.add(new Drink( createdAt ,id, name, price,updatedAt ));
}
drinkField = (Spinner) findViewById(R.id.countryField);
DrinkAdapter dAdapter = new DrinkAdapter(this, android.R.layout.simple_spinner_item, drinkList);
drinkField.setAdapter(dAdapter);
drinkField.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Drink selectedDrink = drinkList.get(position);
GlobalDrinkSelected = drinkList.get(position).getId().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
} catch (JSONException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
下面是适配器类的代码
package com.android.main;
import java.util.ArrayList;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class DrinkAdapter extends ArrayAdapter<Drink>
{
private Activity context;
ArrayList<Drink> data = null;
public DrinkAdapter(Activity context, int resource, ArrayList<Drink> data)
{
super(context, resource, data);
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{ // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item
return super.getView(position, convertView, parent);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // This view starts when we click the spinner.
View row = convertView;
if(row == null)
{
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.dropdown_value_id, parent, false);
}
Drink item = data.get(position);
String test = item.getName();
Log.d("test ", test);
if(item != null)
{
TextView drinkName = (TextView) row.findViewById(R.id.item_value);
if(drinkName != null){
drinkName.setText(item.getName());
Log.d("find me ", drinkName.toString());
}
}
return row;
}
}
下面是在适配器中使用dropdown_value_id布局这就是在xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/item_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
如果需要更多的信息,就骂。
任何帮助将大大apprieciated
编辑:截图