gpt4 book ai didi

java - 适配器 getFilter 不过滤

转载 作者:太空宇宙 更新时间:2023-11-04 11:26:02 26 4
gpt4 key购买 nike

我对这个感到茫然,读了几篇文章后我仍然不知道如何处理我目前的情况。但是,当我过滤游标适配器时,列表不会更新或过滤任何内容,就像什么都没有发生一样。

这是我查看客户列表的 Activity

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.");
}

}

}

感谢您的宝贵时间,如果您需要任何其他信息,请告诉我!

最佳答案

要过滤 CursorAdapterSimpleCursorAdapter,您必须使用 clientAdapter.setFilterQueryProvider 才能过滤内容。

  clientAdapter .setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
//your code here
}

});

您可以在这里查看更详细的说明:How to set Filter to ListView with CursorAdapter?此外,您不必设置适配器两次,从 addTextChangedListener 方法中删除 allClients.setAdapter(clientAdapter); 。如果需要,您可以调用 notifyDataSetChanged();

关于java - 适配器 getFilter 不过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44358333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com