I have 2 android sqlite databases and I'm trying to see them both in the same listview, but just one of them it is shown.
This is my MainActivity where the listview it is shown.
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.tubapps.accountdb.MESSAGE";
private ListView obj, obj1;
DBHelper mydb;
DBHelperExpense mydb1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new DBHelper(this);
ArrayList array_list = mydb.getAllIncomes();
mydb1 = new DBHelperExpense(this);
ArrayList array_list1 = mydb1.getAllExpenses();
ArrayAdapter arrayAdapter =
new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
ArrayAdapter arrayAdapter1 =
new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list1);
//adding it to the list view.
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(arrayAdapter);
obj1 = (ListView)findViewById(R.id.listView1);
obj1.setAdapter(arrayAdapter1);
obj.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.IncomeActivity.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
obj1.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),tubapps.datepickerdb.ExpenseActivity.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
Intent intent = null;
switch(item.getItemId())
{
case R.id.income_id:
Bundle dataBundle_Income = new Bundle();
dataBundle_Income.putInt("id", 0);
intent = new Intent(getApplicationContext(),tubapps.datepickerdb.IncomeActivity.class);
intent.putExtras(dataBundle_Income);
startActivity(intent);
return true;
case R.id.expense_id:
Bundle dataBundle_Expense = new Bundle();
dataBundle_Expense.putInt("id", 0);
intent = new Intent(getApplicationContext(),tubapps.datepickerdb.ExpenseActivity.class);
intent.putExtras(dataBundle_Expense);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
}
return super.onKeyDown(keycode, event);
}
}
This is my Income Database.
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBIncome.db";
public static final String INCOME_TABLE_NAME = "incomes";
public static final String INCOME_COLUMN_ID = "id";
public static final String INCOME_COLUMN_AMOUNT = "amount";
public static final String INCOME_COLUMN_PAYER = "payer";
public static final String INCOME_COLUMN_DATE = "date";
public static final String INCOME_COLUMN_PAYMENTS = "payments";
public static final String INCOME_COLUMN_CATEGORY = "category";
private HashMap hp;
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 8);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table incomes " +
"(id integer primary key, amount text, payer text, date text, payments text, category text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS incomes");
onCreate(db);
}
public boolean insertIncome (String amount, String payer, String date, String payments, String category)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("amount", amount);
contentValues.put("payer", payer);
contentValues.put("date", date);
contentValues.put("payments", payments);
contentValues.put("category", category);
db.insert("incomes", null, contentValues);
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from incomes where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, INCOME_TABLE_NAME);
return numRows;
}
public boolean updateIncome (Integer id, String amount, String payer, String date, String payments, String category)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("amount", amount);
contentValues.put("payer", payer);
contentValues.put("date", date);
contentValues.put("payments", payments);
contentValues.put("category", category);
db.update("incomes", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteIncome (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("incomes",
"id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList getAllIncomes()
{
ArrayList array_list = new ArrayList();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from incomes", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(INCOME_COLUMN_PAYER)));
res.moveToNext();
}
return array_list;
}
}
And this is my Expense DataBase.
public class DBHelperExpense extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBExpense.db";
public static final String EXPENSE_TABLE_NAME = "expenses";
public static final String EXPENSE_COLUMN_ID = "id";
public static final String EXPENSE_COLUMN_AMOUNT = "amount";
public static final String EXPENSE_COLUMN_PAYEE = "payee";
public static final String EXPENSE_COLUMN_DATE = "date";
public static final String EXPENSE_COLUMN_PAYMENTS = "payments";
public static final String EXPENSE_COLUMN_CATEGORY = "category";
private HashMap hp;
public DBHelperExpense(Context context)
{
super(context, DATABASE_NAME , null, 8);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table expenses " +
"(id integer primary key, amount text, payee text, date text," +
" payments text, category text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS expenses");
onCreate(db);
}
public boolean insertExpense (String amount, String payee, String date, String payments, String category)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("amount", amount);
contentValues.put("payee", payee);
contentValues.put("date", date);
contentValues.put("payments", payments);
contentValues.put("category", category);
db.insert("expenses", null, contentValues);
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from expenses where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, EXPENSE_TABLE_NAME);
return numRows;
}
public boolean updateExpense (Integer id, String amount, String payee, String date, String payments, String category)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("amount", amount);
contentValues.put("payee", payee);
contentValues.put("date", date);
contentValues.put("payments", payments);
contentValues.put("category", category);
db.update("expenses", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteExpense (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("expenses",
"id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList getAllExpenses()
{
ArrayList array_list1 = new ArrayList();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from expenses", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list1.add(res.getString(res.getColumnIndex(EXPENSE_COLUMN_PAYEE)));
res.moveToNext();
}
return array_list1;
}
}
When I try to save an income it doesn't show in the listview and it doesn't even have an error in the logcat.
Aucun commentaire:
Enregistrer un commentaire