0

我正在尝试从 sqlite db 中获取包含总金额的名称列表。它的工作方式是显示具有正确组合总数的所有交易的列表。我在同一个数据库中也有一个包含用户名和电话号码的表,但我认为这对这个活动没有太大用处。另外,我如何使用 onListItemClick 发送下一个活动的东西,我可以用它来只从用户选择的人那里提取名称?正在发送 ID,但我不知道如何使用它。

即:反表:
贾斯汀25
贾斯汀25
贾斯汀25
索菲亚80

希望的结果:
贾斯汀 75
索菲亚 80

实际结果:
贾斯汀 75
贾斯汀 75
贾斯汀 75
索菲亚 80

填充列表的 ListActivity(带有光标和 TextView 链接)

public class Totals extends ListActivity {
PaymentHelper helper;
Cursor model = null;
PaymentAdapter adapter = null;

UserHelper uhelp;
Cursor umodel = null;

public final static String ID_EXTRA = "com.curtis.bookkeeping._ID";

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_person);

    helper = new PaymentHelper(this);
    model = helper.getAll();

    startManagingCursor(model);

    adapter = new PaymentAdapter(model);
    setListAdapter(adapter);
}

public void onDestroy() {
    super.onDestroy();
    helper.close();
}

@Override
public void onListItemClick(ListView list, View view, int position, long id) {
    Intent i = new Intent(Totals.this, Detail.class);
    i.putExtra(ID_EXTRA, String.valueOf(id));
    startActivity(i);
}
public class PaymentAdapter extends CursorAdapter {
    PaymentAdapter(Cursor c) {
        super(Totals.this, c, FLAG_REGISTER_CONTENT_OBSERVER);
    }

    @Override
    public void bindView(View row, Context context, Cursor c) {
        PaymentHolder holder = (PaymentHolder)row.getTag();
        holder.populateFrom(c, helper);
    }

    @Override
    public View newView(Context context, Cursor c, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.person_row, parent, false);

        PaymentHolder holder = new PaymentHolder(row);
        row.setTag(holder);
        return row;
    }

}
static class PaymentHolder {

    private TextView name_line = null;
    private TextView amount_line = null;

    PaymentHolder(View row) {
        name_line = (TextView)row.findViewById(R.id.name_row);
        amount_line = (TextView)row.findViewById(R.id.amount_row);
    }

    void populateFrom(Cursor c, PaymentHelper helper) {
        name_line.setText(helper.getName(c));
        amount_line.setText(Integer.toString(helper.sumPerson(c, helper.getName(c))));
    }
}

}

检索信息的 SQLiteOpenHelper 代码

public class PaymentHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "bookkeeping.db";
private static final int SCHEMA_VERSION = 1;
SQLiteDatabase db;

public PaymentHelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db1) {
    db = db1;
    String sql = "CREATE TABLE IF NOT EXISTS trans (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, date TEXT, amount INT, note TEXT)";
    //execute the sql statement
    db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void insert(String name, String date, int amount, String note){
    Log.e(name, date + " " + amount);
    db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put("name", name);
    cv.put("date", date);
    cv.put("amount", amount);
    cv.put("note", note);
    Log.e("Almost", "there");
    db.insert("trans", "abc", cv);
    Log.e("successfully", "inserted");
}
public Cursor getAll(){
    String sql = "SELECT * FROM trans ORDER BY name";
    Cursor cursor = getReadableDatabase().rawQuery(sql, null);
    return cursor;
}
public Cursor getAllNames(){
    String sql = "SELECT * FROM users";
    Cursor cursor = getReadableDatabase().rawQuery(sql, null);
    return cursor;
}
public String getName(Cursor c){
    return c.getString(c.getColumnIndex("name"));
}
public String getDate(Cursor c){
    return c.getString(c.getColumnIndex("date"));
}
public int getAmount(Cursor c){
    return c.getInt(c.getColumnIndex("amount"));
}
public String getNote(Cursor c){
    return c.getString(c.getColumnIndex("note"));
}
public void delete(String id){
    String[] args = {id};
    getWritableDatabase().delete("trans", "_id=?", args);
}
public Cursor getById(String id){
    String[] args = {id};
    String sql = "SELECT * FROM trans WHERE _id=?";
    Cursor cursor = getReadableDatabase().rawQuery(sql, args);
    return cursor;
}
public void update(String id, String name, String date, int amount, String note){
    String[] args = {id};
    ContentValues cv = new ContentValues();
    cv.put("name", name);
    cv.put("date", date);
    cv.put("amount", amount);
    cv.put("note", note);
    getWritableDatabase().update("trans", cv, "_ID=?", args);
}
public int sumPerson(Cursor c, String name){
    int total = 0;
    // add up totals
    String sql = "SELECT amount FROM trans WHERE name=?";
    String[] aname = new String[]{name};
    getReadableDatabase().rawQuery(sql,aname);
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        if(name.equals(getName(c))){
            total += c.getInt(c.getColumnIndex("amount"));
        }
    }
    return total;
}

}

