2012-10-31 40 views
0

getView不呈現我正在處理android ListView和我面臨奇怪的錯誤當我充氣ListItem。即使數據已在LogCat中顯示,我也無法在getView方法中看到我的數據。有任何想法嗎 ?即使數據顯示在LogCat

private ListViewAdapter m_adapter; 
public ArrayList<String> cat = new ArrayList<String>(); 
private SharedPreferences preferences; 
private SharedPreferences.Editor editor; 
String title, character, thumbnail, totalPages; 
int Code; 
ViewHolder vh; 
TextView titleView, charView, totalView; 
ImageView thumbView, removeView; 
Uri thumbUri; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setTheme(android.R.style.Theme_Black_NoTitleBar); 
    getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.myputet); 
    init_UI(); 
} 

public void init_UI() { 
    preferences = getApplicationContext().getSharedPreferences(
      Constants.SHARED_PRED, 0); 
    editor = preferences.edit(); 

    title = preferences.getString("TITLE", ""); 
    character = preferences.getString("CHAR", ""); 
    thumbnail = preferences.getString("THUMB", ""); 
    thumbUri = Uri.parse(thumbnail); 
    totalPages = preferences.getString("TOTAL", ""); 
    Code = preferences.getInt("CODE", 0); 

    System.out.println("Getting Title " + title); 
    System.out.println("Getting character " + character); 
    System.out.println("Getting thumbnail " + thumbnail); 
    System.out.println("Getting thumbUri " + thumbUri); 
    System.out.println("Getting totalPages " + totalPages); 

    Log.v("ArrayList", cat.toString()); 
    ListView lv = (ListView) findViewById(R.id.listview); 

    m_adapter = new ListViewAdapter(this, R.layout.myputet_item, cat); 
    lv.setAdapter(m_adapter); 
} 

private class ListViewAdapter extends ArrayAdapter<String> { 
    private ArrayList<String> items; 

    public ListViewAdapter(Context context, int textViewResourceId, 
      ArrayList<String> items) { 
     super(context, textViewResourceId, items); 
     this.items = items; 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.myputet_item, null); 
     } 

     Log.v("Title View", title); 
     titleView = (TextView) v.findViewById(R.id.titleTxtView); 
     charView = (TextView) v.findViewById(R.id.charTxtView); 
     totalView = (TextView) v.findViewById(R.id.totalTxtView); 
     thumbView = (ImageView) v.findViewById(R.id.charImageView); 
     removeView = (ImageView) v.findViewById(R.id.removeListItem); 
     if (titleView != null) { 
      titleView.setText(title); 
      Log.v("Update Title", title); 
     } 
     charView.setText(character); 
     Log.v("Update character", character); 
     totalView.setText(totalPages); 
     Log.v("Update totalPages", totalPages); 
     thumbView.setImageURI(thumbUri); 

     return convertView; 
    } 
} 

LogCat輸出

10-31 23:52:34.438: V/Title View(4568): မိခ်ိဳေစ်းကအျပန္ 
10-31 23:52:34.438: V/Update Title(4568): မိခ်ိဳေစ်းကအျပန္ 
10-31 23:52:34.438: V/Update character(4568): ကိုေရႊထူး(ျပည္) 
10-31 23:52:34.438: V/Update totalPages(4568): 3 
+0

http://www.youtube.com/watch?v=wDBM6wVEO70。這應該可以幫助你解決你的問題。 – Raghunandan

回答

2

您需要返回您在getView()修改視圖:

return v; // not convertView 

在另一方面,在你的ListView的每一行是一樣的.. title,character等永不改變,所以它們會一遍又一遍地顯示出來。

最後,請觀看Android的Romain Guy discuss efficient adapters以幫助加速您的速度。

+0

我已經嘗試過使用return v;但它不起作用。有任何想法嗎 ? –

+1

你需要返回v並告訴我們什麼不行。 – Shark

+0

嘗試用'v = vi.inflate(R.layout.myputet_item,parent,false)'膨脹你的視圖;'。 – Sam