0

我有两个tables要存储members,另一个要存储expense,当我保存任何一个时expense,可以选择其中members存在的人expense,当我想查看任何一个的详细信息时,我会expense在自定义ListView中看到名称member和数量,但是我在两个textViews自定义中都获得了成员的名称,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活动是

public class ViewExpenseDetail extends Activity {

protected Cursor presentMember = null;
protected MMSDataSource source = null;

protected ViewExpenseAdapter viewExpenseAdapter = null;
protected ListView membersList = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_expense);

    membersList = (ListView) findViewById(R.id.present_members);

    source = new MMSDataSource(ViewExpenseDetail.this);
    source.openMMSDatabase();

    String EXPENSE_ID = getIntent().getStringExtra(MemberExpenseColumns.EXPENCE_ID);


    presentMember = source.getPresentMembers(EXPENSE_ID);

    viewExpenseAdapter = new ViewExpenseAdapter(ViewExpenseDetail.this, presentMember);
    membersList.setAdapter(viewExpenseAdapter);
}

public void onBackBtn(View view) {
    // TODO Auto-generated method stub
    finish();
}

}
4

1 回答 1

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);
}
于 2013-09-22T21:02:48.980 回答