这是从 Totals 接收 ID 的活动:我希望它只显示一个用户(他们从总计页面选择)及其所有交易。

public class Detail extends ListActivity {
PaymentHelper helper;
Cursor model = null;
PaymentAdapter adapter = null;
public final static String ID_EXTRA = "com.curtis.bookkeeping._ID";

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view);

    helper = new PaymentHelper(this);
    model = helper.getAll();
    startManagingCursor(model);

    adapter = new PaymentAdapter(model);
    setListAdapter(adapter);


}

public void onDestroy() {
    super.onDestroy();
    helper.close();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.details_menu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.totals:
            startActivity(new Intent(this, Totals.class));
            break;
        case R.id.users:
            startActivity(new Intent(this, Users.class));
            break;
        case R.id.home:
            startActivity(new Intent(this, MainMenu.class));
            break;
    }
    return true;
}

@Override
public void onListItemClick(ListView list, View view, int position, long id) {
    Intent i = new Intent(Detail.this, DeletePayment.class);
    i.putExtra(ID_EXTRA, String.valueOf(id));
    startActivity(i);
}

public class PaymentAdapter extends CursorAdapter {
    PaymentAdapter(Cursor c) {
        super(Detail.this, c, FLAG_REGISTER_CONTENT_OBSERVER);
    }

    @Override
    public void bindView(View row, Context context, Cursor c) {
        PaymentHolder holder = (PaymentHolder)row.getTag();
        holder.populateFrom(c, helper);
    }

    @Override
    public View newView(Context context, Cursor c, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row, parent, false);

        PaymentHolder holder = new PaymentHolder(row);
        row.setTag(holder);
        return row;
    }

}
static class PaymentHolder {

    private TextView name_line = null;
    private TextView date_line = null;
    private TextView amount_line = null;
    private TextView note_line = null;

    PaymentHolder(View row) {
        name_line = (TextView)row.findViewById(R.id.name_line);
        amount_line = (TextView)row.findViewById(R.id.amount_line);
        date_line = (TextView)row.findViewById(R.id.date_line);
        note_line = (TextView)row.findViewById(R.id.note_line);
    }

    void populateFrom(Cursor c, PaymentHelper helper) {
        Log.e(helper.getName(c), Integer.toString(helper.getAmount(c)));
        name_line.setText(helper.getName(c));
        date_line.setText(helper.getDate(c));
        amount_line.setText(Integer.toString(helper.getAmount(c)));
        note_line.setText(helper.getNote(c));
    }
}
}

我知道这很长......但帮助会很棒!

如果只有两个名称,我觉得“PaymentAdapter”需要修改为只读取两个名称。我应该使用“UserHelper”数据库助手来填充它吗?但是当我这样做时,它只运行一个游标,并得到一个 nullpointerexception 错误,因为它没有移动其中一个游标。我应该在 PaymentAdapter 中创建一个 PaymentAdapter 以使用另一个游标吗?

4

1 回答 1

1

以下 SQL 查询将为您提供所需的结果:

SELECT name, SUM(amount)
FROM trans
GROUP BY name
于 2013-08-08T19:42:04.537 回答