0

我有一个应用程序,我在其中接受诸如没有用户之类的输入,并根据列表视图填充一些修复名称、电话...等。我使用 viewholder 来优化我的所有列表视图。除了加载用户数据的列表视图外,所有列表视图都可以正常工作。

在此处输入图像描述

从图片中您可以看到第一行和最后一行已加载,但中间两行未加载,并且确实需要很长时间才能加载。我提供给每一行的信息最初是相同的,并留给用户修改。这是我使用的代码。

public View getView(final int position, View convertView,final ViewGroup parent)
{
    View rowview = convertView;
    if(rowview == null)
    {
        ViewHolder viewholder = new ViewHolder();
        final LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowview = inflater.inflate(R.layout.list_skeleton_people_expenses, parent, false);
        viewholder.tv1 = (TextView)rowview.findViewById(R.id.name);
        viewholder.tv2 = (TextView)rowview.findViewById(R.id.destination);
        viewholder.tv3 = (TextView)rowview.findViewById(R.id.start);
        viewholder.tv4 = (TextView)rowview.findViewById(R.id.phno);
        viewholder.rowview = rowview;
        rowview.setTag(viewholder);
    }
    else
    {   
        ViewHolder holder = (ViewHolder) rowview.getTag();
        UserData temp = objects.get(position);
        holder.tv1.setText(temp.name);

        holder.tv2.setText("Destination: "+temp.loc);

        holder.tv3.setText("Start: "+start);

        String phone_txt = objects.get(position).phno;
        phone_txt = "("+phone_txt.substring(0, 3)+") "+phone_txt.substring(3, 6)+"-"+phone_txt.substring(6, 10);

        holder.tv4.setText(phone_txt);
    }
    return rowview;
}

static class ViewHolder
{
    TextView tv1,tv2,tv3,tv4;
    View rowview;
}

提前致谢。

4

1 回答 1

2

您需要在 if else 语句之外填充文本视图。中间的那些被第一个案例捕获并且属性没有被填充。

public View getView(final int position, View convertView,final ViewGroup parent)
{
    View rowview = convertView; 
    ViewHolder viewholder;

    if (rowview == null) {
        viewholder = new ViewHolder();
        final LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowview = inflater.inflate(R.layout.list_skeleton_people_expenses, parent, false);
        viewholder.tv1 = (TextView)rowview.findViewById(R.id.name);
        viewholder.tv2 = (TextView)rowview.findViewById(R.id.destination);
        viewholder.tv3 = (TextView)rowview.findViewById(R.id.start);
        viewholder.tv4 = (TextView)rowview.findViewById(R.id.phno);
        viewholder.rowview = rowview;
        rowview.setTag(viewholder);     
    } else {
        viewholder = (ViewHolder) convertView.getTag();
    }

    UserData temp = objects.get(position);

    holder.tv1.setText(temp.name);

    holder.tv2.setText("Destination: "+temp.loc);

    holder.tv3.setText("Start: "+start);

    String phone_txt = objects.get(position).phno;
    phone_txt = "("+phone_txt.substring(0, 3)+") "+phone_txt.substring(3, 6)+"-"+phone_txt.substring(6, 10);

    holder.tv4.setText(phone_txt);
    return rowview;
}

另外,随机建议,尝试使用 StringBuilder 而不是将一堆字符串与“+”运算符连接在一起,因为它的效率很低。每次使用“+”时,Java 都会在后台创建一个新的 StringBuilder 对象以将字符串附加在一起,因为字符串是不可变的。所以不妨只创建一个 StringBuilder 并将要连接的字符串附加到它上面,而不是使用 '+' 四五次并强制它创建更多垃圾收集器必须清理的不必要的对象。

快乐编码!

于 2013-08-02T20:01:35.553 回答