我有两个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();
}
}