2

我正在尝试GridViewFragment. 由于某种原因,没有调用 CursorAdapter 的newView()orbindView()方法。这是我的代码:

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 输出,所以我知道它没有到达那里。谢谢你。

4

3 回答 3

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 来工作。

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

于 2012-09-14T17:37:52.253 回答
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.

            } 
        }

希望能帮助到你。

于 2012-07-14T11:33:04.860 回答
0

我也遇到了这个问题,我就是这样解决的,你可以试试。在您的片段中,更改此行:

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

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

前者不起作用,因为它将 Activity 作为上下文,因此不会通知 Fragment。

于 2017-02-09T12:37:54.937 回答