ListFragment 错误
Posted
技术标签:
【中文标题】ListFragment 错误【英文标题】:ListFragment error 【发布时间】:2013-01-20 17:38:38 【问题描述】:我在“listview”中有一个列表,可以使用这个混乱滚动列表, 分组,列表有一个带有图标的标题。
公共类 MyCustomAdapter 扩展 BaseAdapter
private static final String ASSETS_DIR = "images/";
private static final int TYPE_HEAD = -1;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private Context ctx;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter(Context context)
this.ctx = context;
mInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public void addItem(final String item)
mData.add(item);
notifyDataSetChanged();
public void addSeparatorItem(final String item)
mData.add(item);
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
public void addHeadItem()
mData.add("");
mSeparatorsSet.add(0);
notifyDataSetChanged();
@Override
public int getCount()
return mData.size();
//return equipos.size();
@Override
public String getItem(int position)
return mData.get(position) ;
@Override
public long getItemId(int position)
// TODO Auto-generated method stub
return position;
@Override
public int getItemViewType(int position)
if (position==0)
return TYPE_HEAD;
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
@Override
public int getViewTypeCount()
return TYPE_MAX_COUNT;
@Override
public View getView(int position, View convertView, ViewGroup parent)
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView
+ " type = " + type);
if (convertView == null)
holder = new ViewHolder();
switch (type)
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.list_item, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
holder.textView3 = (TextView) convertView.findViewById(R.id.textView3);
holder.imageView1 = (ImageView) convertView.findViewById(R.id.imageView1);
String[] datos = mData.get(position).split("-");
holder.textView1.setText(String.format(" %s - %s", datos[0],datos[1]));
holder.textView2.setText(datos[2]);
holder.textView3.setText(datos[3]);
String sel_bandera = datos[4].trim() ;
String imgFilePath = "";
if (sel_bandera.equals("verde"))
imgFilePath = ASSETS_DIR + "circle_green.png" ;
else if (sel_bandera.equals("amarilla"))
imgFilePath = ASSETS_DIR + "circle_yellow.png";
else
imgFilePath = ASSETS_DIR + "circle_red.png";
try
Bitmap bitmap = BitmapFactory.decodeStream(this.ctx.getResources().getAssets().open(imgFilePath));
holder.imageView1.setImageBitmap(bitmap);
//bandera.setImageBitmap(bitmap);
catch (IOException e)
e.printStackTrace();
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.list_group, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.textSeparator);
holder.textView1.setText(mData.get(position));
break;
case TYPE_HEAD:
convertView = mInflater.inflate(R.layout.list_head, null);
break;
convertView.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
return convertView;
public static class ViewHolder
public TextView textView1;
public TextView textView2;
public TextView textView3;
public ImageView imageView1;
public TextView getTextView1()
return textView1;
public void setTextView1(TextView textView1)
this.textView1 = textView1;
public TextView getTextView2()
return textView2;
public void setTextView2(TextView textView2)
this.textView2 = textView2;
public TextView getTextView3()
return textView3;
public void setTextView3(TextView textView3)
this.textView3 = textView3;
public ImageView getImageView1()
return imageView1;
public void setImageView1(ImageView imageView1)
this.imageView1 = imageView1;
公共类 EquiposActivity 扩展 ListFragment 实现 OnTouchListener
public View onCreateView(LayoutInflater inflater, ViewGroup 容器, 捆绑保存的InstanceState) mAdapter = new MyCustomAdapter(getActivity());
if (lista.length > 0)
String[] datos = lista[0].split("-");
cabecera_grupo = datos[4];
mAdapter.addHeadItem();
for (int i = 0; i < lista.length; i++)
String[] datos = lista[i].split("-");
String grupo = datos[4];
if (i == 0)
mAdapter.addSeparatorItem(grupo.replace("_", " "));
if (!grupo.equals(cabecera_grupo))
mAdapter.addSeparatorItem(grupo.replace("_", " "));
cabecera_grupo = grupo;
mAdapter.addItem(String.format("%s - %s - %s - %s - %s",
datos[0], datos[1], datos[2], datos[3], datos[5]));
setListAdapter(mAdapter);
return super.onCreateView(inflater, container, savedInstanceState);
【问题讨论】:
有什么问题?什么错误? 报错是滚动列表时出现乱码 【参考方案1】:我建议你重写你的 getView() 方法,因为我认为你错误地使用了 ViewHolder 模式。阅读:http://www.jmanzano.es/blog/?p=166 或者干脆去掉 ViewHolder 并在没有它的情况下编写 getView() 代码。
【讨论】:
以上是关于ListFragment 错误的主要内容,如果未能解决你的问题,请参考以下文章
在 Android 上的 ListFragment 中使用 ArrayAdapter 获取构造函数错误
ListFragment:运行时错误“您的内容必须有一个 id 属性为 'android.R.list' 的 ListView