嗨,我只是在学习android我有一张桌子
rollno类别付费日期金额
1 Rent Cash 11/11/2014 10
2实用卡12/11/2014 20
3运输现金15/12/2014 10
我在表中使用spinner for Category Column来选择数据类别,我希望用户能够根据他们选择的类别(租金,运输,实用程序等)在此表中查询,如果用户选择租他们看到它的所有数据加上我希望查询总计租金类别金额列的总和。我希望我没有混淆任何一个我非常感谢你的导游谢谢。
此时此刻只能选择下面的一个 如果(图== btnShowInfo) { 游标c = db.rawQuery(“SELECT * FROM expense WHERE Category ='Utility'”,null);她是我的代码:
public class MainActivity extends Activity implements OnClickListener
{
EditText editRollno,editName,editamount ,category,editDate,Category,Paidby,amount,expense;
int Amount;
Button btnAdd,btnDelete,btnModify,btnView,btnViewAll,btnShowInfo;
Spinner mCategory;
Spinner SpPayType;
ArrayAdapter<CharSequence> adapter;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editRollno=(EditText)findViewById(R.id.editRollno);
// Spinners
mCategory = (Spinner)findViewById(R.id.spCategory);
SpPayType = (Spinner)findViewById(R.id.sp_PayType);
adapter = ArrayAdapter.createFromResource(this, R.array.Expense_Category,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mCategory.setAdapter(adapter);
adapter = ArrayAdapter.createFromResource(this, R.array.Payment_Option,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SpPayType.setAdapter(adapter);
editDate =(EditText)findViewById(R.id.editDate);
editamount =(EditText)findViewById(R.id.editAmount);
btnAdd =(Button)findViewById(R.id.btnAdd);
btnDelete =(Button)findViewById(R.id.btnDelete);
btnModify =(Button)findViewById(R.id.btnModify);
btnView =(Button)findViewById(R.id.btnView);
btnViewAll =(Button)findViewById(R.id.btnViewAll);
btnShowInfo =(Button)findViewById(R.id.btnShowInfo);
btnAdd.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnModify.setOnClickListener(this);
btnView.setOnClickListener(this);
btnViewAll.setOnClickListener(this);
btnShowInfo.setOnClickListener(this);
db=openOrCreateDatabase("HHECDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS expense(rollno INTEGER PRIMARY KEY ,Category VARCHAR, Paidby VARCHAR,date VARCHAR, amount VARCHAR);");
}
public void onClick(View view)
{
if(view==btnAdd)
{
if(editRollno.getText().toString().trim().length()==0||
mCategory.getSelectedItem().toString().trim().length()==0||
SpPayType.getSelectedItem().toString().trim().length()==0||
editDate.getText().toString().trim().length()==0||
editamount.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO expense VALUES('"+editRollno.getText()+"','"+mCategory.getSelectedItem()+
"','"+SpPayType.getSelectedItem()+"','"+editDate.getText()+"','"+editamount.getText()+"');");
showMessage("Success", "Record added");
clearText();
}
if(view==btnDelete)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM expense WHERE rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM expense WHERE rollno='"+editRollno.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
if(view==btnModify)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM expense WHERE rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
/* db.execSQL("UPDATE student SET name='"+editName.getText()+"',marks='"+editMarks.getText()+
"' WHERE rollno='"+editRollno.getText()+"'"); */
db.execSQL("UPDATE expense SET Category='"+mCategory.getSelectedItem()+"',paidby ='"+SpPayType.getSelectedItem()+"',date='"+editDate.getText()+
"',amount='"+editamount.getText()+
"' WHERE rollno='"+editRollno.getText()+"'");
showMessage("Success", "Record Modified");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
if(view==btnView)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM expense WHERE rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
///
Category.setText(c.getString(1));
Paidby.setText(c.getString(3));
editDate.setText(c.getString(3));
editamount.setText(c.getString(4));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
if(view==btnViewAll)
{
Cursor c=db.rawQuery("SELECT * FROM expense", null);
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Rollno: "+c.getString(0)+"\n");
buffer.append("Category: "+c.getString(1)+"\n");
buffer.append("Paid By : "+c.getString(2)+"\n");
buffer.append("Date : "+c.getString(3)+"\n");
buffer.append("Amount: "+c.getString(4)+"\n\n");
}
showMessage("Expense Details", buffer.toString());
}/* this wher i want the user if showInfo btn pressed be able to choose all category */
if(view==btnShowInfo)
{
Cursor c=db.rawQuery("SELECT * FROM expense WHERE Category ='Utility' ", null);
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Rollno: "+c.getString(0)+"\n");
buffer.append("Category: "+c.getString(1)+"\n");
buffer.append("Paid By : "+c.getString(2)+"\n");
buffer.append("Date : "+c.getString(3)+"\n");
buffer.append("Amount: "+c.getString(4)+"\n\n");
}
showMessage("Expense Details", buffer.toString());
}}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
editRollno.setText("");
mCategory.setSelected(true);
SpPayType.setSelected(true);
editDate.setText("");
editamount.setText("");
editRollno.requestFocus();
}
}
答案 0 :(得分:0)
尝试此查询
SELECT rollno, 类别, SUM(量) 从费用 其中Category =“Selected Category” GROUP BY rollno, 分类