第一个ListView中的项在Cursor加载器中重复

时间:2015-10-06 11:56:02

标签: android

请我从我的本地数据库加载数据,并使用Cursor加载程序将图像从我的服务器下载到ListView

一切运作良好,但问题是图像不正确地互相替换,稍后(在几秒钟内)返回正常(正确)顺序。

例如,如果ListView有四个项目(项目A,B,C和D)带有图像,则在创建图像时会发生变化,因此项目A的图像将替换项目D的图像和项目图像B将替换项目C的图像。

以下是我的尝试。

    public class Notices extends NavigationDrawer implements LoaderCallbacks<Cursor>  {
        DotCursorAdapter mAdapter;
        private ListView lv;
         Context context = this;
         private  final int LOADER_ID = 1890;
         public static DatabaseHandler dbHelper;


        public String msgImageUrl;
        public String senderImageUrl;

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

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

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

             String mTitle = "Notices";
             getSupportActionBar().setTitle(mTitle);

             msgImageUrl = "http://akobima.newsysstm.com/images/";




         }


         @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.fetchAllNotices();

                 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.notices, parent, false);
              }

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



                 //This downloads the msg image
                 if(msgImageName == null || msgImageName.length() == 0|| msgImageName.equalsIgnoreCase("null")){
                     msgImageView.getLayoutParams().height = 0;
                     msgImageView.requestLayout();

                 }else{
                     //instantiate the the downlaod image task
                     new DownloadImageTask(msgImageView).execute(msgImage);
                     msgImageView.getLayoutParams().height = 170;
                     msgImageView.requestLayout();
                 }


             }

            }


        /**
         * This sub class downloads the attached message image
         * file to be viewed on the page. 
         */
        private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
            ImageView bmImage;

            public DownloadImageTask(ImageView bmImage) {
                this.bmImage = bmImage;
            }

            protected Bitmap doInBackground(String... urls) {
                String urldisplay = urls[0];
                Bitmap mIcon11 = null;
                try {
                    InputStream in = new java.net.URL(urldisplay).openStream();
                    mIcon11 = BitmapFactory.decodeStream(in);
                } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return mIcon11;
            }

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

    }







    /**
     * This is the xml list view
     */
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


       <ListView
           android:id="@+id/lists"
           android:layout_height="match_parent"
           android:layout_width="match_parent"
           android:fontFamily="sans-serif-condensed">
        </ListView>


    </RelativeLayout>

请问我做错了什么?谢谢你的帮助。

更新

使用LoaderCallbacks是否是一个错误,因为我已经尝试了所有可能的方法,但它仍然无效。如果有人冷,请我确认这是一个错误或其他方面。先感谢您。

1 个答案:

答案 0 :(得分:0)

尝试更改

mAdapter.swapCursor(data) 

to

mAdapter.changeCursor(cursor) 

and

mAdapter.swapCursor(null) 

to

mAdapter.changeCursor(null)