1

美好的一天,请有人帮我找出我的代码有什么问题。我可能错过了一些东西。我的CursorAdapter. 已经在这里挣扎了一段时间了。任何帮助将不胜感激!

public class ManageTagFragment extends Fragment implements LoaderCallbacks<Cursor> {

    private static String TAG = "Image_Debugger";
    private static final int IMAGE_LOADER = 0;
    TextView text;
    GridView grid;
    CustomGridImageAdapter imageAdapter;
    Cursor cursor;
    ImageDB dbadapter;
    private Cursor c;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        dbadapter = new ImageDB(getActivity().getApplicationContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.wallpaper_images, container, false);      
        grid = (GridView)view.findViewById(R.id.gridview);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);    
        imageAdapter = new CustomGridImageAdapter(getActivity(), null, 0);
        grid.setAdapter(imageAdapter);
        imageAdapter.notifyDataSetChanged();
        getActivity().getSupportLoaderManager().initLoader(IMAGE_LOADER, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CustomCursorLoader(getActivity().getApplicationContext(), dbadapter, ImageDB.IMAGES);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        imageAdapter.swapCursor(data);
    }

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

    public static final class CustomCursorLoader extends SimpleCursorLoader {

        String columnname;
        ImageDB dbadapter;
        ArrayList<String> imageuri = new ArrayList<String>();

        public CustomCursorLoader(Context context, ImageDB dbadapter, String columnname){
            super(context);
            this.dbadapter = dbadapter;
            this.columnname = columnname;
        }

        public CustomCursorLoader(Context context,String columnname){
            super(context);
            this.columnname = columnname;

        }

        @Override
        public Cursor loadInBackground() {
            Cursor cursor = null;
            dbadapter.open();

            Log.d(TAG, "retrieving Images from database now");
            cursor = dbadapter.retrieveTag(columnname);

            if(cursor.moveToFirst()){
                 final int index = cursor.getColumnIndex(columnname);

                do {
                    final String val = cursor.getString(index); // this is just
                                                                // for test
                    if (val != null) {
                        Log.d(TAG, "files are" + val);
                        imageuri.add(val);
                    }

                } while (cursor.moveToNext());
            }   
            return cursor;
        }
    }
}

CustomGridImageAdapter.java:

public class CustomGridImageAdapter extends CursorAdapter {
    private static String TAG = "Image_Debugger";
    private LayoutInflater inflater;
    private int count;

    public CustomGridImageAdapter(Context context, Cursor cursor, int flags) {
        super(context, cursor, flags);
        inflater = LayoutInflater.from(context);
        Log.d(TAG, "calling grid imageadapter now");
    }

    @Override
    public int getCount() {
        return count;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        Log.d(TAG, "calling bind view");
        ViewHolder holder = (ViewHolder) view.getTag();
        String name = cursor.getString(cursor
                .getColumnIndexOrThrow(ImageDB.IMAGES));
        Log.d(TAG, "string name in gridimageadapter is" + name);

        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeFile(name);

        Bitmap newbitmap = Bitmap.createScaledBitmap(bitmap, 120, 120, false);
        holder.image.setImageBitmap(newbitmap);

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup container) {
        Log.d(TAG, "calling new view here");
        ViewHolder holder = new ViewHolder();
        View view = inflater.inflate(R.layout.media_view, container, false);

        holder.image = (ImageView) view.findViewById(R.id.media_image_id);
        holder.image.setLayoutParams(new GridView.LayoutParams(100, 100));
        holder.image.setScaleType(ImageView.ScaleType.FIT_CENTER);
        view.setTag(holder);

        return view;
    }

    class ViewHolder {
        ImageView image;
        TextView item_name;
    }
}
4

1 回答 1

1

问题可能就在这里。它只是一个假设。对不起,如果我错了。

@Override
  public int getCount() {
     return count;
  }

这里的 count 值是多少。我没有找到您传递 . 值的任何地方count。所以我的假设是,这里返回了 int 的默认值(即 count=0),因此您在 GridView 中找不到任何数据。

你能检查一下并发表评论吗?

于 2012-07-20T09:53:28.793 回答