mardi 12 janvier 2016

how to load different image to different button without creating multiple database in android studio?

I wanted to have multiple image button, where when we click on the image button, it will display different product item. However in order to do that i have to create one database each for the image button. So let say i have 8 image button i have to create 8 database. I try to create just one database by sorting the item based on categories, but whenever i point to the database it will display all item. What should i do?

<LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp">

<ImageButton
    android:layout_width="120dp"
    android:layout_height="100dp"
    android:id="@+id/imageButton"
    android:layout_below="@+id/textView2"
    android:src="@drawable/fashion"
    android:layout_toStartOf="@+id/imageButton3" />

<ImageButton
    android:layout_width="120dp"
    android:layout_height="100dp"
    android:id="@+id/imageButton2"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/textView2"
    android:src="@drawable/book"
    android:layout_above="@+id/imageButton5"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="40dp"/>

    </LinearLayout>

This is the sqlite demo activity where i get my picture from mobile phone category and i will manually insert its detail.

  case PICK_FROM_GALLERY:
            Bundle extras2 = data.getExtras();

            if (extras2 != null) {
                Bitmap yourImage = extras2.getParcelable("data");
                // convert bitmap to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                yourImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte imageInByte[] = stream.toByteArray();
                Log.e("output before conversion", imageInByte.toString());
                // Inserting Contacts
                Log.d("Insert: ", "Inserting ..");
                db.addContact(new Contact("bonamana", "RM20.60","books", 
    "What the heck", imageInByte));
                Intent i = new Intent(SQLiteDemoActivity1.this,
                        SQLiteDemoActivity1.class);
                startActivity(i);
                finish();
            }

            break;
    }
  }

This is the display image activity where i get the information i want to display from the database

  List<Contact> contacts = db.getAllContacts();
    for (Contact cn : contacts) {
        String log = "ID:" + cn.getID() + " Name: " + cn.getName()+ " Price: 
   " + cn.getPrice() +" Categories: " + cn.getCategories()
                + " Description: " + cn.getDescription() + " ,Image: " + 
   cn.getImage();
        // Writing Contacts to log
        Log.d("Result: ", log);
        // add contacts data in arrayList
        imageArry.add(cn);

This is the database where i store my information.

// Database Name
private static final String DATABASE_NAME = "imagedb1";

// Contacts table name
private static final String TABLE_CONTACTS = "contacts1";


// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PRICE = "price";
private static final String KEY_IMAGE = "image";
private static final String CATEGORIES = "categories";
private static final String DESCRIPTION = "description";

public DataBaseHandler1(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"+ KEY_PRICE + " TEXT," + CATEGORIES + " TEXT,"
            + DESCRIPTION + " TEXT,"+ KEY_IMAGE + " BLOB" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);

}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
    // Create tables again
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

public// Adding new contact
void addContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact._name); // Contact Name
    values.put(KEY_PRICE, contact._price); // Contact Name
    values.put(CATEGORIES, contact._categories); // Contact Name
    values.put(DESCRIPTION, contact._description); // Contact Name
    values.put(KEY_IMAGE, contact._image); // Contact Phone

    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    db.close(); // Closing database connection
}

// Getting All Contacts
public List<Contact> getAllContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    // Select All Query
    //String selectQuery = "SELECT  id,name,price,image FROM contacts WHERE categories = 'fashion' ORDER BY name";
    String selectQuery = "SELECT * FROM contacts1 WHERE categories = 'books' ORDER BY name";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.setID(Integer.parseInt(cursor.getString(0)));
            contact.setName(cursor.getString(1));
            contact.setPrice(cursor.getString(2));
            contact.setCategories(cursor.getString(3));
            contact.setDescription(cursor.getString(4));
            contact.setImage(cursor.getBlob(5));
            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }
    // close inserting data from database
    db.close();
    // return contact list
    return contactList;

}

}

Aucun commentaire:

Enregistrer un commentaire