为Separated ListAdapter Android创建自定义Arrayadapter

时间:2014-01-05 14:06:36

标签: java android

您好我正在使用SeparatedListAdapter将部分添加到listView。我正在尝试实现一个自定义数组适配器,通过传入JSONArray的JSONArray并从这个添加字符串到ArrayList来填充该部分中的每个项目。但是,当我运行应用程序时,标题工作正常,但它不显示每个标题下的任何项目。

继承人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 =这表明我的自定义数组适配器没有获取视图但我不知道为什么

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;
    }

}

1 个答案:

答案 0 :(得分:1)

你在getview中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; 
}
相关问题