2016-03-31 105 views
0

我無法從列表視圖中重新獲取所選項目的ID。 我在做什麼的邏輯是:我有一個產品顯示在列表視圖。當我點擊編輯本產品時,它會將我發送到產品的詳細信息頁面。這裏的產品名稱,價格等都是可以接受的,除了ID以外。 誰能告訴我我做錯了什麼?下面是代碼: 類數據庫處理器無法獲取所選項目_id

//addItemToDB 
public void addItem(ItemModel item) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(Constants.PRODUCT_ID, item.getItemId()); 
    values.put(Constants.PRODUCT_NAME, item.getItemName()); 
    values.put(Constants.PRODUCT_PRICE, item.getItemPrice()); 
    db.insert(Constants.TABLE_NAME, null, values); 
} 

> //get all items 
public ArrayList<ItemModel> getAllItems() { 

    SQLiteDatabase db = getReadableDatabase(); 

    Cursor cursor = db.query(Constants.TABLE_NAME, new String[]{Constants.PRODUCT_ID, Constants.PRODUCT_NAME, Constants.PRODUCT_PRICE}, 
      null, null, null, null, Constants.PRODUCT_DATE + " DESC"); 

    if (cursor.moveToFirst()) { 
     do { 

      ItemModel model = new ItemModel(); 

      //format date 
      java.text.DateFormat dateFormat = java.text.DateFormat.getDateInstance(); 
      String data = dateFormat.format(new Date(cursor.getLong(cursor.getColumnIndex(Constants.PRODUCT_DATE)))); 

      model.setItemId(cursor.getInt(cursor.getColumnIndex(Constants.PRODUCT_ID))); 
      model.setItemName(cursor.getString(cursor.getColumnIndex(Constants.PRODUCT_NAME))); 
      model.setItemPrice(cursor.getDouble(cursor.getColumnIndexOrThrow(Constants.PRODUCT_PRICE))); 

      modelArrayList.add(model); 

     } while (cursor.moveToNext()); 
    } 
    return modelArrayList; 

在listViewAdapter我有getView方法:

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    View row = convertView; 
    final ViewHolder holder; 

    if (row == null || (row.getTag()) == null){ 

     LayoutInflater inflater = LayoutInflater.from(activity); 
     row = inflater.inflate(layoutResource,null); 

     holder = new ViewHolder(); 

     holder.hItemName = (TextView) row.findViewById(R.id.custom_row_productName); 
     holder.hItemPrice = (TextView) row.findViewById(R.id.custom_row_productPrice); 

     holder.hItemEdit = (ImageView) row.findViewById(R.id.custom_row_edit); 

     row.setTag(holder); 
    } else { 
     holder = (ViewHolder) row.getTag(); 
    } 

    holder.hModel = getItem(position); 

    holder.hItemName.setText(holder.hModel.getItemName()); 
    holder.hItemPrice.setText(String.valueOf(holder.hModel.getItemPrice())); 

    holder.hItemEdit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      int itemID = holder.hModel.getItemId(); 
      String itemName = holder.hModel.getItemName(); 
      String itemPrice = String.valueOf(holder.hModel.getItemPrice()); 

      Intent intent = new Intent(activity, ItemDetail.class); 

      intent.putExtra("id", itemID); 
      intent.putExtra("product", itemName); 
      intent.putExtra("price", itemPrice); 
      intent.putExtra("type", itemType); 

      startActivity(activity, intent,null); 
     } 
    }); 

並在該產品的詳細信息頁面onCreate我有:

Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
      itemProductName.setText(extras.getString("product")); 
      itemPrice.setText(extras.getString("price")); 
      final int itemID = extras.getInt("id"); 

      saveButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
         dbHandler.updateItem(itemID); 
         Toast.makeText(ItemDetail.this, itemProductName.getText().toString() + " was modified", Toast.LENGTH_SHORT).show(); 

         startActivity(new Intent(ItemDetail.this, MainActivity.class)); 
        } 
       } 
      }); 
    } 

回答

0

我發現這個問題,就在您的適配器僅更換一個行:

intent.putExtra("id", itemID); 

intent.putExtra("id", ""+itemID); 

還有一個編輯,你哈瓦做:

Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
      itemProductName.setText(extras.getString("product")); 
      itemPrice.setText(extras.getString("price")); 

//Here is the one change 

      final String itemID = extras.getString("id"); 

      saveButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
         dbHandler.updateItem(itemID); 
         Toast.makeText(ItemDetail.this, itemProductName.getText().toString() + " was modified", Toast.LENGTH_SHORT).show(); 

         startActivity(new Intent(ItemDetail.this, MainActivity.class)); 
        } 
       } 
      }); 
    } 
+0

坦克DäñishShärmà它可以像你說的那樣..它可以與我的代碼也只是知道調試器知道問題是我所有插入到數據庫的項目插入的ID爲0。不知道爲什麼,因爲我已經把列_id INTEGER PRIMARY KEY AUTOINCREMENT, – Adrian

+0

你的意思是說你的AUTOINCREMENT功能不工作,它總是爲每個記錄插入零? –