2014-01-05 63 views
0

嗨我正在使用SeparatedListAdapter將部分添加到listView。我試圖實現一個自定義數組適配器來填充該部分中的每個項目,方法是傳入JSONObjects的JSONArray,然後將字符串添加到ArrayList中。但是,當我運行應用程序的標頭工作正常,但它不顯示每個標題下的任何項目。爲SeperatedListAdapter創建自定義數組適配器Android

繼承人的SeparatedListAdapter

import java.util.LinkedHashMap; 
import java.util.Map; 

import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Adapter; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 

public class SeparatedListAdapter extends BaseAdapter { 

    public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>(); 
    public final ArrayAdapter<String> headers; 
    public final static int TYPE_SECTION_HEADER = 0; 

    public SeparatedListAdapter(Context context) { 
     headers = new ArrayAdapter<String>(context, R.layout.list_header); 
    } 

    public void addSection(String section, Adapter adapter) { 
     this.headers.add(section); 
     this.sections.put(section, adapter); 
    } 

    public Object getItem(int position) { 
     for(Object section : this.sections.keySet()) { 
      Adapter adapter = sections.get(section); 
      int size = adapter.getCount() + 1; 

      // check if position inside this section 
      if(position == 0) return section; 
      if(position < size) return adapter.getItem(position - 1); 

      // otherwise jump into next section 
      position -= size; 
     } 
     return null; 
    } 

    public int getCount() { 
     // total together all sections, plus one for each section header 
     int total = 0; 
     for(Adapter adapter : this.sections.values()) 
      total += adapter.getCount() + 1; 
     return total; 
    } 

    public int getViewTypeCount() { 
     // assume that headers count as one, then total all sections 
     int total = 1; 
     for(Adapter adapter : this.sections.values()) 
      total += adapter.getViewTypeCount(); 
     return total; 
    } 

    public int getItemViewType(int position) { 
     int type = 1; 
     for(Object section : this.sections.keySet()) { 
      Adapter adapter = sections.get(section); 
      int size = adapter.getCount() + 1; 

      // check if position inside this section 
      if(position == 0) return TYPE_SECTION_HEADER; 
      if(position < size) return type + adapter.getItemViewType(position - 1); 

      // otherwise jump into next section 
      position -= size; 
      type += adapter.getViewTypeCount(); 
     } 
     return -1; 
    } 

    public boolean areAllItemsSelectable() { 
     return false; 
    } 

    public boolean isEnabled(int position) { 
     return (getItemViewType(position) != TYPE_SECTION_HEADER); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     int sectionnum = 0; 
     for(Object section : this.sections.keySet()) { 
      Adapter adapter = sections.get(section); 
      int size = adapter.getCount() + 1; 

      // check if position inside this section 
      if(position == 0) return headers.getView(sectionnum, convertView, parent); 
      if(position < size) return adapter.getView(position - 1, convertView, parent); 

      // otherwise jump into next section 
      position -= size; 
      sectionnum++; 
     } 
     return null; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 



} 

這裏是我的自定義陣列適配器,當我登錄出來它只能運行日誌1 =實際上從未註銷2 =這表明,我認爲我的自定義陣列適配器ISN」不明白爲什麼。

public class ContactArrayAdapter extends ArrayAdapter<String> { 



    // declaring our ArrayList of items 
    private JSONArray contact; 
    private ArrayList<String> contactName; 
    private ArrayList<String> accessLevel; 
    private ArrayList<String> docModified; 
    private ArrayList<String> fileName; 
    private String bgColor; 
    public Typeface myTypeFace; 
    /* here we must override the constructor for ArrayAdapter 
    * the only variable we care about now is ArrayList<Item> objects, 
    * because it is the list of objects we want to display. 
    */ 
    public ContactArrayAdapter(Context context, int layoutResourceId, JSONArray Contact, String bgColor,Typeface font) { 
     super(context, layoutResourceId); 
     this.contact = Contact; 
     this.bgColor = bgColor; 
     myTypeFace = font; 
     Log.v("CAA", " 1 = "); 
    } 

