jeudi 31 mars 2016

Having trouble getting selected item _id

I am having trouble retriving the selected item's id from a listView. The logic of what i am doing is : i have a product displayed in a listView. When i click to edit this product it sends me to a details page of the product. Here the product name, price etc. are retrived ok execept for the ID. Can anyone tell me what i am doing wrong? Here is the code: Class DatabaseHandler

//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;

In the listViewAdapter i have the getView method:

 @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);
        }
    });

And in the details page of the product on the onCreate i have:

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));
                    }
                }
            });
    }

Aucun commentaire:

Enregistrer un commentaire