我在这篇文章中感到茫然,在阅读了几篇文章后,我仍然不知道如何处理我目前的情况。但是当我过滤我的Cursor Adapter时,列表不会更新或过滤任何内容,就像没有任何事情发生一样。
这是我查看客户列表的活动
public class ViewClientActivity extends Activity
{
EditText inputSearch;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.viewclient_activity);
inputSearch = (EditText) findViewById(R.id.search_view);
DBHandler handler = new DBHandler(this, null, null, 1);
SQLiteDatabase db = handler.getReadableDatabase();
final Cursor ClientCursor = db.rawQuery("SELECT * FROM clients", null);
final ListView allClients = (ListView) findViewById(R.id.allClients);
final ClientCursorAdapter clientAdapter = new ClientCursorAdapter(this, ClientCursor);
allClients.setAdapter(clientAdapter);
allClients.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Cursor cur = (Cursor) parent.getItemAtPosition(position);
int clientIdint = cur.getInt(cur.getColumnIndex("_id"));
String clientId = Integer.toString(clientIdint);
Intent AddClientIntent = new Intent(getApplicationContext(), AddClientActivity.class);
AddClientIntent.putExtra("CLIENT_IDINT", clientIdint);
AddClientIntent.putExtra("CLIENT_ID", clientId);
startActivity(AddClientIntent);
}
});
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3)
{
// When user changed the Text
clientAdapter.getFilter().filter(cs.toString());
allClients.setAdapter(clientAdapter);
}
@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
}
});
}
}
这是我的光标类
public class ClientCursorAdapter extends CursorAdapter
{
public ClientCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.display_client_row, parent, false);
}
// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView txtViewName = (TextView) view.findViewById(R.id.txtViewName);
TextView txtViewAddress = (TextView) view.findViewById(R.id.txtViewAddress);
TextView txtViewPhoneNum = (TextView) view.findViewById(R.id.txtViewPhoneNum);
// Extract properties from cursor
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
String address = cursor.getString(cursor.getColumnIndexOrThrow("address"));
String homenum = cursor.getString(cursor.getColumnIndexOrThrow("homenum"));
String cellnum = cursor.getString(cursor.getColumnIndexOrThrow("cellnum"));
String worknum = cursor.getString(cursor.getColumnIndexOrThrow("worknum"));
// Populate fields with extracted properties
txtViewName.setText(name);
txtViewAddress.setText(address);
if (!cellnum.equals("") && !cellnum.equalsIgnoreCase("cell phone"))
{
txtViewPhoneNum.setText(String.valueOf(cellnum));
}
else if (!homenum.equals("") && !homenum.equalsIgnoreCase("home phone"))
{
txtViewPhoneNum.setText(String.valueOf(homenum));
}
else if (!worknum.equals("") && !worknum.equalsIgnoreCase("work phone"))
{
txtViewPhoneNum.setText(String.valueOf(worknum));
}
else
{
txtViewPhoneNum.setText("No phone listed.");
}
}
}
感谢您的时间,如果您需要任何其他信息,请告诉我们!
答案 0 :(得分:0)
要过滤CursorAdapter
或SimpleCursorAdapter
,您必须使用clientAdapter.setFilterQueryProvider
,而不是过滤内容。
clientAdapter .setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
//your code here
}
});
您可以在此处查看更详细的说明:How to set Filter to ListView with CursorAdapter?
此外,您不必设置适配器两次从allClients.setAdapter(clientAdapter);
方法中删除addTextChangedListener
。如果需要,您可以致电notifyDataSetChanged();