CursorAdapter没有调用newView或bindView android

时间:2012-07-14 10:58:40

标签: android android-cursoradapter

我正在尝试在GridView内的Fragment中显示数据库中的图像。出于某种原因,CursorAdapter的newView()bindView()方法未被调用。这是我的代码:

GridImageAdapter.java类:

public class GridImageAdapter extends CursorAdapter {
    private static String TAG = "Image_Debug";

    private Context mContext;
    private int count;
    private String columnname;

    public GridImageAdapter(Context context,Cursor cursor,int flag){
        super(context,cursor,flag);
        this.mContext = context;
        //this.columnname = columnname;
        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 View getView(int position, View convertView, ViewGroup parent) {

        ImageView image;
        ViewHolder holder;

        if(convertView == null) {
            LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.media_view, null);
            holder = new ViewHolder();  
            holder.image = (ImageView)convertView.findViewById(R.id.media_image_id);    
            convertView.setTag(holder); 
        } else {
            holder = (ViewHolder)convertView.getTag();
        }   

           // holder.image.setImageBitmap((position).getBitmap());    

        return convertView;
    }*/

    @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(WeatherImageDB.CLOUDY));
        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();

            LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.media_view, container, false);

            holder.image = (ImageView)view.findViewById(R.id.media_image_id);   
            view.setTag(holder);    

            return view;
    }
}

class ViewHolder {
   ImageView image;
   TextView item_name;
}

和片段:

public class PhotoFragment extends Fragment implements LoaderCallbacks<Cursor> {

private static final int FRIENDS_LOADER = 0;

private static final int MYLOADER_LOADER = 1;

    TextView text;
    GridView grid;
    GridImageAdapter imageAdapter;

    PhotoDB dbadapter;

public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        dbadapter = new PhotosDB(getActivity().getApplicationContext()); //database class

        getActivity().getSupportLoaderManager().initLoader(FRIENDS_LOADER, null, this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState){

        View view = inflater.inflate(R.layout.my_tags, container, false);


    text = (TextView)view.findViewById(R.id.my_header_text_id);
    grid = (GridView)view.findViewById(R.id.my_tag_gridview_id);
    grid.setClipToPadding(false);

    imageAdapter = new GridImageAdapter(getActivity().getApplicationContext(), null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    grid.setAdapter(imageAdapter);
        return view;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {

        switch(id){
        case FRIENDS_LOADER:
                return new CustomCursorLoader(getActivity().getApplicationContext(), dbadapter,PhotoDB.FRIENDS);

        case MY_LOADER:

            return new CustomCursorLoader(getActivity().getApplicationContext(),dbadapter,PhotoDB.MYFOTOS);
        }

        return null;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        if(loader.getId() == 0){
            imageAdapter.swapCursor(data);
                    text.setText("Friend Photos");  
    }

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

    }

    public static final class CustomCursorLoader extends SimpleCursorLoader {

        String columnname;
        PhotoDB dbadapter;

        public CustomCursorLoader(Context context, PhotoDB 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 tag from database now");
            cursor = dbadapter.retrieveTag(columnname);

            return cursor;
        }

    }

}

所以,我的问题是,为什么CursorAdapter没有调用newView() / bindView()方法?我甚至无法获得这些方法的logcat输出,所以我知道它没有到达那里。谢谢。

3 个答案:

答案 0 :(得分:4)

嗯,我知道它已经很晚了 - 我没有找到这个帖子,但我遇到了同样的问题(尽管我只是想做一个待办事项列表)。我花了最后三个小时试图弄清问题是什么,并在网络上寻找可能的解决方案。

也许它可以帮助别人,所以我会说出来。

就我而言,有效的是:

这是使用onCreate方法的主要活动的一部分:

public class TimeTrackerActivity extends Activity implements LoaderCallbacks<Cursor>
{
public static final int TIME_ENTRY_REQUEST_CODE = 1;
public static final int TIME_EDIT_REQUEST_CODE = 2;

private TimeTrackerAdapterDB adapter;
private TimeTrackerDatabaseHelper databaseHelper;
private ListView listView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Database Helper
    databaseHelper = new TimeTrackerDatabaseHelper(this);
    //ListView that wasn't showing anything -before solving it-
    listView = (ListView)findViewById(R.id.times_list);
    //A private method I made for adding a listener to the ListView
    setListViewListener(listView);

    this.getLoaderManager().initLoader(0, null, this);
}

...并向下移动到LoaderCallbacks方法:

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    CursorLoader loader = new CursorLoader(this, 
            TimeTrackerContentProvider.CONTENT_URI, null, null, null, null);

    return loader;
}

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
            // I'm initializing my custom adapter here because it requires a cursor
    if (adapter == null)
        adapter = new TimeTrackerAdapterDB(this, cursor);
    else
        adapter.swapCursor(cursor);

    //THIS IS WHAT SOLVED IT!
    listView.setAdapter(adapter);
}

所以,在我的情况下,它不工作的原因是因为适配器没有被设置为ListView,所以我的列表没有显示任何内容,并且方法newView()和bindView()从未被调用之前。

一旦光标加载完成,也许可以通过在onLoadFinished上将适配器设置为GridView来实现。

不知道它是否会解决您的问题或其他任何人,但它对我有用。 希望它有所帮助。

答案 1 :(得分:2)

尝试以这种格式更改代码,它应该可以正常工作。

 class GridImageAdapter extends CursorAdapter {
            LayoutInflater mInflater; 
            SimpleDateFormat sdf; 

            GridImageAdapter (Context context, Cursor cursor)
            {
                super(context, cursor);
                mInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            }

            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent)
            {
                return mInflater.inflate(R.layout.yourxml, parent, false);
            }

           @Override
            public void bindView(View row, Context context, Cursor cursor)
            {
                ImageView expensesdate = (ImageView) row.findViewById(R.id.dateWorked); 
                        You can Assign your image with this imageview.

            } 
        }

希望它有所帮助。

答案 2 :(得分:0)

我也遇到了这个问题,我用这种方式解决了,你可以尝试一下。 在您的片段中,更改以下行:

getActivity().getSupportLoaderManager().initLoader(FRIENDS_LOADER, null, this);

getLoaderManager().initLoader(FRIENDS_LOADER, null, HomeFragment.this).forceLoad();

前者不起作用,因为它将“活动”作为上下文,因此片段不会被通知。