sqlite查询无法给出正确的结果

时间:2013-09-22 20:21:29

标签: android sqlite

我有两个tables存储members而另一个存储expense,当我保存任何expense时,可以选择members expense中出现的人,当我想查看任何expense的详细信息然后在自定义ListView中时,我会看到member的名称和金额,但我得到了自定义textViews的{​​{1}}中的成员姓名,但无法从ListView表中获取金额。我这样做了。

expense

Adapter class

public class ViewExpenseAdapter extends CursorAdapter { protected Activity activity = null; public ViewExpenseAdapter(Activity activty, Cursor cursor) { super(activty, cursor, false); // TODO Auto-generated constructor stub this.activity = activty; } @Override public void bindView(View row, Context context, Cursor cursor) { // TODO Auto-generated method stub ViewExpenseHolder holder = (ViewExpenseHolder) row.getTag(); holder.populateFrom(cursor); } @Override public View newView(Context context, Cursor cursor, ViewGroup root) { // TODO Auto-generated method stub LayoutInflater inflater = activity.getLayoutInflater(); View row = inflater.inflate(R.layout.view_expense_row, root, false); ViewExpenseHolder holder = new ViewExpenseHolder(activity, row); row.setTag(holder); return (row); } }

Holder class

获取数据的public class ViewExpenseHolder { /** * */ protected Activity activity = null; protected TextView memberName, totalAmount; public ViewExpenseHolder(Activity activity, View row) { // TODO Auto-generated constructor stub this.activity = activity; memberName = (TextView) row.findViewById(R.id.member_name); totalAmount = (TextView) row.findViewById(R.id.amount_per_member); } public void populateFrom(Cursor cursor) { // TODO Auto-generated method stub memberName.setText(cursor.getString(1)); totalAmount.setText(cursor.getString(3)); } }

query

public Cursor getPresentMembers(String EXPENSE_ID) { //String args [] = {EXPENSE_ID}; Log.v(DataConstants.MMs_TAG, "All present members returned"); String query = "SELECT " + " members._id, " + MemberColumns.NAME + ", " + " expense._id, expense.amount, " + " member_expense._id, member_expense.bridge_id" +" FROM " + MemberTable.TABLE_MEMBER + ", " + ExpenseTable.TABLE_EXPENSE + ", " + MemberExpenseTable.TABLE_MEMBER_EXPENSE + " WHERE " + " member_expense.exp_id = expense._id " + " AND expense._id = " + EXPENSE_ID + " AND members._id = member_expense._id"; return db.rawQuery(query, null); } 活动

ViewExpenseDetail

1 个答案:

答案 0 :(得分:1)

此处,相同的光标用于getMemebrNamegetExpenseAmount

public void populateFrom(Cursor cursor) {
    memberName.setText(MemberDataManager.getMemebrName(cursor));
    totalAmount.setText(ExpenseDataManager.getExpenseAmount(cursor));
}

两种get方法都依赖于相同的列索引1:

public static String getMemebrName(Cursor cursor) {
    return cursor.getString(1);
}

public static String getExpenseAmount(Cursor cursor) {
    return cursor.getString(1);
}

因此,它们返回列表视图行布局中填充的相同值。根据你的光标我宁愿期望列索引3 ...

public static String getExpenseAmount(Cursor cursor) {
    return cursor.getString(3);
}