1

こんばんは、私のコードの何が問題なのか誰か教えてください。私はおそらく何かが欠けています。my からは何も表示されません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 に答える