问题描述
我试图实施用getFilter()
函数在我的的ListView
,但每次我在的EditText 我的的ListView
消失。
我的 SetHelpRows
文件:
公共类SetHelpRows {
字符串名称;
字符串ID;
公共字符串的getName(){
返回名称;
}
公共无效setname可以(字符串名称){
this.name =名称;
}
公共字符串的getID(){
返回ID;
}
公共无效SETID(字符串ID){
this.id = ID;
}
公共SetHelpRows(字符串名称,字符串ID){
超();
this.name =名称;
this.id = ID;
}
}
我的 SetHelpRowsCustomAdapter
文件:
进口的java.util.ArrayList;
进口android.app.Activity;
进口android.content.Context;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.Filter;
进口android.widget.ImageView;
进口android.widget.TextView;
公共类SetRowsCustomAdapter扩展ArrayAdapter< SetRows> {
上下文语境;
INT layoutResourceId;
ArrayList的< SetRows>数据=新的ArrayList< SetRows>();
私人的ArrayList< SetRows>原版的;
私人的ArrayList< SetRows> fitems;
私人过滤器过滤;
公共SetRowsCustomAdapter(上下文的背景下,诠释layoutResourceId,ArrayList的< SetRows>数据){
超级(上下文,layoutResourceId,数据);
this.layoutResourceId = layoutResourceId;
this.context =背景;
this.data =数据;
this.original =数据; //新的ArrayList< Pkmn>();
this.fitems =数据; //新的ArrayList< Pkmn>();
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看排= convertView;
imageHolder的持有人= NULL;
如果(行== NULL)
{
LayoutInflater充气=((活动)上下文).getLayoutInflater();
行= inflater.inflate(layoutResourceId,父母,假);
持有人=新imageHolder的();
holder.txtTitle =(TextView中)row.findViewById(R.id.txtTitle);
holder.imgIcon =(ImageView的)row.findViewById(R.id.imgIcon);
holder.txtID =(TextView中)row.findViewById(R.id.txtModDate);
row.setTag(保持器);
}
其他
{
支架=(imageHolder的)row.getTag();
}
SetRows MYIMAGE = data.get(位置);
holder.txtTitle.setText(myImage.name);
holder.txtID.setText(myImage.id);
INT outImage = myImage.image;
holder.imgIcon.setImageResource(outImage);
返回行;
}
静态类imageHolder的
{
ImageView的imgIcon;
TextView的txtTitle;
TextView的txtID;
}
@覆盖
公共过滤用getFilter()
{
如果(过滤== NULL){
Log.i(过滤器前,过滤器前);
过滤器=新PkmnNameFilter();
}
返回过滤器;
}
私有类PkmnNameFilter扩展过滤器
{
@覆盖
保护FilterResults performFiltering(CharSequence的约束)
{
FilterResults结果=新FilterResults();
字符串preFIX = constraint.toString()与toLowerCase()。
如果(preFIX == NULL || prefix.length()== 0)
{
Log.i(preFIX为空或0,preFIX为空或0);
ArrayList的< SetRows>名单=新的ArrayList< SetRows>(原件);
results.values =清单;
results.count =则为list.size();
}
其他
{
Log.i(preFIX为空或0!,preFIX为空或0!);
最后的ArrayList< SetRows>名单=新的ArrayList< SetRows>(原件);
最后的ArrayList< SetRows> NLIST =新的ArrayList< SetRows>();
诠释计数=则为list.size();
的for(int i = 0; I<计数;我++)
{
最终SetRows pkmn = list.get(ⅰ);
最终的字符串值= pkmn.getName()与toLowerCase()。
如果(value.startsWith(preFIX))
{
nlist.add(pkmn);
}
}
results.values = NLIST;
results.count = nlist.size();
}
返回结果;
}
@燮pressWarnings(未登记)
@覆盖
保护无效publishResults(CharSequence的约束,FilterResults结果){
Log.i(公布结果,发布结果);
fitems =(ArrayList的< SetRows>)results.values;
明确();
诠释计数= fitems.size();
的for(int i = 0; I<计数;我++)
{
SetRows pkmn =(SetRows)fitems.get(ⅰ);
加(pkmn);
}
}
}
}
我的偏 MainActivity
文件:
inputSearch =(EditText上)findViewById(R.id.etSearch);
DataList控件=(ListView控件)findViewById(R.id.lvFiles);
对于(INT Y = 0; Y< strNames.length; Y ++){
名称= strNamesOfAllah [Y]
意思= strMeaning [Y]
rowsArray.add(新SetRows(R.drawable.icon,名字,意思));
}
适配器=新SetRowsCustomAdapter(MainActivity.this,R.layout.customlist,rowsArray);
dataList.setAdapter(适配器);
dataList.setClickable(真正的);
inputSearch.addTextChangedListener(新TextWatcher(){
@覆盖
公共无效onTextChanged(CharSequence的CS,诠释ARG1,诠释ARG2,诠释ARG3){
//当用户更改文本
//MainActivity.this.adapter.getFilter().filter(cs);
//字符串文本= inputSearch.getText()的toString()与toLowerCase()。
。adapter.getFilter()滤波器(cs.toString()与toLowerCase(Locale.US));
}
@覆盖
公共无效beforeTextChanged(CharSequence的arg0中,诠释ARG1,诠释ARG2,诠释ARG3){
// TODO自动生成方法存根
}
@覆盖
公共无效afterTextChanged(编辑为arg0){
// TODO自动生成方法存根
}
});
在 LogCat中
我看到以下内容:
12月12日至一十二日:15:57.077:I /前滤波器(720):在过滤器
十二月12号至12号:15:57.106:I / preFIX为空或0(720):!preFIX为空或0!
十二月12号至12号:15:57.216:I /公布结果(720):公布结果
我修改了code,并能得到它的工作。如果有人想用它,是我的客人:)
SetRows的Java文件:
公共类SetRows {
INT形象;
字符串名称;
字符串ID;
公众诠释的getImage(){
返回形象;
}
公共无效setImage(INT图像){
this.image =图像;
}
公共字符串的getName(){
返回名称;
}
公共无效setname可以(字符串名称){
this.name =名称;
}
公共字符串的getID(){
返回ID;
}
公共无效SETID(字符串ID){
this.id = ID;
}
公共SetRows(INT图像,字符串名称,字符串ID){
超();
this.image =图像;
this.name =名称;
this.id = ID;
}
@覆盖
公共字符串的toString(){
返回图像++姓名++ ID;
}
}
SetRowsCustomAdapter的Java文件:
进口的java.util.ArrayList;
进口android.app.Activity;
进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.Filter;
进口android.widget.ImageView;
进口android.widget.TextView;
公共类SetRowsCustomAdapter扩展ArrayAdapter< SetRows> {
上下文语境;
INT layoutResourceId;
ArrayList的< SetRows>数据=新的ArrayList< SetRows>(); //数据= countryList
私人的ArrayList< SetRows> originalList;
私人NameFilter过滤器;
公共SetRowsCustomAdapter(上下文的背景下,诠释layoutResourceId,ArrayList的< SetRows>数据){
超级(上下文,layoutResourceId,数据);
this.layoutResourceId = layoutResourceId;
this.context =背景;
this.data =数据;
//this.data.addAll(data);
this.originalList =新的ArrayList< SetRows>();
this.originalList.addAll(数据);
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看排= convertView;
imageHolder的持有人= NULL;
如果(行== NULL)
{
LayoutInflater充气=((活动)上下文).getLayoutInflater();
行= inflater.inflate(layoutResourceId,父母,假);
持有人=新imageHolder的();
holder.txtTitle =(TextView中)row.findViewById(R.id.txtTitle);
holder.imgIcon =(ImageView的)row.findViewById(R.id.imgIcon);
holder.txtID =(TextView中)row.findViewById(R.id.txtModDate);
row.setTag(保持器);
}
其他
{
支架=(imageHolder的)row.getTag();
}
SetRows MYIMAGE = data.get(位置);
holder.txtTitle.setText(myImage.name);
holder.txtID.setText(myImage.id);
INT outImage = myImage.image;
holder.imgIcon.setImageResource(outImage);
返回行;
}
静态类imageHolder的
{
ImageView的imgIcon;
TextView的txtTitle;
TextView的txtID;
}
@覆盖
公共过滤用getFilter(){
如果(过滤== NULL){
过滤器=新NameFilter();
}
返回过滤器;
}
私有类NameFilter扩展过滤器
{
@覆盖
保护FilterResults performFiltering(CharSequence的约束){
约束= constraint.toString()与toLowerCase()。
FilterResults导致=新FilterResults();
如果(约束= NULL和放大器;!&安培; constraint.toString()长度()大于0)
{
ArrayList的< SetRows> filteredItems =新的ArrayList< SetRows>();
的for(int i = 0,L = originalList.size(); I<升;我++)
{
SetRows名单= originalList.get(我);
如果(nameList.toString()。与toLowerCase()。包括(约束))
filteredItems.add(名单);
}
result.count = filteredItems.size();
result.values = filteredItems;
}
其他
{
同步(这)
{
result.values = originalList;
result.count = originalList.size();
}
}
返回结果;
}
@燮pressWarnings(未登记)
@覆盖
保护无效publishResults(CharSequence的约束,
FilterResults结果){
数据=(ArrayList的< SetRows>)results.values;
notifyDataSetChanged();
明确();
的for(int i = 0,L = data.size(); I<升;我++)
添加(data.get(一));
notifyDataSetInvalidated();
}
}
}
其他一切保持不变:)
我希望人们会发现它有用为自己的应用程序。
I am trying to implement the getFilter()
function in my ListView
but everytime I enter something in the EditText
my ListView
disappears.
My SetHelpRows
file:
public class SetHelpRows {
String name;
String id;
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public String getID () {
return id;
}
public void setID (String id) {
this.id = id;
}
public SetHelpRows(String name, String id) {
super();
this.name = name;
this.id = id;
}
}
My SetHelpRowsCustomAdapter
file:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;
public class SetRowsCustomAdapter extends ArrayAdapter<SetRows> {
Context context;
int layoutResourceId;
ArrayList<SetRows> data=new ArrayList<SetRows>();
private ArrayList<SetRows> original;
private ArrayList<SetRows> fitems;
private Filter filter;
public SetRowsCustomAdapter(Context context, int layoutResourceId, ArrayList<SetRows> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
this.original = data;//new ArrayList<Pkmn>();
this.fitems = data;//new ArrayList<Pkmn>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtID = (TextView)row.findViewById(R.id.txtModDate);
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
SetRows myImage = data.get(position);
holder.txtTitle.setText(myImage.name);
holder.txtID.setText(myImage.id);
int outImage=myImage.image;
holder.imgIcon.setImageResource(outImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
TextView txtID;
}
@Override
public Filter getFilter()
{
if (filter == null) {
Log.i("Before Filter", "Before Filter");
filter = new PkmnNameFilter();
}
return filter;
}
private class PkmnNameFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults results = new FilterResults();
String prefix = constraint.toString().toLowerCase();
if (prefix == null || prefix.length() == 0)
{
Log.i("prefix is null or 0", "prefix is null or 0");
ArrayList<SetRows> list = new ArrayList<SetRows>(original);
results.values = list;
results.count = list.size();
}
else
{
Log.i("prefix is !null or !0", "prefix is !null or !0");
final ArrayList<SetRows> list = new ArrayList<SetRows>(original);
final ArrayList<SetRows> nlist = new ArrayList<SetRows>();
int count = list.size();
for (int i=0; i<count; i++)
{
final SetRows pkmn = list.get(i);
final String value = pkmn.getName().toLowerCase();
if (value.startsWith(prefix))
{
nlist.add(pkmn);
}
}
results.values = nlist;
results.count = nlist.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
Log.i("publish result", "publish result");
fitems = (ArrayList<SetRows>)results.values;
clear();
int count = fitems.size();
for (int i=0; i<count; i++)
{
SetRows pkmn = (SetRows)fitems.get(i);
add(pkmn);
}
}
}
}
My partial MainActivity
file:
inputSearch = (EditText) findViewById(R.id.etSearch);
dataList = (ListView) findViewById(R.id.lvFiles);
for (int y=0; y<strNames.length;y++) {
name = strNamesOfAllah[y];
meaning = strMeaning[y];
rowsArray.add(new SetRows(R.drawable.icon, name, meaning));
}
adapter = new SetRowsCustomAdapter(MainActivity.this, R.layout.customlist, rowsArray);
dataList.setAdapter(adapter);
dataList.setClickable(true);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
//MainActivity.this.adapter.getFilter().filter(cs);
//String text = inputSearch.getText().toString().toLowerCase();
adapter.getFilter().filter(cs.toString().toLowerCase(Locale.US));
}
@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
}
});
In the LogCat
I do see the following:
12-12 12:15:57.077: I/Before Filter(720): Before Filter
12-12 12:15:57.106: I/prefix is !null or !0(720): prefix is !null or !0
12-12 12:15:57.216: I/publish result(720): publish result
I modified my code and was able to get it to work. If anyone wants to use it, be my guest :)
SetRows Java file:
public class SetRows {
int image;
String name;
String id;
public int getImage () {
return image;
}
public void setImage (int image) {
this.image = image;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public String getID () {
return id;
}
public void setID (String id) {
this.id = id;
}
public SetRows(int image, String name, String id) {
super();
this.image = image;
this.name = name;
this.id = id;
}
@Override
public String toString() {
return image + " " + name + " " + id;
}
}
SetRowsCustomAdapter Java file:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;
public class SetRowsCustomAdapter extends ArrayAdapter<SetRows> {
Context context;
int layoutResourceId;
ArrayList<SetRows> data=new ArrayList<SetRows>(); //data = countryList
private ArrayList<SetRows> originalList;
private NameFilter filter;
public SetRowsCustomAdapter(Context context, int layoutResourceId, ArrayList<SetRows> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
//this.data.addAll(data);
this.originalList = new ArrayList<SetRows>();
this.originalList.addAll(data);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtID = (TextView)row.findViewById(R.id.txtModDate);
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
SetRows myImage = data.get(position);
holder.txtTitle.setText(myImage.name);
holder.txtID.setText(myImage.id);
int outImage=myImage.image;
holder.imgIcon.setImageResource(outImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
TextView txtID;
}
@Override
public Filter getFilter() {
if (filter == null){
filter = new NameFilter();
}
return filter;
}
private class NameFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if(constraint != null && constraint.toString().length() > 0)
{
ArrayList<SetRows> filteredItems = new ArrayList<SetRows>();
for(int i = 0, l = originalList.size(); i < l; i++)
{
SetRows nameList = originalList.get(i);
if(nameList.toString().toLowerCase().contains(constraint))
filteredItems.add(nameList);
}
result.count = filteredItems.size();
result.values = filteredItems;
}
else
{
synchronized(this)
{
result.values = originalList;
result.count = originalList.size();
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
data = (ArrayList<SetRows>)results.values;
notifyDataSetChanged();
clear();
for(int i = 0, l = data.size(); i < l; i++)
add(data.get(i));
notifyDataSetInvalidated();
}
}
}
Everything else stayed the same :)
I hope people find it useful for their own app.
这篇关于在使用用getFilter功能ListView控件是空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!