嗨我正在使用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;
}
}
是什麼'SeparatedListAdapter'在'getView'你'返回null'用於有。並考慮爲您的自定義適配器使用一個ViewHolder模式 – Raghunandan
@Raghunandan感謝您的幫助我編輯了我的自定義數組適配器來獲得父ViewGroup而不是null,但它仍然不顯示項目只是標題 –