【发布时间】:2017-06-04 20:10:12
【问题描述】:
我对此一无所知,在阅读了几篇帖子后,我仍然不知道如何处理我目前的情况。但是当我过滤我的光标适配器时,列表不会更新或过滤任何内容,就像什么都没发生一样。
这是我查看客户列表的活动
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.");
}
}
}
感谢您的宝贵时间,如果您需要任何其他信息,请告诉我!
【问题讨论】:
-
addTextChangedListener不能直接与CursorAdapter一起使用 -
所以要制作一个新的适配器?