android - CursorAdapter 和 CursorLoader android 的问题

标签 android

美好的一天,请有人帮我找出我的代码有什么问题。我可能遗漏了什么。我的 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;
    }
}

最佳答案

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

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

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

你能检查一下并回复吗?

关于android - CursorAdapter 和 CursorLoader android 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11576765/

相关文章:

android - 如何通过按下按钮在第二个 fragment 的 TextView 中设置文本

android 加载多个标记 block 我的 UI

android - 防止 Jacoco 在默认 gradle 任务中运行(这发生在每个本地构建上)

android - 如何在 Lollipop 之前的设备上使用 Material design ProgressBar?

来自 viewpager getActivity 的 Android childfragments 返回 null

android - Android Studio-无法解析配置 ':classpath'的所有依赖项。但是可以通过网络浏览器下载

android - 在 Button 中动画化 Drawable 变化

android - Android 中的屏幕和 View 坐标

java - 将时间转换为毫秒

android - android 设备中的 egnos 定位