我创建了一个带有两个表的android SQLite数据库,每个表都在不同的活动中执行。而且我试图将它们放在同一个列表视图中。
这是我的listview活动。
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);
}
});
}
问题是它只显示费用。我认为问题来自适配器,但我不知道如何将它们都包含在同一个适配器中。 :)
答案 0 :(得分:2)
set
并不代表add
。如果你setAdapter
,则意味着旧的(如果有的话)将被替换。 OnItemClickListener
也是如此。
如果游标具有相同的列名,您可以将它们组合起来,但它们不同,并且适配器无法区分2个游标的数据(费用和收入)。
此外,每个单击侦听器都具有特定于类型的操作,例如放置特定的intent附加内容。这只是意味着您需要多个适配器,因此需要多个ListView
您需要使您的桌面和应用设计更通用或继续使用2 ListView
s。