0

我正在开发一个我遇到问题的应用程序的每个人(实际上没有问题但不起作用)。我在这里使用适配器

      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 Adapter 的帮助下将所有这些数据从数据库中检索到我的模拟器中。我正在使用自定义适配器,因为在应用程序中我必须在每一行中添加按钮,并且在每个按钮上单击我必须执行不同的任务。但是我遇到了数据未检索或显示在列表视图中的问题。我也没有得到任何异常也没有任何错误,这只是逻辑错误。我是android世界的新手。所以请带我摆脱这个问题。在此先感谢大家。

4

2 回答 2

1

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

于 2013-09-25T16:26:57.790 回答
0

我认为这是光标的问题。可能是数据未存储在数据库中。请检查您的光标。

于 2013-09-29T17:49:08.833 回答