好吧,我有一个问题我的微调.它正在填充从webservice中提取的数据.问题在于,当微调器未被单击时,而不是在微调器中显示第一个项目的字符串,而是显示对象引用.
我已经看过相关的问题,但是仍然看不到什么缺失,它是否像一个简单的缺少一个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,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>
如果需要任何更多的信息只是喊.
任何帮助将被大大的配合
编辑:截图