    /* 
    * we are overriding the getView method here - this is what defines how each 
    * list item will look. 
    */ 
    public View getView(int position, View convertView, ViewGroup parent){ 
     Log.v("CAA", " 2 = "); 
     // assign the view we are converting to a local variable 
     View v = convertView; 

     // first check to see if the view is null. if so, we have to inflate it. 
     // to inflate it basically means to render, or show, the view. 
     if (v == null) { 
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.document_cell, parent); 
      Log.v("CAA", " 3 = "); 
     } 

     JSONObject singleContactDict; 

     for (int i=0; i<contact.length(); i++){ 
      Log.v("CAA", " 4 = "); 
      Log.v("CAA", " contact = " + contact); 
      try { 
       singleContactDict = contact.getJSONObject(i); 

      Log.v("CAA", "Contact singleContactDict " + i +"= " + singleContactDict); 
      contactName.add(singleContactDict.getString("first_name") + " " + singleContactDict.getString("last_name")); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }   
     } 
     /* 
     * Recall that the variable position is sent in as an argument to this method. 
     * The variable simply refers to the position of the current object in the list. (The ArrayAdapter 
     * iterates through the list we sent it) 
     * 
     * Therefore, i refers to the current Item object. 
     */ 
     String title = contactName.get(position); 
     String types = accessLevel.get(position); 
     String modified = docModified.get(position); 
     String extension = fileName.get(position); 

     Log.v("CAA","DocumentArrayAdapter, = " + title); 

     if (title != null) { 

      // This is how you obtain a reference to the TextViews. 
      // These TextViews are created in the XML files we defined. 

      TextView docTitle = (TextView) v.findViewById(R.id.name); 
      docTitle.setTypeface(myTypeFace); 
      docTitle.setTextColor(Color.parseColor(bgColor)); 
      TextView docType = (TextView) v.findViewById(R.id.doctype); 
      docType.setTypeface(myTypeFace); 
      TextView docMod = (TextView) v.findViewById(R.id.modified); 
      docMod.setTypeface(myTypeFace); 

      ImageView docImage = (ImageView) v.findViewById(R.id.docicon); 



      // check to see if each individual textview is null. 
      // if not, assign some text! 
      if (docTitle != null){ 
       docTitle.setText(title); 
      } 
//   if (accessLevel != null){ 
//    docType.setText(types); 
//   } 



    } 
     return v; 
    } 

} 
+0

是什麼'SeparatedListAdapter'在'getView'你'返回null'用於有。並考慮爲您的自定義適配器使用一個ViewHolder模式 – Raghunandan

+0

@Raghunandan感謝您的幫助我編輯了我的自定義數組適配器來獲得父ViewGroup而不是null,但它仍然不顯示項目只是標題 –

回答

1

您在012view中的return null。你應該返回一個視圖,而不是

此外,我會建議你解析你json之前,並將其添加到列表。將列表傳遞給適配器類的構造函數。

super(context, layoutResourceId); 

是錯誤的

List<type> list 
public ContactArrayAdapter(Context context, int layoutResourceId, List<type> Contact, String bgColor,Typeface font) { 
    super(context, layoutResourceId,Contact); 
    this.list =Contact; 
    this.bgColor = bgColor; 
    myTypeFace = font; 
    Log.v("CAA", " 1 = "); 
} 

然後用ViewHolder模式

public View getView(int position, View convertView, ViewGroup parent){ 
    ViewHolder holder; 
    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.document_cell,parent,false); 
     holder = new ViewHolder(); 
     holder.docTitle = (TextView) v.findViewById(R.id.name); 
     holder.docType = (TextView) v.findViewById(R.id.doctype); 
     holder.docMod = (TextView) v.findViewById(R.id.modified); 
     holder.docImage = (ImageView) v.findViewById(R.id.docicon); 
     convertView.setTag(holder); 
    } 
    else 
    { 
      holder =(ViewHolder) convertView.getTag();  
    } 
     // with list update ui here 

     holder.docTitle.setTypeface(myTypeFace); 
     holder.docTitle.setTextColor(Color.parseColor(bgColor)); 
     holder.docType.setTypeface(myTypeFace); 
     holder.docMod.setTypeface(myTypeFace); 

    return convertView; 
} 

static class ViewHolder 
{ 
     TextView docTitle,docStyle,docType,docMod; 
     ImageView docImage; 
} 
+0

謝謝你這工作 –

+0

@LukeBatley不用謝。更多信息@ http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works和這個http://developer.android.com/training/improving-layouts/smooth-scrolling.html – Raghunandan