猿问

使用 customlitview 进行 Android 操作栏搜索

我是 android 开发的新手。我有使用 Arrayadopter 的自定义列表视图我的所有数据都来自 sqlite 数据库并在此自定义列表视图中显示我如何在我的操作栏上实现搜索功能我尝试了许多其他代码但它在这里不起作用我拜托告诉我是否有经验的开发人员可以帮助我,我非常感谢他提前请告诉我?



有只小跳蛙
浏览 63回答 1
1回答

Helenr

对于您的适配器,您创建了一个名为filter的公共方法,它不是真正的Filter类。我建议使用 2 种不同的方法来使发布的代码正常工作。方法一:在Activity中,将:adapter.getFilter().filter(newText);改为adapter.filter(newText);。方法 2:由于您的数据是一个String列表并且 ArrayAdapter 已经是Filterable,所以保留adapter.getFilter().filter(newText);并使适配器像这样简单:public class ListViewAdapter extends ArrayAdapter {private MainActivity activity;public ListViewAdapter(MainActivity context, int resource, List<String> objects) {&nbsp; &nbsp; super(context, resource, objects);&nbsp; &nbsp; this.activity = context;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {&nbsp; &nbsp; ViewHolder holder;&nbsp; &nbsp; LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);&nbsp; &nbsp; // If holder not exist then locate all view from UI file.&nbsp; &nbsp; if (convertView == null) {&nbsp; &nbsp; &nbsp; &nbsp; // inflate UI from XML file&nbsp; &nbsp; &nbsp; &nbsp; convertView = inflater.inflate(R.layout.item_listview, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; // get all UI view&nbsp; &nbsp; &nbsp; &nbsp; holder = new ViewHolder(convertView);&nbsp; &nbsp; &nbsp; &nbsp; // set tag for holder&nbsp; &nbsp; &nbsp; &nbsp; convertView.setTag(holder);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // if holder created, get tag from view&nbsp; &nbsp; &nbsp; &nbsp; holder = (ViewHolder) convertView.getTag();&nbsp; &nbsp; }&nbsp; &nbsp; String item = (String)getItem(position);&nbsp; &nbsp; holder.friendName.setText(item);&nbsp; &nbsp; //get first letter of each String item&nbsp; &nbsp; String firstLetter = item.substring(0, 1);&nbsp; &nbsp; ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT&nbsp; &nbsp; // generate random color&nbsp; &nbsp; int color = generator.getColor(item);&nbsp; &nbsp; TextDrawable drawable = TextDrawable.builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .buildRound(firstLetter, color); // radius in px&nbsp; &nbsp; holder.imageView.setImageDrawable(drawable);&nbsp; &nbsp; return convertView;}private class ViewHolder {&nbsp; &nbsp; private ImageView imageView;&nbsp; &nbsp; private TextView friendName;&nbsp; &nbsp; public ViewHolder(View v) {&nbsp; &nbsp; &nbsp; &nbsp; imageView = (ImageView) v.findViewById(R.id.image_view);&nbsp; &nbsp; &nbsp; &nbsp; friendName = (TextView) v.findViewById(R.id.text);&nbsp; &nbsp; }}}我在 ListView 上的博客还包含带有过滤器的示例:http: //programandroidlistview.blogspot.com/希望对您有所帮助!
随时随地看视频慕课网APP

相关分类

Java
我要回答