George Zoiade
George Zoiade

Reputation: 73

Android 2 sqlite database tables in the same listview adapter

I have made an android SQLite database with two tables, each of them are performing in separate activities. And I'm trying to put them in the same listview.

This is my activity where the listview is.

public class MainActivity extends ActionBarActivity {

    ListView lv;
    SQLController dbcon;
    TextView incomeID_tv, incomePayer_tv, incomeAmount_tv;
    TextView incomeDate_tv, incomeCategory_tv, incomePayments_tv;
    TextView expenseID_tv, expensePayee_tv, expenseAmount_tv;
    TextView expenseDate_tv, expenseCategory_tv, expensePayments_tv;

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

        dbcon = new SQLController(this);
        dbcon.open();
        lv = (ListView) findViewById(R.id.incomeList_id);

        Cursor cursor = dbcon.readIncomeData();
        String[] from = new String[] { DBHelper.INCOME_ID, DBHelper.INCOME_AMOUNT, DBHelper.INCOME_PAYER,
                DBHelper.INCOME_DATE, DBHelper.INCOME_CATEGORY, DBHelper.INCOME_PAYMENTS};
        int[] to = new int[] { R.id.income_id, R.id.income_amount, R.id.income_payer, R.id.income_date,
                R.id.income_category, R.id.income_payments};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                MainActivity.this, R.layout.income_entry, cursor, from, to);

        adapter.notifyDataSetChanged();
        lv.setAdapter(adapter);

        Cursor ecursor = dbcon.readExpenseData();
        String[] efrom = new String[] { DBHelper.EXPENSE_ID, DBHelper.EXPENSE_AMOUNT, DBHelper.EXPENSE_PAYEE,
                DBHelper.EXPENSE_DATE, DBHelper.EXPENSE_CATEGORY, DBHelper.EXPENSE_PAYMENTS};
        int[] eto = new int[] { R.id.expense_id, R.id.expense_amount, R.id.expense_payee, R.id.expense_date,
                R.id.expense_category, R.id.expense_payments};

        SimpleCursorAdapter eadapter = new SimpleCursorAdapter(
                MainActivity.this, R.layout.expense_entry, ecursor, efrom, eto);

        eadapter.notifyDataSetChanged();
        lv.setAdapter(eadapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                incomeID_tv = (TextView) view.findViewById(R.id.income_id);
                incomeAmount_tv = (TextView) view.findViewById(R.id.income_amount);
                incomePayer_tv = (TextView) view.findViewById(R.id.income_payer);
                incomeDate_tv = (TextView) view.findViewById(R.id.income_date);
                incomeCategory_tv = (TextView) view.findViewById(R.id.income_category);
                incomePayments_tv = (TextView) view.findViewById(R.id.income_payments);

                String incomeID = incomeID_tv.getText().toString();
                String incomeAmount = incomeAmount_tv.getText().toString();
                String incomePayer = incomePayer_tv.getText().toString();
                String incomeDate = incomeDate_tv.getText().toString();
                String incomeCategory = incomeCategory_tv.getText().toString();
                String incomePayments = incomePayments_tv.getText().toString();

                Intent modify_intent = new Intent(getApplicationContext(),
                        IncomeEdit.class);
                modify_intent.putExtra("incomeID", incomeID);
                modify_intent.putExtra("newIncomeAmount", incomeAmount);
                modify_intent.putExtra("newIncomePayer", incomePayer);
                modify_intent.putExtra("newIncomeDate", incomeDate);
                modify_intent.putExtra("newIncomeCategory", incomeCategory);
                modify_intent.putExtra("newIncomePayments", incomePayments);
                startActivity(modify_intent);
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                expenseID_tv = (TextView) view.findViewById(R.id.expense_id);
                expenseAmount_tv = (TextView) view.findViewById(R.id.expense_amount);
                expensePayee_tv = (TextView) view.findViewById(R.id.expense_payee);
                expenseDate_tv = (TextView) view.findViewById(R.id.expense_date);
                expenseCategory_tv = (TextView) view.findViewById(R.id.expense_category);
                expensePayments_tv = (TextView) view.findViewById(R.id.expense_payments);

                String expenseID = expenseID_tv.getText().toString();
                String expenseAmount = expenseAmount_tv.getText().toString();
                String expensePayer = expensePayee_tv.getText().toString();
                String expenseDate = expenseDate_tv.getText().toString();
                String expenseCategory = expenseCategory_tv.getText().toString();
                String expensePayments = expensePayments_tv.getText().toString();

                Intent modify_intent = new Intent(getApplicationContext(),
                        ExpenseEdit.class);
                modify_intent.putExtra("expenseID", expenseID);
                modify_intent.putExtra("newExpenseAmount", expenseAmount);
                modify_intent.putExtra("newExpensePayee", expensePayer);
                modify_intent.putExtra("newExpenseDate", expenseDate);
                modify_intent.putExtra("newExpenseCategory", expenseCategory);
                modify_intent.putExtra("newExpensePayments", expensePayments);
                startActivity(modify_intent);
            }
        });
    }

The problem is that it shows just the expenses. I think the problem is from the adapter, but I don't know how to include both of them in the same adapter. :)

Upvotes: 0

Views: 367

Answers (1)

Simas
Simas

Reputation: 44158

set doesn't mean add. If you setAdapter it means the old one (if any) will be replaced. The same goes for the OnItemClickListener.

If the cursors had the same column names, you could combine them however they differ and the adapter has no means to distinguish between the data of the 2 cursors (expenses and incomes).

Furthermore each click listener has type-specific actions like putting specific intent extras. This just means that you want multiple adapters therefore multiple ListView's.

You need to make your table and app design more generic or continue using 2 ListViews.

Upvotes: 2

Related Questions