我有一个应用程序,我接受输入,例如没有用户,并在列表视图方面填充一些修复名称,电话等。我使用了viewholder来优化我的所有列表视图。除了列表视图加载userdata之外,所有列表视图都可以正常工作。
从图中可以看出,第一行和最后一行都已加载,但中间的两行未加载,并且需要花费大量时间才能加载。我提供给每一行的信息最初是相同的,留给用户修改。这是我用于此的代码。
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;
}
提前致谢。
答案 0 :(得分: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并在其上附加要连接的字符串,而不是使用“+”四到五次,并强制它创建垃圾收集器必须清理的更多不必要的对象。
快乐的编码!