自定义适配器不工作

时间:2013-09-25 16:22:08

标签: android custom-adapter

我正在开发一个应用程序,我正在解决一个问题(实际上没有任何问题,但不起作用)。我在这里使用适配器

      menuList = (ListView) findViewById(R.id.menu_list);
    //Cursor  cursor = dh.rawQuery("SELECT _id, item_id, item_type , item_name,Item_cost FROM temp_menu WHERE item_name LIKE ? AND Item_cost LIKE ? ",new String[]{"%","%"});

    Cursor cursor = dh.query(DatabaseHelpereKOT.RESTAURANT_menu_temp, null,"item_name=?", new String[] {"Apple"}, null,null, null);
    startManagingCursor(cursor);
    CustAdpt custadapter = new CustAdpt(this, cursor);
    menuList.setAdapter(custadapter);

Custdpt是我的自定义适配器。这是我的自定义适配器代码

     public class CustAdpt extends CursorAdapter{
    private Context mContext;
    private LayoutInflater mInflater;
    Cursor cursor, c;
    View convertView;
    private String str_item_id;
    private String str_item_type;
    private String str_item_name;
    private String str_item_cost;
    ViewHolder holder;
    SQLiteDatabase dh = DatabaseHelpereKOT.getInstance().getDb();
    public CustAdpt(Context context, Cursor c) {
        super(context, c);
        mInflater = LayoutInflater.from(context);
        mContext = context;
        cursor = c;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        holder = (ViewHolder) view.getTag();
        holder.setImType((ImageView) view.findViewById(R.id.veg_nv_image));
        holder.setTvTitle((TextView) view.findViewById(R.id.item_name));
        holder.setTvPrice((TextView) view.findViewById(R.id.item_cost));
        holder.getTvTitle().setText(cursor.getString(cursor.getColumnIndex("item_name")));
        holder.getTvDescription().setText(cursor.getString(cursor.getColumnIndex("Item_cost")));
        int _id = cursor.getInt(cursor.getColumnIndex("_id"));
        view.setTag(R.id.item_name, _id);
        int type = cursor.getInt(cursor.getColumnIndex("item_type")); 
        if (type == 1) {
            //holder.getImType().setPadding(6, 0, 0, 0);
            holder.getImType().setBackgroundResource(R.drawable.non_veg);
        } else {  
            holder.getImType().setBackgroundResource(R.drawable.veg);
        }


    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final ViewHolder holder;
        c = cursor;
        convertView = mInflater.inflate(R.layout.list_data_item, parent, false);
        holder = new ViewHolder();
        holder.setId(cursor.getInt(0));
        ((ImageView) (convertView
                .findViewById(R.id.main_body_item_title_second_pics)))
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // System.out.println(view.getTag());
                        addToDB(holder.getId());
                        //Toast.makeText(getApplicationContext(), "insertDataOrder method", 2000).show();
                        System.out.println("i m in ryt [palce");
                    }
                });
        convertView.setTag(holder);
        return convertView;
    }
    void addToDB(Integer objId) {
        if (objId != null) {
            int _id = objId;
            Cursor cursor = dh.query(DatabaseHelpereKOT.RESTAURANT_menu_temp,
                    new String[] { "_id", "item_id", "item_type", "item_name",
                            "Item_cost"},"_id=?", new String[] { String
                            .valueOf(_id) }, null, null, null);
            if ((cursor != null) && (cursor.getCount() > 0)
                    && cursor.moveToFirst()) 
            {
                str_item_id = cursor.getString(cursor.getColumnIndex("item_id"));
                str_item_type = cursor.getString(cursor
                        .getColumnIndex("item_type"));
                str_item_name = cursor.getString(cursor.getColumnIndex("item_name"));
                str_item_cost = cursor.getString(cursor
                        .getColumnIndex("Item_cost"));
                ContentValues orderValues = new ContentValues();
                orderValues.put("item_id", str_item_id);
                orderValues.put("item_type", str_item_type);
                orderValues.put("item_name", str_item_name);
                orderValues.put("Item_cost", str_item_cost);
                dh.insert(DatabaseHelpereKOT.RESTAURANT_menu_order, null, orderValues);
                String msg = "Menu Item Added Successfully";
                Message msgObject = new Message();
                msgObject.what = 1;
                msgObject.obj = msg;
                addMenuItemHandler.sendMessage(msgObject);


            }
        }
    }
    public Handler addMenuItemHandler = new Handler() {

        @Override
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 1) {
                System.out.println("inside handler");
                Toast.makeText(mContext, (String) msg.obj, Toast.LENGTH_SHORT)
                        .show();  
            }
        };

    };
}

这是我的数据库结构 -

              "create table "
                    + RESTAURANT_menu_temp
                    + " (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,item_id TEXT , item_type TEXT , item_name TEXT , Item_cost DOUBLE)",
                    "create table "
                    + RESTAURANT_menu_order
                    + " (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,item_id TEXT , item_type TEXT , item_name TEXT , Item_cost DOUBLE)",

我在CustAdpt.java文件中使用的xml文件如下

     <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/veg_nv_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/non_veg" />

<TextView
    android:id="@+id/item_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/item_cost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<ImageView
    android:id="@+id/add_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/add_item_order" />
    </LinearLayout>

现在这是我遇到问题的所有代码。现在我要描述一下。首先,数据是动态的json格式。我解析所有这些json并将其保存到sqlite数据库。我成功完成了上述所有这些步骤。现在我尝试在自定义适配器的帮助下从数据库检索所有这些数据到我的模拟器。我正在使用Custom Adpter,因为在应用程序中我必须在每行中添加按钮并在每个按钮上单击我必须执行不同的任务。但我遇到的问题是数据未在列表视图中检索或显示。我也没有任何异常,也没有任何错误,这只是逻辑错误。我是android世界的新手。所以请带我解决这个问题。谢谢所有人。

2 个答案:

答案 0 :(得分:1)

menuList.setAdapter(adapter);替换为menuList.setAdapter(custadapter );您附加了错误的对象。

答案 1 :(得分:0)

我认为这是游标的问题。可能是数据没有存储在数据库中。检查光标。