我试图在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输出,所以我知道它达不到那里。谢谢您。 最佳答案
好吧,我知道已经很晚了——我还没找到这篇文章——但我也有同样的问题(尽管我只是想做一个待办事项列表)。我花了三个小时试图找出问题所在,并在网上寻找可能的解决方案。
也许它可以帮助别人,所以我会放它。
就我而言,成功的是:
这是我使用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。
不知道它是否能解决你或其他人的问题,但它对我有效。
希望有帮助。