0

是否可以從@android:id/text1膨脹Textview? 我不想創建自己的佈局,我只想得到一些修改後的文本。可能會膨脹由ActionBarSherlock擴展的android微調器佈局?

這裏是我的代碼:

首先,我創建變量來存儲數據

private List<HashMap<String, String>> dataCities = new ArrayList<HashMap<String, String>>(); 
//Hashmap with keys and values 
//id - 0 
//name - default 

其次,我的onCreate

創建自定義適配器
@Override 
protected void onCreate(Bundle savedInstanceState) { 
//... 
Spinner citiesSpinner = (Spinner) findViewById(R.id.city_sp); 
citiesAdapter = new CustomArrayAdapter(this, R.layout.sherlock_spinner_item, dataCities); 
citiesSpinner.setAdapter(citiesAdapter); 
} 

第三,我建立了我的聽衆。它的工作原理,但調用notifyDataSetChanged後沒有任何反應。爲什麼?

@Override 
public void onRequestJsonResponded(RequestType type, JSONArray array) { 
    //my enum type 
    switch (type) { 
     case cities: 
      //returns hashmap in arraylist, ArrayList<HashMap<String,String>> ... 
      dataCities = parseJSonArray(array); 
      Log.d(TAG, "End of parsing"); 
      citiesAdapter.notifyDataSetChanged(); 
      break; 
     case mark: 
      //... 
      break; 
     case model: 
      break; 
    } 
} 

這裏是我的自定義arrayadapter

private class CustomArrayAdapter extends ArrayAdapter<HashMap<String, String>> { 

    public CustomArrayAdapter(Context context, int textViewResourceId, List<HashMap<String, String>> objects) { 
     super(context, textViewResourceId, objects); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater layoutInflater = LayoutInflater.from(getContext()); 
     View v = layoutInflater.inflate(R.layout.sherlock_spinner_item, null); 
     TextView tv = (TextView)v.findViewById(android.R.id.text1); 
     tv.setText(getItem(position).get("name")); 

     return v; 

    } 

} 

有人能告訴我爲什麼我得到空飛旋的數據? (Spinner是空的)。以及如何在不創建新佈局的情況下修改文本?我只想使用sherlock微調器項目佈局。請幫忙。

回答

0

簡單,在你的適配器列表是不一樣的,您檢索到的列表:

 //In here you update your activity's list to the returned values 
     dataCities = parseJSonArray(array); 
     Log.d(TAG, "End of parsing"); 
     //But the adapter is using the original value of dataCities (new ArrayList<HashMap<String, String>>()) 
     citiesAdapter.notifyDataSetChanged(); 

由於您的適配器取決於ArrayAdapter,最簡單的解決辦法是隻創建一個新的適配器時數據收到。

+0

拜託,你能告訴我爲什麼notifyDataSetChanged在這裏不起作用嗎? – deadfish

+0

Errr「簡單,適配器中的列表與您檢索的列表不同。」它在代碼片段中有深入的解釋嗎? – dmon