gpt4 book ai didi

android - 为 simplecursoradapter 实现延迟加载

转载 作者:行者123 更新时间:2023-11-29 16:08:24 27 4
gpt4 key购买 nike

这里我从数据库中获取存储的图片路径。这样我就可以在路径的帮助下在 gridview 中显示图像。但正如我下面的代码所示,我正在 asynctask 中加载 cursoradapter。这里的问题是第一次加载所有图像需要一些时间是可以的。

但是如果用户再向数据库中添加一张图片,加载所有图片又会花费很多时间,因为我将 asynctask 保留在 onResume() 中以进行添加图片也可见。每当用户添加每张图片时都是如此。有人可以建议我一个简单的方法来让最后添加的图片在 gridview 中可见,而不必再次加载所有图片。或者至少在不可能的情况下,有人可以帮助我实现当前 cursoradapter 的惰性适配器吗?

    public class ImagesScreen extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.images_screen);
gridView = (GridView)findViewById(R.id.gridView1);
String[] from = new String[] { Reminders.MOM_PATHS };
int[] to = new int[] {};
dbCon = new SQLiteConnectClass(ImagesScreen.this);

imageCursorAdapter = new ImageCursorAdapter(ImagesScreen.this, R.layout.griditem, null, from, to);
gridView.setAdapter(imageCursorAdapter);

}

protected void onResume() {
super.onResume();
new GetImages().execute((Object[]) null);
}

private class GetImages extends AsyncTask<Object, Object, Cursor> {

@Override
protected Cursor doInBackground(Object... params) {
return dbCon.getAllImages();
}

@Override
protected void onPostExecute(Cursor result) {
imageAdapter.changeCursor(result);
}

public void addpictures(View view){
// code for adding picture paths to the database by transferring to another activity.
}


}

自定义适配器类:

public class ImageCursorAdapter extends SimpleCursorAdapter{

private int layout;
private LayoutInflater mLayoutInflater;
private Context mContext;

public ImageAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
super(context, layout, c, from, to,0);
this.layout = layout;
mLayoutInflater=LayoutInflater.from(context);
mContext = context;
}

public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(layout, parent, false);
return v;
}

public void bindView(final View v, final Context context, Cursor c) {
final int id = c.getInt(c.getColumnIndex("id"));
final String imagepath = c.getString(c.getColumnIndex("paths"));


ImageView imageview = (ImageView)v.findViewById(R.id.imageView1);

File imgFile = new File(imagepath);
if(imgFile.exists()){

Bitmap imageBitmap = decodeFile(imgFile);
imageview.setImageBitmap(imageBitmap);
}

}
}

最佳答案

使用此代码

public class Imagegallery extends Activity implements OnItemClickListener,
OnScrollListener, OnTouchListener {
Button btn;

SQLiteDatabase sampleDB = null;
String SAMPLE_DB_NAME = "hic";

int size = 0;
TextView headertext;
Button cartbtn;

ImageView footer1, footer2, footer3, footer4, footer5;

GridView gridView;

boolean larg = false;
String products;
int j = 1;
int ii = 0;
String imagepath[];
String productname[];
int productid[] = new int[1000];

String productids[] = new String[1000];
int integerprodids[] = new int[1000];
final Context context = this;
String filename2[];

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currentspecial);

File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "aiwhic/product" + File.separator);
File[] fileName = root.listFiles();
filename2 = new String[fileName.length];
for (int j = 0; j < fileName.length; j++) {
Uri uri = Uri.fromFile(fileName[j]);
filename2[j] = fileName[j].getAbsolutePath();

Log.e("file", filename2[j]);
}

gridView = (GridView) findViewById(R.id.gridView1);

gridView.setAdapter(new ImageAdapter(this, filename2, filename2));

gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {

int isf = filename2[position].lastIndexOf("/");
int laf = filename2[position].lastIndexOf(".");
String filename = filename2[position].substring(isf + 1, laf);
int pos = Integer.parseInt(filename);
Intent go = new Intent(Imagegallery.this, ItemsPage.class);
Bundle bundle = new Bundle();

bundle.putInt("position", pos);
bundle.putString("whichclass", "imagegallery");
bundle.putInt("catid", 2);
go.putExtras(bundle);
startActivity(go);

}
});

}

public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
private final String[] mobileimages;

public ImageAdapter(Context context, String[] mobileValues, String[] mo) {
this.context = context;
this.mobileValues = mobileValues;
this.mobileimages = mo;
}

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

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View gridView;

if (convertView == null) {

gridView = new View(context);

gridView = inflater.inflate(R.layout.currentspeciallist, null);

TextView textView = (TextView) gridView
.findViewById(R.id.textView1);
textView.setText(mobileValues[position]);
textView.setVisibility(View.INVISIBLE);
ImageView imageView = (ImageView) gridView
.findViewById(R.id.imageView1);

imageView.setTag(mobileimages[position]);
new Loadimage().execute(imageView);
// imageView.setImageBitmap(BitmapFactory
// .decodeFile(mobileimages[position]));

} else {
gridView = (View) convertView;
}

return gridView;
}

public int getCount() {
return mobileimages.length;
}

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

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

}

class Loadimage extends AsyncTask<Object, Void, Bitmap> {

private ImageView imv;
private String path;

@Override
protected Bitmap doInBackground(Object... params) {

imv = (ImageView) params[0];

path = imv.getTag().toString();

// Bitmap thumb = BitmapFactory.decodeFile(path);
BitmapFactory.Options opts = new BitmapFactory.Options();

opts.inSampleSize = 2; // for 1/2 the image to be loaded
Bitmap thumb = Bitmap.createScaledBitmap(
BitmapFactory.decodeFile(path, opts), 120, 120, false);

return thumb;

}

@Override
protected void onPostExecute(Bitmap bitmap) {
if (!imv.getTag().toString().equals(path)) {
/*
* The path is not same. This means that this image view is
* handled by some other async task. We don't do anything and
* return.
*/
return;
}

if (bitmap != null && imv != null) {
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(bitmap);
} else {
imv.setVisibility(View.GONE);
}
}

}

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

}

然后创建一个xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" >
</ImageView>
</RelativeLayout>

我正在直接获取文件路径。你只需从 sqlite 中获取名称并传递数组字符串

关于android - 为 simplecursoradapter 实现延迟加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15848037/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com