简体   繁体   中英

Listview android values mixed up in the scroll

I'm using an ArrayAdapter to display items in my Listview, but when i scroll the order of the items change, i guess the pb is in the getView method, I tried to add a viewHolder to resolve this but nothing changed, my Listview still mixed up

Here my adapter :

public class AdpaterFeed extends ArrayAdapter<String> {
    String[] data;
    int layoutResourceId;
    Helper helper;

    public AdpaterFeed(Context context, int layoutResourceId, String[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.data = data;
        this.helper = Helper.getInstance();

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(layoutResourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.txt =(TextView) convertView.findViewById(R.id.text3);
            viewHolder.webview = (WebView) convertView.findViewById(R.id.webview);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (getItem(position) != null) {

            if (getItem(position).contains("<img")) {

                viewHolder.webview.loadData(helper.rss[position], "text/html", "UTF-8");

            } else {

                viewHolder.txt.setText(Html.fromHtml(helper.title[position]));
                viewHolder.webview.loadData(helper.rss[position], "text/html", "UTF-8");
            }
        }

        return convertView;
    }

    public static class ViewHolder {
        TextView txt;
        WebView webview;

    }

}

Do anyone can help me ?

Update: with the OP's precision in the comments, I believe the problem comes from the way the webviews are loaded. Maybe you should find a way to control the loading with AsyncTasks, and make sure you match the right view to the right content.


Maybe your problem lies in the fact that you don't erase the text in the first part of that if:

if (getItem(position).contains("<img")) {
    viewHolder.text.setVisibility(View.GONE); // maybe you want this here?
    viewHolder.webview.loadData(getItem(position), "text/html", "UTF-8");
} else {
    viewHolder.text.setVisibility(View.VISIBLE); // and compensate here
    viewHolder.text.setText(Html.fromHtml(helper.title[position]));
    viewHolder.webview.loadData(getItem(position), "text/html", "UTF-8");
}

If you have different kinds of items (with text or with image), maybe you should take a look at the view types in adapters. This is a very important feature allowing to have different layouts for different types of view.


UNRELATED: Regarding this code:

TextView text = (TextView) convertView.findViewById(R.id.text3);
WebView wv = (WebView) convertView.findViewById(R.id.webview);

viewHolder = new ViewHolder();
viewHolder.txt = text;
viewHolder.webview = wv;

I find it cleaner (and definitely shorter) to skip the local variables:

viewHolder = new ViewHolder();
viewHolder.txt = (TextView) convertView.findViewById(R.id.text3);
viewHolder.webview = (WebView) convertView.findViewById(R.id.webview);

Use the view holder concept as written in below sample...

@Override public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder viewHolder;

    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.row_layout, null);

        TextView programName = (TextView)convertView.findViewById(R.id.programName);
        TextView programTime = (TextView)convertView.findViewById(R.id.programTime);
        TextView programState = (TextView)convertView.findViewById(R.id.programState);
        ImageView programImage =(ImageView)convertView.findViewById(R.id.programImage);



        viewHolder = new ViewHolder();
        viewHolder.p_Name = programName;
        viewHolder.p_Time = programTime;
        viewHolder.p_State = programState;
        viewHolder.p_Image = programImage;

        convertView.setTag(viewHolder);

    }else{

        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.p_Name.setText(values.programNames.get(position));
    viewHolder.p_Time.setText(values.programTimes.get(position));
    new imageDownload(programImage).execute(values.programImageUrls.get(position)); 


    return convertView;
}

And the View holder class look like this

public static class ViewHolder {
 TextView p_Name;
 TextView p_Time;
 TextView p_State;
 ImageView p_Image;

}

Hope this would help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM