我在这个网站上经历了几个问题。但是,没有一个解决方案对我有用。我不确定,但我的代码也可能有点不同。
问题:
我有一个listView,它使用来自预先存在的数据库的数据从SimpleCursorAdapter填充。我使用外部库来实现这一目标。现在我想实现过滤,以便当用户键入几个字母时,listView应显示过滤结果。我也试过使用SearchView,但无济于事。如果有人能指出我正确的方向,我会很高兴。
以下是代码 的 Display_Spinner_Info.java
public class Display_Spinner_Info extends Activity implements SearchView.OnQueryTextListener{
private SearchView mSearchView;
private EditText ed1;
private AutoCompleteTextView tv4;
private ListView list1;
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_display__spinner__info);
// Show the Up button in the action bar.
setupActionBar();
mSearchView = (SearchView) findViewById(R.id.search_view);
tv4 = (AutoCompleteTextView) findViewById(R.id.act1);
Cursor employees;
//Cursor hello;
MyDatabase db;
db = new MyDatabase(this);
Intent intent = getIntent();
final String m=intent.getStringExtra("branch");
if(intent.getStringExtra("branch").contentEquals("SELECT"))
{
employees = db.getCursor("sel");
}
else
{
//Toast.makeText(this, m, Toast.LENGTH_SHORT).show();
employees = db.getCursor(m.toLowerCase());
}
//hello = db.getRecord();// you would not typically call this on the main thread
Toast.makeText(this, employees.getString(1), Toast.LENGTH_SHORT).show();
list1 = (ListView) findViewById(R.id.listView1);
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
employees,
new String[] {"name"},
new int[] {android.R.id.text1},0);
list1.setTextFilterEnabled(true);
list1.setFastScrollEnabled(true);
list1.setAdapter(adapter);
setupSearchView();
//adapter.setStringConversionColumn(employees.getColumnIndex(db.getDatabaseName()))
tv4.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
adapter.getFilter().filter(s.toString());
//Toast.makeText(getApplicationContext(), "Text changed"+s, Toast.LENGTH_SHORT).show();
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
MyDatabase db1 = new MyDatabase(getApplicationContext());
Toast.makeText(getApplicationContext(), constraint, Toast.LENGTH_SHORT).show();
Cursor ab = db1.ans(constraint);
String abc = ab.toString();
return ab;
//return db1.suggestItemCompletions(partialItemName);
}
});
list1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Cursor d = (Cursor) list1.getItemAtPosition(position);
String name = d.getString(1);
Intent intent1 = new Intent (getApplication() , Clickone.class );
intent1.putExtra("Fname", name);
intent1.putExtra("branch", m);
startActivity(intent1);
overridePendingTransition(R.anim.anim1, R.anim.anim2);
}
});
overridePendingTransition(R.anim.anim2, R.anim.anim1);
}
private void setupSearchView() {
// TODO Auto-generated method stub
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here");
}
/*private void setupSearchView() {
// TODO Auto-generated method stub
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here");
}*/
/**
* Set up the {@link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display__spinner__info, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
onSearchRequested();
break;
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
if (TextUtils.isEmpty(newText)) {
list1.clearTextFilter();
} else {
list1.setFilterText(newText.toString());
}
return true;
}
@Override
public boolean onQueryTextSubmit(String arg0) {
// TODO Auto-generated method stub
return false;
}
}
MyDatabase.java
public class MyDatabase extends SQLiteAssetHelper{
int a;
public static final String TAG = "HELLO";
public static String ans = "";
private static final String DATABASE_NAME = "scse.db";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public Cursor getCursor(String g) {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String [] sqlSelect = {"0 _id", "name","dec", "cabin","email"};
String sqlTables = g;
qb.setTables(sqlTables);
//qb.s;
//startManagingCursor(a);
Cursor c = qb.query(db,sqlSelect , null, null,
null, null, null);
/*Cursor d = db.rawQuery("SELECT Fac_Cabin FROM SCSE_FAC WHERE Fac_Name = ?", new String[] {"Senthil J"});*/
/*ex = extract.toString();*/
c.moveToFirst();
return c;
}
public Cursor ans(CharSequence str)
{
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
Cursor a = db.query(getDatabaseName(), new String[]{"name"}, "name LIKE '%"+str.toString()+"%'", null, null, null, null, null);
a.close();
return a;
}
}