1

我遇到的问题很不寻常。我正在为 listview 使用自定义 arrayadapter 类。适合屏幕区域的列表项目正常显示,而需要滚动才能显示的项目要么顺序错误,要么只是列表顶部项目的重复。此外,通过上下滚动,有时项目的顺序会发生变化。

尽管当我通过单击它们来调试故障项目时,所有数据都是正确的。

taskList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
                TaskEntity temp = (TaskEntity) adapter.getItemAtPosition(position);

                Toast.makeText(ScreenMain.this, "test : " + temp.toString(),Toast.LENGTH_LONG).show();
            }
        });

此错误仅发生在 HTC wildfire Platform 2.3.5 Api Level 10 手机中。

但是当我在三星 Galaxy S3 4.1.2 Platform Api Level 16 手机上测试我的应用程序时,一切都很好。

为什么我收到此错误?我的自定义适配器有问题吗?

public class MyTaskListAdapter extends ArrayAdapter<TaskEntity> {

    private final List<TaskEntity> list;
    private final Activity context;

    public MyTaskListAdapter(Activity context, List<TaskEntity> objects) {
        super(context, R.layout.row_task_layout, objects);

        this.context = context;
        this.list = objects;
    }

    static class ViewHolder {
        protected TextView label;
        protected TextView count;
        protected ImageView icon;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.row_task_layout, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.label = (TextView) view.findViewById(R.id.label);
            viewHolder.count = (TextView) view.findViewById(R.id.count);
            viewHolder.icon = (ImageView) view.findViewById(R.id.icon);

            view.setTag(viewHolder);

            ViewHolder holder = (ViewHolder) view.getTag();
            holder.label.setText(list.get(position).title);

            if(Integer.parseInt(list.get(position).tbl_durum)==1)
            {
                holder.icon.setImageResource(R.drawable.check_icon);
            }
            if(Integer.parseInt(list.get(position).child_count)==0)
            {
                holder.count.setVisibility(View.GONE);
            }
            else
            {
                holder.count.setText(list.get(position).child_count);
            }
        } else {
            view = convertView;
        }
        return view;
    }
}
4

1 回答 1

2

尝试这个:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    LayoutInflater inflator = context.getLayoutInflater();
    view = inflator.inflate(R.layout.row_task_layout, null);
    final ViewHolder viewHolder = new ViewHolder();
    viewHolder.label = (TextView) view.findViewById(R.id.label);
    viewHolder.count = (TextView) view.findViewById(R.id.count);
    viewHolder.icon = (ImageView) view.findViewById(R.id.icon);

    view.setTag(viewHolder);

    ViewHolder holder = (ViewHolder) view.getTag();
    holder.label.setText(list.get(position).title);

    if(Integer.parseInt(list.get(position).tbl_durum)==1)
    {
         holder.icon.setImageResource(R.drawable.check_icon);
    }
    if(Integer.parseInt(list.get(position).child_count)==0)
    {
         holder.count.setVisibility(View.GONE);
    }
    else
    {
        holder.count.setText(list.get(position).child_count);
    }

    return view;
}

}

基本上继续使用convertView。这会导致很多这样的问题。如果您的列表中没有那么多对象,那也没关系。

于 2013-05-15T16:23:15.117 回答