1

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

但是,如果用户在数据库中再添加一张图片,则再次加载所有图片需要很长时间,因为我保留了asynctaskonResume ()以使添加的图片也可见。每当用户添加每张图片时,情况都是如此。有人可以建议我一种简单的方法来让最后添加的图片在 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);
            }

        }
}
4

2 回答 2

4

使用此代码

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 获取名称并传递数组字符串

于 2013-04-06T07:54:19.200 回答
0

使用Android-Universal-Image-Loader 并设置

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

     File imgFile = new File(imagepath);
         if(imgFile.exists()){
               imageLoader.displayImage(imageUri(ur file path), imageView);
                }
于 2013-04-06T07:44:22.053 回答