Android SQLite 使用两个微调器作为视图过滤器
Android SQLite Using two spinners as view filters
主要问题:
想知道当我尝试使用 LoadGrid 显示结果时这两个微调器是否有任何冲突。就像一个人压倒另一个人并因此而得到一个空结果一样。如果是这样,我该如何结合微调器的两个结果来获得所需的视图。
一些细节:
我想在我的 gridview 上使用两个微调器作为过滤器。一份用于条款,一份用于状态。具体来说,我想要发生的是,用户可以单独或一起使用这两个微调器来过滤数据库中的帐户视图。
一步一个脚印,我得到了一个微调器 运行 并且工作正常。所以我克隆了那个工作微调器代码来启动我的第二个微调器代码 运行。不,不工作。现在使用两个微调器中的任何一个都不会产生任何结果。不再显示任何帐户。它会让我从微调器中 select 但我的网格仍然是空的。
如果有任何遗漏或您需要查看更多代码,请随时询问。干杯。
这是旋转器片段:
Utilities.ManageTermSpinner(this.getParent(), spinTerm);
try {
spinTerm.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
LoadGrid();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
} catch (Exception ex) {
txtTest.setText(ex.toString());
}
Utilities.ManageStatSpinner(this.getParent(), spinStat);
try {
spinStat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
LoadGrid();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
} catch (Exception ex) {
txtTest2.setText(ex.toString());
}
}
这是它引用的 LoadGrid:
public void LoadGrid() {
dbHelper = new DatabaseHelper(this);
try {
View v = spinTerm.getSelectedView();
TextView txt = (TextView) v.findViewById(R.id.txtTermClass);
String Terms = String.valueOf(txt.getText());
Cursor c = dbHelper.getAccByTerms(Terms);
startManagingCursor(c);
View x = spinStat.getSelectedView();
TextView txt2 = (TextView) x.findViewById(R.id.txtStatID);
String Status = String.valueOf(txt2.getText());
Cursor b = dbHelper.getAccByStatus(Status);
startManagingCursor(b);
String[] from = new String[]{DatabaseHelper.colName, DatabaseHelper.colAmount, DatabaseHelper.colTermsClass, DatabaseHelper.colStatClass};
int[] to = new int[]{R.id.colName, R.id.colAmount, R.id.colTerms, R.id.colStat};
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.gridrow, c, from, to);
grid.setAdapter(sca);
SimpleCursorAdapter sba = new SimpleCursorAdapter(this, R.layout.gridrow, b, from, to);
grid.setAdapter(sba);
} catch (Exception ex) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setMessage(ex.toString());
b.show();
}
}
条款和状态的 dbHelper 代码段:
public Cursor getAccByTerms(String Terms)
{
SQLiteDatabase db=this.getReadableDatabase();
String [] columns=new String[]{"_id",colName,colAmount,colPurpose,colTermsClass,colDate,colEditDate,colStatClass};
Cursor c=db.query(viewAccs, columns, colTermsClass+"=?", new String[]{Terms}, null, null, null);
return c;
}
public Cursor getAccByStatus(String Status)
{
SQLiteDatabase db=this.getReadableDatabase();
String [] columns=new String[]{"_id",colName,colAmount,colPurpose, colTermsClass,colDate,colEditDate,colStatClass};
Cursor d=db.query(viewAccs,columns, colStatClass +"=?",new String[]{Status},null,null,null);
return d;
}
还有我保存 ManageSpinner 内容的实用程序片段:
public class Utilities {
static public void ManageTermSpinner(Context context, Spinner view) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
Cursor c = dbHelper.getAllTerms();
SimpleCursorAdapter ca = new SimpleCursorAdapter(context, R.layout.termspinnerrow, c, new String[]{DatabaseHelper.colTermsClass, "_id"}, new int[]{R.id.txtTermClass});
view.setAdapter(ca);
}
static public void ManageStatSpinner(Context context, Spinner view) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
Cursor d = dbHelper.getAllStatus();
SimpleCursorAdapter da = new SimpleCursorAdapter(context,R.layout.statspinnerrow, d, new String[]{DatabaseHelper.colStatClass,"_id"}, new int[]{R.id.txtStatClass});
view.setAdapter(da);
}
这就是你需要做的:
public Cursor getAccByTermsAndStatus(String Terms, String status)
{
SQLiteDatabase db=this.getReadableDatabase();
String [] columns=new String[]{"_id",colName,colAmount,colPurpose,colTermsClass,colDate,colEditDate,colStatClass};
Cursor c=db.query(viewAccs, columns, colTermsClass+"=? AND " + colStatClass + "=?", new String[]{Terms, status}, null, null, null);
return c;
}
主要问题:
想知道当我尝试使用 LoadGrid 显示结果时这两个微调器是否有任何冲突。就像一个人压倒另一个人并因此而得到一个空结果一样。如果是这样,我该如何结合微调器的两个结果来获得所需的视图。
一些细节:
我想在我的 gridview 上使用两个微调器作为过滤器。一份用于条款,一份用于状态。具体来说,我想要发生的是,用户可以单独或一起使用这两个微调器来过滤数据库中的帐户视图。
一步一个脚印,我得到了一个微调器 运行 并且工作正常。所以我克隆了那个工作微调器代码来启动我的第二个微调器代码 运行。不,不工作。现在使用两个微调器中的任何一个都不会产生任何结果。不再显示任何帐户。它会让我从微调器中 select 但我的网格仍然是空的。
如果有任何遗漏或您需要查看更多代码,请随时询问。干杯。
这是旋转器片段:
Utilities.ManageTermSpinner(this.getParent(), spinTerm);
try {
spinTerm.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
LoadGrid();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
} catch (Exception ex) {
txtTest.setText(ex.toString());
}
Utilities.ManageStatSpinner(this.getParent(), spinStat);
try {
spinStat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
LoadGrid();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
} catch (Exception ex) {
txtTest2.setText(ex.toString());
}
}
这是它引用的 LoadGrid:
public void LoadGrid() {
dbHelper = new DatabaseHelper(this);
try {
View v = spinTerm.getSelectedView();
TextView txt = (TextView) v.findViewById(R.id.txtTermClass);
String Terms = String.valueOf(txt.getText());
Cursor c = dbHelper.getAccByTerms(Terms);
startManagingCursor(c);
View x = spinStat.getSelectedView();
TextView txt2 = (TextView) x.findViewById(R.id.txtStatID);
String Status = String.valueOf(txt2.getText());
Cursor b = dbHelper.getAccByStatus(Status);
startManagingCursor(b);
String[] from = new String[]{DatabaseHelper.colName, DatabaseHelper.colAmount, DatabaseHelper.colTermsClass, DatabaseHelper.colStatClass};
int[] to = new int[]{R.id.colName, R.id.colAmount, R.id.colTerms, R.id.colStat};
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.gridrow, c, from, to);
grid.setAdapter(sca);
SimpleCursorAdapter sba = new SimpleCursorAdapter(this, R.layout.gridrow, b, from, to);
grid.setAdapter(sba);
} catch (Exception ex) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setMessage(ex.toString());
b.show();
}
}
条款和状态的 dbHelper 代码段:
public Cursor getAccByTerms(String Terms)
{
SQLiteDatabase db=this.getReadableDatabase();
String [] columns=new String[]{"_id",colName,colAmount,colPurpose,colTermsClass,colDate,colEditDate,colStatClass};
Cursor c=db.query(viewAccs, columns, colTermsClass+"=?", new String[]{Terms}, null, null, null);
return c;
}
public Cursor getAccByStatus(String Status)
{
SQLiteDatabase db=this.getReadableDatabase();
String [] columns=new String[]{"_id",colName,colAmount,colPurpose, colTermsClass,colDate,colEditDate,colStatClass};
Cursor d=db.query(viewAccs,columns, colStatClass +"=?",new String[]{Status},null,null,null);
return d;
}
还有我保存 ManageSpinner 内容的实用程序片段:
public class Utilities {
static public void ManageTermSpinner(Context context, Spinner view) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
Cursor c = dbHelper.getAllTerms();
SimpleCursorAdapter ca = new SimpleCursorAdapter(context, R.layout.termspinnerrow, c, new String[]{DatabaseHelper.colTermsClass, "_id"}, new int[]{R.id.txtTermClass});
view.setAdapter(ca);
}
static public void ManageStatSpinner(Context context, Spinner view) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
Cursor d = dbHelper.getAllStatus();
SimpleCursorAdapter da = new SimpleCursorAdapter(context,R.layout.statspinnerrow, d, new String[]{DatabaseHelper.colStatClass,"_id"}, new int[]{R.id.txtStatClass});
view.setAdapter(da);
}
这就是你需要做的:
public Cursor getAccByTermsAndStatus(String Terms, String status)
{
SQLiteDatabase db=this.getReadableDatabase();
String [] columns=new String[]{"_id",colName,colAmount,colPurpose,colTermsClass,colDate,colEditDate,colStatClass};
Cursor c=db.query(viewAccs, columns, colTermsClass+"=? AND " + colStatClass + "=?", new String[]{Terms, status}, null, null, null);
return c;
}