我正在尝试GridView
在Fragment
. 由于某种原因,没有调用 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 输出,所以我知道它没有到达那里。谢谢你。