我正在制作一本类似Android的应用程序的电子书,我想过滤书的标题,但每次你在编辑文本中放一个单词或句子,它都会搜索书的内容...有人可以帮我这个......
答案 0 :(得分:15)
试试这个,每当你在edittext中输入文字时,列表会显示过滤结果,因为我已经在图片中显示了这些东西
<强>初始强>
<强>过滤强>
这是main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/etSearchbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="@+id/lvFirst"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
这是FilterListActivity.java
package com.filterlist;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class FilterListActivity extends Activity{
EditText etSearchbox;
ListView lvFirst;
ArrayAdapter<String> adapter1;
String[] data = {"mehul joisar","amit mishra","amitabh","Aamir khan","jesica","katrina"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etSearchbox=(EditText)findViewById(R.id.etSearchbox);
lvFirst=(ListView)findViewById(R.id.lvFirst);
lvFirst.setTextFilterEnabled(true);
adapter1 = new ArrayAdapter<String>(FilterListActivity.this, android.R.layout.simple_list_item_1, data);
lvFirst.setAdapter(adapter1);
etSearchbox.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
FilterListActivity.this.adapter1.getFilter().filter(arg0);
}
@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
}
});
}
}
答案 1 :(得分:4)
我有一个有效的例子,试试这个:
filterEditText = (EditText)findViewById(R.id.filter);
filterEditText.addTextChangedListener(filterTextWatcher);
TextWatcher filterTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
public void onTextChanged(CharSequence s, int start, int before,int count) {
adapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
适配器必须implements Filterable
@Override
public Filter getFilter() {
// Filter filter = null;
if(filter == null)
filter = new CheeseFilter();
return filter;
}
过滤类:
public class CheeseFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// TODO Auto-generated method stub
constraint = constraint.toString().toLowerCase();
FilterResults newFilterResults = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<String> auxData = new ArrayList<String>();
for (int i = 0; i < data.size(); i++) {
if (data.get(i).toLowerCase().contains(constraint))
auxData.add(data.get(i));
}
newFilterResults.count = auxData.size();
newFilterResults.values = auxData;
} else {
newFilterResults.count = data.size();
newFilterResults.values = data;
}
return newFilterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<String> resultData = new ArrayList<String>();
resultData = (ArrayList<String>) results.values;
EfficientAdapter adapter = new EfficientAdapter(context, resultData);
list.setAdapter(adapter);
// notifyDataSetChanged();
}
}
您可以查看此帖子了解更多信息:
答案 2 :(得分:0)
您需要为edittext编写textChangedlistener(或)TextWatcher。在监听器内部,您需要编写搜索逻辑。这是APIdoc。以下是textwatcher上的示例。
答案 3 :(得分:0)
使用自定义listView时
适配器:
public class Adapter extends ArrayAdapter {
ArrayList<String> list = new ArrayList<>();
ArrayList<String> filteredData = new ArrayList<>();
public Adapter(@NonNull Context context, int resource) {
super(context, resource);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
@SuppressLint("ViewHolder") View vi = inflate.inflate(R.layout.ly_items, null);
try {
JSONObject js = new JSONObject(list.get(position));
TextView txtItem = vi.findViewById(R.id.txtItem);
ImageView imgItem = vi.findViewById(R.id.imgItem);
txtItem.setText(js.getString("name") + " - " + js.getInt("number"));
Picasso.get().load(js.getString("logo_url")).into(imgItem);
} catch (JSONException e) {
e.printStackTrace();
}
return vi;
}
@Override
public void add(@Nullable Object object) {
super.add(object);
list.add(object.toString());
filteredData.add(object.toString());
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return list.get(position);
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
list.clear();
if (charText.length() == 0) {
list.addAll(filteredData);
} else {
for (String wp : filteredData) {
try {
JSONObject json = new JSONObject(wp);
if (json.getString("name").toLowerCase().contains(charText) || json.getString("number").contains(charText)) {
list.add(wp);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
notifyDataSetChanged();
}
}
还有你的班级
Adapter adapter;
ListView list;
EditText edtSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = findViewById(R.id.list);
edtSearch = findViewById(R.id.edtSearch);
adapter = new Adapter(this, android.R.layout.simple_list_item_1);
list.setAdapter(adapter);
edtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}