这就是我现在所拥有的。当我在onitemselected上面使用时,我的listview会正常填充,但是当我将它注释掉或者当我尝试在spinner onitemselected中使用where子句时,它不起作用。
活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_supports);
try {
openDB();
} catch (SQLException e) {
e.printStackTrace();
}
//populateListFromDbSupports();
final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.typeArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ViewSupports.this,
"You selected " + spinner2.getSelectedItem(),
Toast.LENGTH_LONG).show();
final String type = (String) spinner2.getSelectedItem();
final ImageView im = (ImageView) findViewById(R.id.imageView2);
if (type.equals("One-Hole"))
im.setImageDrawable(getResources().getDrawable(R.drawable.a167));
//populateOneHole();
if (type.equals("Two-Hole"))
im.setImageDrawable(getResources().getDrawable(R.drawable.twohole));
populateTwoHole();
if (type.equals("One-Piece Strut"))
im.setImageDrawable(getResources().getDrawable(R.drawable.a174));
if (type.equals("Two-Piece Strut"))
im.setImageDrawable(getResources().getDrawable(R.drawable.a131));
if (type.equals("Mineralac"))
im.setImageDrawable(getResources().getDrawable(R.drawable.a116));
// myCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
// @Override
// public Cursor runQuery(CharSequence constraint) {
// return DB.fetchSupportsBySize(type);
//
// }
// });
Log.d(null, type, null);
//DB.fetchSupportsBySize(type);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
适配器
public static Cursor fetchSupportsBySize(String inputText) throws SQLiteException {
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null ||inputText.length() == 0) {
mCursor = db.query(SUPPORT_TABLE, new String[]{KEY_SUPPORT_ROWID, KEY_SUPPORT_SIZE, KEY_SUPPORT_TYPE, KEY_SUPPORT_MANUFACTURER, KEY_SUPPORT_PARTNUMBER},
null, null, null, null, null);
}
else {
mCursor = db.query(true, SUPPORT_TABLE, new String[] {KEY_SUPPORT_ROWID, KEY_SUPPORT_SIZE, KEY_SUPPORT_TYPE, KEY_SUPPORT_MANUFACTURER, KEY_SUPPORT_PARTNUMBER},
KEY_SUPPORT_SIZE + inputText, null, null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public static Cursor fetchAllSupports() {
Cursor mCursor = db.query(SUPPORT_TABLE, new String[] {KEY_SUPPORT_ROWID, KEY_SUPPORT_SIZE, KEY_SUPPORT_TYPE, KEY_SUPPORT_MANUFACTURER, KEY_SUPPORT_PARTNUMBER},
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public static Cursor getAllSupportsBySize() {
return db.query(SUPPORT_TABLE, null, "support_size=?", new String[]{"1/2"}, null, null, null);
}
public static Cursor getOneHoles() {
return db.query(SUPPORT_TABLE, null, "support_type=?", new String[]{"One-Hole"}, null, null, null);
}
public static Cursor getTwoHoles() {
return db.query(SUPPORT_TABLE, null, "support_type=?", new String[]{"Two-Hole"}, null, null, null);
}
});
}