mardi 7 juillet 2015

Android Studio ListView

I am new to Android Development and creating a project that contains a database with 2 tables.

public class DbOpenHelper extends SQLiteOpenHelper{

private static final String DATABSE_NAME = "ExpenseManagerDB";
private static final int DATABASE_VERSION = 1;

public static final String TABLE_PEOPLE = "people";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_TP = "tp";

public static final String TABLE_TRANSACTIONS = "transactions";
public static final String COLUMN_TID = "tid";
public static final String COLUMN_PID = "pid";
public static final String COLUMN_DESC = "desc";
public static final String COLUMN_AMOUNT = "amount";
public static final String COLUMN_DT = "dt";

private static final String TABLE_CREATE_PEOPLE = "CREATE TABLE " + TABLE_PEOPLE + " (" +
        COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COLUMN_NAME + " TEXT, " +
        COLUMN_TP + " TEXT )";

private static final String TABLE_CREATE_TRANSACTIONS = "CREATE TABLE " + TABLE_TRANSACTIONS + " (" +
        COLUMN_TID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COLUMN_PID + " INTEGER, " +
        COLUMN_DESC + " TEXT, " +
        COLUMN_AMOUNT + " INTEGER "+
        COLUMN_DT + " TEXT )";

public DbOpenHelper(Context context) {
    super(context, DATABSE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL(TABLE_CREATE_PEOPLE);
    sqLiteDatabase.execSQL(TABLE_CREATE_TRANSACTIONS);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_PEOPLE);
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_TRANSACTIONS);
    onCreate(sqLiteDatabase);
}


I have created a ListView and I want to show the names from the people table and total amount of money from the transaction table for each user. I know how to do that.
After that I want to click on a certain name in the list and direct it to another activity where I can enter transactions for only that user, which means I have to carry the id of that item in the listView to the new activity.
What is the bestway to do that. Simply saying I want to understand how to get the data in the table that is related to the name in the list(name can be duplicated, only the is is a primary key).
Thank you.

Aucun commentaire:

Enregistrer un commentaire