mercredi 4 mai 2016

Search button that displays the Item from SqliteDatabase

I am not experienced with android, i only started learning it

Problem: I would like when i type the name of the item in the Edit Text that it displays the items name, cost, quantity etc. I don't know if i have to use a button so when i click it, the fields of item that i typed in the edit text comes up or if there is a different way to display the fields for the item.

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "ShoppingList.db";


public static final String TABLE_NAME = "list_table";


public static final String COL_1 = "ID";


public static final String COL_2 = "NAME";


public static final String COL_3 = "COST";
public static final String COL_4 = "QUANTITY";
public static final String COL_5 = "PURCHASED";
public static double total;


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 2);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME +
            " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,COST TEXT, QUANTITY INTEGER,PURCHASED TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String name, String cost, String quantity, String purchased) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, name);
    contentValues.put(COL_3, cost);
    contentValues.put(COL_4, quantity);
    contentValues.put(COL_5, purchased);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if (result == -1)
        return false;
    else
        return true;
}

This is my xml:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editSearch"
    android:layout_gravity="center_horizontal" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/search"
    android:id="@+id/button"
    android:layout_gravity="center_horizontal" />

This is my Search Class. I don't have really anything because i don't know what to exactly do

public class SearchItem extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_item);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Search List");

}

}

Aucun commentaire:

Enregistrer un commentaire