2013-09-25 77 views
0

每一個身體我開發一個應用程序中,我得到一個問題(在-其實沒有拒絕的問題,但不工作)。我現在用的適配器這裏CustomAdapter不工作

 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)", 

我的XML文件的結構,這是在CustAdpt.java文件中使用的是遵循

 <?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數據庫。我成功完成了上述所有步驟。現在我試圖從自定義適配器的幫助下將所有這些數據從數據庫中檢索到我的模擬器。我使用的是自定義Adpter,因爲在應用程序中,我必須在每行添加按鈕,並且在每個按鈕上單擊我必須執行不同的任務。但我可以堅持與數據不能檢索或顯示在列表視圖中的問題。我也沒有得到任何異常,沒有任何錯誤,這只是邏輯錯誤。我是Android世界的新成員,所以請帶我離開這個問題。提前感謝所有人。

+0

這是很多代碼。你遇到困難的地方是什麼問題,或者你是否有例外? – Raghunandan

+0

@Raghunandan ...感謝您的回覆......我收到任何錯誤或異常......這段代碼對我沒有任何幫助。 – DilAka

回答

0

我覺得這是遊標的問題。可能數據不存儲在數據庫中。請檢查您的光標。

+0

是的...你的right.this是光標問題。我在2天前解決了這個問題,謝謝你的幫助。 – DilAka

1

替換menuList.setAdapter(adapter);menuList.setAdapter(custadapter);您正在附上錯誤的對象。

+0

@Vikram ...這是錯誤的發佈這個任務我仍然是相同的問題.. – DilAka