0
嗨,我剛學的Android我有一個表如何根據列下拉值獲取查詢的選擇並獲得另一列的總和?
rollno類別Paidby日金額
1租金的現金2014年11月11日10
2公用事業卡2014年12月11日20
3運輸現金15/12/2014 10
我正在使用微調錶中的類別列來選擇數據類別,我想要什麼用戶能夠根據他們選擇的範例(租金,運輸,公用事業...)在這個表格中查詢如果用戶選擇租金,他們看到所有的數據加上我想要查詢總計租金類別金額列。我希望我沒有困惑任何一個我真的很感謝你的指導謝謝。
這個目前只能選擇一個像下面 如果(查看== btnShowInfo) { 光標C = db.rawQuery( 「SELECT * FROM費用類別爲 '實用'」,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();
}
}