滚动时,Android Studio ListView文本名称会发生​​变化

时间:2015-05-18 11:47:11

标签: android listview

当我向下滚动listView时,文本会更改名称。

List<ParseObject> ob;
listview = (ListView) rootView.findViewById(R.id.listview);
// Pass the results into an ArrayAdapter

ArrayList<Community> buses1 = new ArrayList<Community>();
//adapter = new ArrayAdapter<String>(getActivity(),
//R.layout.listview_item);

// Retrieve object "name" from Parse.com database
for (ParseObject country : ob) {
    Community busLinea1 = new Community();
    busLinea1.setArticolo((String) country.get("name"));
    busLinea1.setNome((String) country.get("Username"));
    busLinea1.setData((String) country.get("dataPubblicazione"));
    buses1.add(busLinea1);
    //adapter.add((String) country.get("name"));

    listview.setAdapter(new CommunityAdapter(getActivity(), buses1));
}

CommunityAdapter

public class CommunityAdapter extends BaseAdapter {

private Activity context_1;

private ArrayList<Community> post;

public CommunityAdapter(Activity context,
                         ArrayList<Community> post) {
    context_1 = context;
    this.post = post;
}


public int getCount() {
    return post.size();
}


public Object getItem(int position) {
    return null;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder1 = null;
    ViewHolder viewHolder2 = null;
    ViewHolder viewHolder3 = null;

    if (convertView == null) {
        convertView = LayoutInflater.from(context_1).inflate(
                R.layout.custom_row_community, null);
        viewHolder1 = new ViewHolder();
        viewHolder2 = new ViewHolder();
        viewHolder3 = new ViewHolder();


        viewHolder1.txt = (TextView) convertView
                .findViewById(R.id.t1);
        viewHolder2.txt = (TextView) convertView
                .findViewById(R.id.t2);
        viewHolder3.txt = (TextView) convertView
                .findViewById(R.id.t3);

        /**
         * At very first time when the List View row Item control's
         * instance is created it will be store in the convertView as a
         * ViewHolder Class object for the reusability purpose
         **/
        convertView.setTag(viewHolder1);
        convertView.setTag(viewHolder2);
        convertView.setTag(viewHolder3);


    } else {
        /**
         * Once the instance of the row item's control it will use from
         * already created controls which are stored in convertView as a
         * ViewHolder Instance
         * */
        viewHolder1 = (ViewHolder) convertView.getTag();
        viewHolder2 = (ViewHolder) convertView.getTag();
        viewHolder3 = (ViewHolder) convertView.getTag();
    }

    viewHolder1.txt.setText(post.get(position).articolo);
    viewHolder2.txt.setText(post.get(position).nome);
    viewHolder3.txt.setText(post.get(position).data);

    return convertView;
}

public class ViewHolder {
    public TextView txt;

}
}

1 个答案:

答案 0 :(得分:0)

这里的ViewHolder概念有点不对。

您应该只为列表中的每个项目使用一个ViewHolder,并且您的ViewHolder应该包含您可能希望在将来更新的每个子视图的一个字段。

像这样:

public class ViewHolder {
  public TextView txt1;
  public TextView txt2;
  public TextView txt3;
}

所以你的getView应该更像这样:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;

    if (convertView == null) {
        convertView = LayoutInflater.from(context_1).inflate(
                R.layout.custom_row_community, null);
        viewHolder = new ViewHolder();

        viewHolder.txt1 = (TextView) convertView
                .findViewById(R.id.t1);
        viewHolder.txt2 = (TextView) convertView
                .findViewById(R.id.t2);
        viewHolder.txt3 = (TextView) convertView
                .findViewById(R.id.t3);


        /**
         * At very first time when the List View row Item control's
         * instance is created it will be store in the convertView as a
         * ViewHolder Class object for the reusability purpose
         **/
        convertView.setTag(viewHolder);

    } else {
        /**
         * Once the instance of the row item's control it will use from
         * already created controls which are stored in convertView as a
         * ViewHolder Instance
         * */
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.txt1.setText(post.get(position).articolo);
    viewHolder.txt2.setText(post.get(position).nome);
    viewHolder.txt3.setText(post.get(position).data);




    return convertView; }

你可以在这里阅读更多关于这种常用于android的模式: http://developer.android.com/training/improving-layouts/smooth-scrolling.html

祝你好运!