如何使DetailFragment内的gridview运行完美?

时间:2013-02-25 08:39:33

标签: android android-fragments android-gridview baseadapter

Adapter GridViewDetailFragment public class MyAdapter extends BaseAdapter { Context mContext; static final String TAG_SUCCESS = "success"; static final String TAG_MENU = "menu"; static final String TAG_MID = "mid"; static final String TAG_NAME = "name"; static final String TAG_PICT = "picture"; static final String TAG_PRICE = "price"; private LayoutInflater mInflater; private static ArrayList<HashMap<String, String>> menuList; public MyAdapter(Context c) { mContext = c; mInflater = LayoutInflater.from(c); } public void setArrayList(ArrayList<HashMap<String, String>> mlist) { menuList = new ArrayList<HashMap<String, String>>(); menuList = mlist; Log.d("Try To Print", menuList.get(0).get(TAG_NAME)); } @Override public int getCount() { // TODO Auto-generated method stub return menuList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return menuList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub // Building Parameters ViewHolder holder = null; if (convertView == null) { convertView = mInflater.inflate(R.layout.gviewfill, parent, false); holder = new ViewHolder(); holder.ImgView = new ImageView(mContext); holder.ImgView.setScaleType(ImageView.ScaleType.CENTER_CROP); holder.ImgView = (ImageView) convertView .findViewById(R.id.iv_menu_image); holder.txtName = (TextView) convertView .findViewById(R.id.tv_menu_name); holder.price = (TextView) convertView .findViewById(R.id.tv_menu_price); if (position == 0) { convertView.setTag(holder); } } else { holder = (ViewHolder) convertView.getTag(); } String uri = new String(menuList.get(position).get(TAG_PICT)); int resID = convertView.getResources().getIdentifier(uri, "drawable", mContext.getPackageName()); System.out.println("isi res ID " + position + "= " + resID); holder.ImgView.setImageResource(resID); holder.price.setText("Rp. " + menuList.get(position).get(TAG_PRICE)); holder.txtName.setText(menuList.get(position).get(TAG_NAME)); return convertView; } static class ViewHolder { ImageView ImgView; TextView txtName; TextView price; }

gridview


请参阅this capture

向下滚动FORCECLOSE结果02-25 15:12:32.544: E/AndroidRuntime(502): FATAL EXCEPTION: main 02-25 15:12:32.544: E/AndroidRuntime(502): java.lang.NullPointerException 02-25 15:12:32.544: E/AndroidRuntime(502): at com.example.android.rssfeed.MyAdapter.getView(MyAdapter.java:116) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.widget.AbsListView.obtainView(AbsListView.java:1949) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.widget.GridView.makeAndAddView(GridView.java:1323) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.widget.GridView.makeRow(GridView.java:328) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.widget.GridView.fillDown(GridView.java:281) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.widget.GridView.correctTooLow(GridView.java:649) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.widget.GridView.fillGap(GridView.java:257) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4408) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.widget.AbsListView.onTouchEvent(AbsListView.java:2896) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.View.dispatchTouchEvent(View.java:4609) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1554) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1320) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333) 02-25 15:12:32.544: E/AndroidRuntime(502): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1710) 02-25 15:12:32.544: E/AndroidRuntime(502): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1265) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.app.Activity.dispatchTouchEvent(Activity.java:2309) 02-25 15:12:32.544: E/AndroidRuntime(502): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1683) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.View.dispatchPointerEvent(View.java:4677) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2348) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.view.ViewRoot.handleMessage(ViewRoot.java:2017) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.os.Handler.dispatchMessage(Handler.java:99) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.os.Looper.loop(Looper.java:132) 02-25 15:12:32.544: E/AndroidRuntime(502): at android.app.ActivityThread.main(ActivityThread.java:4025) 02-25 15:12:32.544: E/AndroidRuntime(502): at java.lang.reflect.Method.invokeNative(Native Method) 02-25 15:12:32.544: E/AndroidRuntime(502): at java.lang.reflect.Method.invoke(Method.java:491) 02-25 15:12:32.544: E/AndroidRuntime(502): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 02-25 15:12:32.544: E/AndroidRuntime(502): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 02-25 15:12:32.544: E/AndroidRuntime(502): at dalvik.system.NativeStart.main(Native Method)

logcat

有人可以告诉我我的代码有什么问题吗? 我不明白这个gridview
或者,
有人可以指导我如何使用数据库值创建dialog fragment吗? 我需要为{{1}}获取此值。 请帮帮我..

1 个答案:

答案 0 :(得分:0)

检查menuList内容:

if(menuList.get(position) != null) {
   String uri = new String(menuList.get(position).get(TAG_PICT));

    int resID = convertView.getResources().getIdentifier(uri, "drawable",
            mContext.getPackageName());
    System.out.println("isi res ID " + position + "= " + resID);
    holder.ImgView.setImageResource(resID);

    holder.price.setText("Rp. " + menuList.get(position).get(TAG_PRICE));
    holder.txtName.setText(menuList.get(position).get(TAG_NAME));
}