Java 为SeparatedListAdapter Android创建自定义阵列适配器
嗨,我正在使用分离的ListAdapter向listView添加节。我试图实现一个定制的数组适配器,通过传入JSONObject的JSONArray并从中向ArrayList添加字符串来填充节中的每个项。但是,当我运行应用程序时,标题可以正常工作,但不会显示每个标题下的任何项目 这是分离的列表适配器Java 为SeparatedListAdapter Android创建自定义阵列适配器,java,android,Java,Android,嗨,我正在使用分离的ListAdapter向listView添加节。我试图实现一个定制的数组适配器,通过传入JSONObject的JSONArray并从中向ArrayList添加字符串来填充节中的每个项。但是,当我运行应用程序时,标题可以正常工作,但不会显示每个标题下的任何项目 这是分离的列表适配器 import java.util.LinkedHashMap; import java.util.Map; import android.content.Context; import andro
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;
}
}
import java.util.LinkedHashMap;
导入java.util.Map;
导入android.content.Context;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Adapter;
导入android.widget.ArrayAdapter;
导入android.widget.BaseAdapter;
公共类分隔ListAdapter扩展了BaseAdapter{
公共最终映射节=新建LinkedHashMap();
公共最终阵列适配器标头;
公共最终静态整型\节\头=0;
公共分隔的ListAdapter(上下文){
headers=newarrayadapter(上下文,R.layout.list_头);
}
public void addSection(字符串段、适配器){
此.headers.add(节);
此.sections.put(节,适配器);
}
公共对象getItem(int位置){
for(对象节:this.sections.keySet()){
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
如果(位置==0)返回段;
if(位置<大小)返回适配器.getItem(位置-1);
//否则跳到下一节
位置-=大小;
}
返回null;
}
public int getCount(){
//所有章节合计,每个章节标题加上一个章节
int-total=0;
对于(适配器:this.sections.values())
总计+=适配器.getCount()+1;
返回总数;
}
public int getViewTypeCount(){
//假设头数为一,然后合计所有部分
整数合计=1;
对于(适配器:this.sections.values())
总计+=适配器。getViewTypeCount();
返回总数;
}
public int getItemViewType(int位置){
int型=1;
for(对象节:this.sections.keySet()){
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
if(position==0)返回类型\u节\u头;
if(位置<大小)返回类型+适配器.getItemViewType(位置-1);
//否则跳到下一节
位置-=大小;
类型+=适配器。getViewTypeCount();
}
返回-1;
}
公共布尔值areAllItemsSelectable(){
返回false;
}
公共布尔值isEnabled(整型位置){
返回(getItemViewType(位置)!=类型\节\标题);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
int sectionnum=0;
for(对象节:this.sections.keySet()){
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
if(position==0)返回headers.getView(sectionnum,convertView,parent);
if(position
这是我的自定义阵列适配器,当我注销它时,它只运行log1=而从不实际注销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;
}
}
公共类ContactArrayAdapter扩展了ArrayAdapter{
//声明项目的ArrayList
私人JSONArray联系人;
私有ArrayList联系人姓名;
私有数组列表访问级别;
私有ArrayList文档修改;
私有ArrayList文件名;
私有字符串颜色;
公共字体我的字体;
/*这里我们必须重写ArrayAdapter的构造函数
*我们现在唯一关心的变量是ArrayList对象,
*因为它是我们要显示的对象列表。
*/
公共联系人ArrayAdapter(上下文上下文、int-layoutResourceId、JSONArray联系人、字符串bgColor、字体){
超级(上下文、布局资源ID);
this.contact=contact;
this.bgColor=bgColor;
我的字体=字体;
Log.v(“CAA”,“1=”);
}
/*
*我们在这里重写getView方法-这就是定义每个
*列表项将显示。
*/
公共视图getView(int位置、视图转换视图、视图组父视图){
Log.v(“CAA”,“2=”);
//将要转换的视图指定为局部变量
视图v=转换视图;
//首先检查视图是否为空。如果是,我们必须将其充气。
//充气基本上意味着渲染或显示视图。
如果(v==null){
LayoutInflater充气器=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u充气器\u SERVICE);
v=充气机。充气(R.layout.document_单元,父单元);
Log.v(“CAA”,“3=”);
}
JSONObject singleContactDict;
对于(int i=0;i您在getview中返回null
。您应该返回一个视图
我还建议您在解析json之前将其添加到列表中,并将列表传递给适配器类的构造函数
你的
这是错误的
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 = ");
}
在<代码> GETVIEW 中使用的<代码>分隔ListAdvult//C> >您是“代码>返回NULL/<代码>。并考虑为您的自定义使用ViWHOWORD模式adapter@Raghunandan感谢您的帮助,我已经编辑了我的自定义阵列适配器以获取父级V
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 = ");
}
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;
}