【问题标题】:ListView items repeated issueListView 项目重复问题
【发布时间】:2013-08-13 00:27:20
【问题描述】:

我有一个 multicolumn_listview。某些行在列表视图中重复。我查看了有关此问题的所有解决方案。

listview items image

  1. Listview 高度不是 wrap_content
  2. 我的持有人类静态
  3. 我在 getview 方法中创建了 if(convertView==null) 块中的所有视图。

但是问题还没有解决..请帮帮我..

这是我的适配器类;

public class ListViewAdapterCurrentList extends BaseAdapter
{
    public ArrayList<HashMap<String, String>> list;
    boolean isDetail = false;
    private String currentNo;
    private String currentCode;
    private String currentName;
    private String date;
    private String status;
    private Activity activity;
    private Boolean isPlan = true;

    public ListViewAdapterCurrentList(Activity activity, ArrayList<HashMap<String, String>> list, boolean isPlan) {
        super();
        this.activity = activity;
        this.list = list;
        this.isPlan = isPlan;
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public Object getItem(int position)
    {
        return list.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return 0;
    }

    static class ViewHolder
    {
        ImageView currentDetail;
        TextView currentNo;
        TextView currentCode;
        TextView currentName;
        TextView date;
        TextView durum;
        LinearLayout linearLayoutCurrentBase;
        LinearLayout linearLayoutCurrentDetail;
        TextView currentAddress;
        TextView currentPhone;
        TextView currentManager;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final ViewHolder holder;
        LayoutInflater inflater = activity.getLayoutInflater();
        final HashMap<String, String> map = list.get(position);

        if (convertView==null)
        {
            Log.d("CL:getView()", "Fetching Row: " + position);
            convertView = inflater.inflate(R.layout.currentlistitems, parent, false);
            holder = new ViewHolder();
            holder.currentDetail = (ImageView) convertView.findViewById(R.id.imageViewCurrentListDetail);
            holder.currentNo = (TextView) convertView.findViewById(R.id.textViewCurrentListNo);
            holder.currentCode = (TextView) convertView.findViewById(R.id.textViewCurrentListCode);
            holder.currentName = (TextView) convertView.findViewById(R.id.textViewCurrentListName);
            holder.date = (TextView) convertView.findViewById(R.id.textViewCurrentListDate);
            holder.durum = (TextView) convertView.findViewById(R.id.textViewCurrentListStatus);
            holder.linearLayoutCurrentBase = (LinearLayout) convertView.findViewById(R.id.layoutBaseCurrentList);
            holder.linearLayoutCurrentDetail = (LinearLayout) convertView.findViewById(R.id.layoutDetailCurrentList);
            holder.currentAddress = (TextView) convertView.findViewById(R.id.textViewCurrentAddress);
            holder.currentPhone = (TextView) convertView.findViewById(R.id.textViewCurrentPhone);
            holder.currentManager = (TextView) convertView.findViewById(R.id.textViewCurrentManager);
            convertView.setTag(holder);
        } else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        currentNo = map.get("CariNo");
        currentCode = map.get("CariKod");
        currentName = map.get("CariUnvan");
        date = map.get("Tarih");
        status = map.get("Durum");
        holder.currentDetail.setBackgroundResource(R.drawable.box_plus);
        holder.currentNo.setText(currentNo);
        holder.currentCode.setText(currentCode);
        holder.currentName.setText(currentName);
        holder.date.setText(date);
        holder.durum.setText(status);
        if (!isPlan)
        {
            holder.durum.setVisibility(View.GONE);
            holder.date.setVisibility(View.GONE);
            holder.currentName.setWidth(300);
        }
        initDetailInfo(holder.currentAddress, holder.currentPhone, holder.currentManager, currentNo);

        holder.currentDetail.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (!isDetail)
                {
                    holder.linearLayoutCurrentDetail.setVisibility(View.VISIBLE);
                    holder.currentDetail.setBackgroundResource(R.drawable.box_delete_expand);
                    isDetail = true;
                } else
                {
                    holder.linearLayoutCurrentDetail.setVisibility(View.GONE);
                    holder.currentDetail.setBackgroundResource(R.drawable.box_plus);
                    isDetail = false;
                }
            }
        });

        return convertView;
    }

【问题讨论】:

  • 你的 initDetailInfo() 方法是做什么的?
  • 不相关的问题:为什么要在重写一半的arrayadapter方法时使用baseadapter?
  • 我倾向于认为最终持有人可能是相关的,但不确定。另外,为什么不在列表视图上使用 onItemClickListener?
  • 你确定你的arraylist的内容吗?
  • phil, initDetailinfo() 只带一些sql查询,所以不需要写有问题...

标签: android android-listview multiple-columns convertview


【解决方案1】:

已编辑,因为它显然不清楚:

if (convertView==null)
{
    Log.d("CL:getView()", "Fetching Row: " + position);
    convertView = inflater.inflate(R.layout.currentlistitems, parent, false);
    holder = new ViewHolder();
    holder.currentDetail = (ImageView) convertView.findViewById(R.id.imageViewCurrentListDetail);
    holder.currentNo = (TextView) convertView.findViewById(R.id.textViewCurrentListNo);
    holder.currentCode = (TextView) convertView.findViewById(R.id.textViewCurrentListCode);
    holder.currentName = (TextView) convertView.findViewById(R.id.textViewCurrentListName);
    holder.date = (TextView) convertView.findViewById(R.id.textViewCurrentListDate);
    holder.durum = (TextView) convertView.findViewById(R.id.textViewCurrentListStatus);
    holder.linearLayoutCurrentBase = (LinearLayout) convertView.findViewById(R.id.layoutBaseCurrentList);
    holder.linearLayoutCurrentDetail = (LinearLayout) convertView.findViewById(R.id.layoutDetailCurrentList);
    holder.currentAddress = (TextView) convertView.findViewById(R.id.textViewCurrentAddress);
    holder.currentPhone = (TextView) convertView.findViewById(R.id.textViewCurrentPhone);
    holder.currentManager = (TextView) convertView.findViewById(R.id.textViewCurrentManager);
    convertView.setTag(holder);
} else
{
    holder = (ViewHolder) convertView.getTag();
}

在对他们进行任何工作之前就在上面,比如将孩子添加到LinearLayouts:

holder.linearLayoutCurrentBase.removeAllViews();
holder.linearLayoutCurrentDetail.removeAllViews();

我的猜测是,由于您没有对 Viewgroups 执行此操作,因此它永远不会替换子项。让我知道这是否有帮助。

编辑:

我只是希望最后一次编辑答案。对于使用错误的方法删除 ViewGroups 的子项,我深表歉意。现在它应该是有道理的。查看我的评论以了解为什么它会重复您的视图。

【讨论】:

  • 我不明白你的回答
  • 我不明白这样做有什么意义。视图持有者的整个想法就是不必重新搜索视图。
  • @njzk2 这是我的错误,但上面的代码可以工作。由于用户永远不会取消 ViewGroups 中的内容,因此当 Views 被回收时它仍然存在。当然它只发生在 ViewGroups 上。将其留在 if 语句中是我的错误。如果在对视图本身进行任何工作之前立即完成,它将确保它们为空以供使用。
  • 我很抱歉 babeyh 不清楚。上面的代码应该可以解决您复制这些视图的问题。
  • it remains there 这就是 viewholder 模式的全部意义(并且不明白为什么它只会发生在 viewgroups 上。)。无论如何,SO 在点击之前不会使用这些。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-09
  • 2014-08-20
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多