我决定在实现Cursor Adapter
和Cursor Loader
的帮助下填充我的列表视图,因为我期望在我的活动中有大量的列表内容(很多行)。
我现在在做什么
我首先从我的sqlite数据库中获取数据内容,然后通过bindview
内部类中的DotCursorAdapter
方法将其填充到列表视图中,该类扩展了Cursor Adapter
。
其次,我使用Async Task类从我的服务器异步下载图像,并通过imageview
方法将其设置为bindView
。
以上所有工作都正常,但下载到bindview
方法的图像表现得很有趣:执行时,假设要设置的图像,例如list item 1
个交换使用list item 4
并在一段时间后通过将图像设置为其原始列表项目编号恢复正常。
我真的不明白为什么图像设置表现得那样。我已经在这个问题上坐了大约一个星期了,我仍然无法弄清问题是什么。
我上面的工作代码如下,请问我做错了什么或者有更好的过程来做我想做的事情。我很高兴知道。谢谢你的帮助。
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);
}
}
}