形象地说,在bindview方法设置的行为搞笑 [英] Image that is set in bindview method behaves funny

查看:95
本文介绍了形象地说,在bindview方法设置的行为搞笑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定来填充我的列表视图与执行的帮助下光标适配器光标装载机因为我在我的活动期待大量的列表内容(行)。

我在做什么,现在

  1. 我第一次从我的SQLite数据库获取数据的内容,并将其填充通过在 bindview 方法列表视图我的 DotCursorAdapter 内部类,它扩展了光标适配器

  2. 其次,我asynchronosly使用的异步任务级我的服务器上下载一个图像,并将其设置为的ImageView 通过 bindView 方法。

所有上述工作正常,但是它被下载到 bindview 方法的图像的行为滑稽:在执行时,它是假设的图像被设置为,例如,列表项1 交流与列表项4 之后的某个时候它会回到正常的图像设置为原来的列表项数量。

我真的不明白,为什么图像设置的行为就像这一点。我曾坐在这个问题的一个星期左右了,我仍然无法弄清楚的问题是什么。

我的codeS的工作上面下面,请是不是我做错了,或有更好的方法做我想做的事情。我将不胜感激知道。感谢您的帮助。

 公共类ListViewExample扩展活动实现LoaderCallbacks<光标> {

            DotCursorAdapter mAdapter;
            私人的ListView LV;
            上下文的背景下=这一点;
            私人最终诠释LOID = 500;
            公共静态数据库处理器dbHelper;



            @覆盖
            保护无效的onCreate(包savedInstanceState){
                super.onCreate(savedInstanceState);

                。getLayoutInflater()膨胀(R.layout.list_main_activity,的FrameLayout);
                LV =(ListView控件)findViewById(R.id.lists);


                mAdapter =新DotCursorAdapter(本,空,1);
                。getSupportLoaderManager()initLoader(LOID,空,这一点);
                lv.setAdapter(mAdapter);


                Urlimage =htt​​p://www.mysiteimage.com/;
            }



            @覆盖
            公共装载机<光标> onCreateLoader(INT I,束束){
                返回新DumbLoader(本);
            }
            @覆盖
            公共无效onLoadFinished(装载机<光标> cursorLoader,光标光标){


                mAdapter.swapCursor(光标);


            }
            @覆盖
            公共无效onLoaderReset(装载机<光标> cursorLoader){
                mAdapter.swapCursor(空);
            }


            / **
             * DumbLoader子类
             * /
            公共静态类DumbLoader扩展CursorLoader {


                公共DumbLoader(上下文的背景下){
                    超(上下文);
                }

                @覆盖
                公共光标loadInBackground(){
                    光标C = dbHelper.fetchAllListViewExample();
                    返回℃;

                }
            }

            公共final类DotCursorAdapter扩展的CursorAdapter {

                公共DotCursorAdapter(上下文的背景下,光标指针,INT标志){
                    超级(上下文,光标,0);
                }


                @覆盖
                公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
                    返回LayoutInflater.from(上下文).inflate(R.layout.ListViewExample,父母,假);
                }


                @覆盖
                公共无效bindView(查看视图,上下文的背景下,光标光标){

                    / **
                     *从本地数据库中的图像数据
                     * /
                    最后弦乐imageName = cursor.getString(cursor.getColumnIndexOrThrow(msgTitle));


                    //这是图像URL名称
                    最后弦乐imageFull =(Urlimage + imageName);



                    //现在位置在布局图中的各种视图变量
                    最后的ImageView ImageView的=(ImageView的)view.findViewById(R.id.ciView);


                    //此下载图像并将其设置为视图
                    如果(imageName == NULL || imageName.length()== 0 || imageName.equalsIgnoreCase(空)){
                        imageView.setImageResource(R.drawable.sample);
                    }其他{

                        新URLimageDownload(ImageView的).execute(imageFull);

                    }

                }

            }



            / **
             *此该dowloads图像中的背景的子类
             *使用异步任务。
             * /
            私有类URLimageDownload扩展的AsyncTask<字符串,太虚,位图> {
                ImageView的imageDownloadView;

                公共URLimageDownload(ImageView的imageDownloadView){
                    this.imageDownloadView = imageDownloadView;
                }

                受保护的位图doInBackground(字符串...网址){
                    串urldisplay =网址[0];
                    位图的BitmapImage = NULL;

                    尝试 {
                        InputStream的时间=新的java.net.URL(urldisplay).openStream();
                        BitmapImage的= BitmapFactory.de codeStream(中);
                    }赶上(例外五){
                        Log.e(错误,e.getMessage());
                        e.printStackTrace();
                    }
                    返回的BitmapImage;
                }

                保护无效onPostExecute(位图的结果){
                    imageDownloadView.setImageBitmap(结果);
                }
            }



        }
 

解决方案

意见被回收,从而为您滚动你做一个请求,响应会异步错排。

要解决这个问题,你应该使用像毕加索或的滑翔

它们都非常相似,并且容易使用

所以更换您的bindview这样的事情

  Glide.with(上下文)
    .load(yourUrl)
    。走进(myImageView);
 

I decided to populate my list view with the help of implementing the Cursor Adapter and Cursor Loader since I am expecting a large number of list contents (many rows) on my activity.

What I am doing right now

  1. I first fetch data content from my sqlite database and populate it to the list view through the bindview method in my DotCursorAdapter inner class which extends the Cursor Adapter.

  2. Secondly, I asynchronosly download an image from my servers using the Async Task class and set it to the imageview through the bindView method.

All the above works correctly, but the image which is been downloaded into the bindview method behaves funny: Upon execution, the image which is suppose to be set to the, for example, list item 1 exchanges with list item 4 and after sometime it gets back to normal by setting the image to its original list item number.

I really do not understand why the image setting is behaving like that. I have sat on this problem for about a week now and I still cannot figure out what the problem is.

My codes for the work above is below, please is there something I am doing wrong or there is a better process to do what I want to do. I would be grateful to know. Thanks for helping.

        public class ListViewExample extends Activity implements LoaderCallbacks<Cursor>  {

            DotCursorAdapter mAdapter;
            private ListView lv;
            Context context = this;
            private  final int LOID = 500;
            public static DatabaseHandler dbHelper;



            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                getLayoutInflater().inflate(R.layout.list_main_activity, frameLayout);
                lv = (ListView) findViewById(R.id.lists);


                mAdapter = new DotCursorAdapter(this, null,1);
                getSupportLoaderManager().initLoader(LOID, null, this);
                lv.setAdapter(mAdapter);


                Urlimage = "http://www.mysiteimage.com/";
            }



            @Override
            public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
                return new DumbLoader(this);
            }
            @Override
            public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {


                mAdapter.swapCursor(cursor);


            }
            @Override
            public void onLoaderReset(Loader<Cursor> cursorLoader) {
                mAdapter.swapCursor(null);
            }


            /**
             * DumbLoader sub class
             */
            public static class DumbLoader extends CursorLoader {


                public DumbLoader(Context context) {
                    super(context);
                }

                @Override
                public Cursor loadInBackground() {
                    Cursor c = dbHelper.fetchAllListViewExample();
                    return c;

                }
            }

            public final class DotCursorAdapter extends CursorAdapter {

                public DotCursorAdapter(Context context, Cursor cursor, int flags) {
                    super(context, cursor, 0);
                }


                @Override
                public View newView(Context context, Cursor cursor, ViewGroup parent) {
                    return LayoutInflater.from(context).inflate(R.layout.ListViewExample, parent, false);
                }


                @Override
                public void bindView(View view, Context context, Cursor cursor) {

                    /**
                     * image Data from the local database
                     */
                    final String imageName = cursor.getString(cursor.getColumnIndexOrThrow("msgTitle"));


                    //This is the the image url name 
                    final String imageFull= (Urlimage+imageName);



                    //Place various views variable in the layout view
                    final ImageView imageView= (ImageView) view.findViewById(R.id.ciView);


                    //This downloads the image and set it to its view
                    if(imageName== null || imageName.length() == 0|| imageName.equalsIgnoreCase("null")) {
                        imageView.setImageResource(R.drawable.sample);
                    }else{

                        new URLimageDownload(imageView).execute(imageFull);

                    }

                }

            }



            /**
             * This sub class that dowloads the image in the background
             * using the async task.
             */
            private class URLimageDownload extends AsyncTask<String, Void, Bitmap> {
                ImageView imageDownloadView;

                public URLimageDownload(ImageView imageDownloadView) {
                    this.imageDownloadView = imageDownloadView;
                }

                protected Bitmap doInBackground(String... urls) {
                    String urldisplay = urls[0];
                    Bitmap bitmapImage = null;

                    try {
                        InputStream in = new java.net.URL(urldisplay).openStream();
                        bitmapImage = BitmapFactory.decodeStream(in);
                    } catch (Exception e) {
                        Log.e("Error", e.getMessage());
                        e.printStackTrace();
                    }
                    return bitmapImage;
                }

                protected void onPostExecute(Bitmap result) {
                    imageDownloadView.setImageBitmap(result);
                }
            }



        }

解决方案

Views are recycled, so as you scroll you make one request, and the response will come asynchronously in the wrong row.

To solve this you should use an Image loader like Picasso or Glide.

They are both very similar and easy to use:

So replace your bindview for something like this

Glide.with(context)
    .load(yourUrl)
    .into(myImageView);

这篇关于形象地说,在bindview方法设置的行为搞笑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