当前位置: 首页 > 面试题库 >

Java 使用getFilter函数时ListView为空白

满增
2023-03-14
问题内容

我想实现getFilter()我的功能ListView,但每次我输入的东西EditTextListView就会消失。

我的SetHelpRows档案:

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;
    }
}

我的SetHelpRowsCustomAdapter档案:

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

       }
}

我的部分MainActivity文件:

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

在中,LogCat我确实看到以下内容:

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

问题答案:

修改了我的代码,并使其能够正常工作。如果有人想使用它,请成为我的客人

SetRows Java文件:

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文件:

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();
    }
   }
}

其他一切都保持不变:)

我希望人们发现它对自己的应用有用。



 类似资料:
  • 我的部分文件: 在中,我确实看到了以下内容:

  • 问题内容: 我一直在使用minidom.toprettyxml整理我的xml文件。当我创建XML文件并使用此方法时,所有工作都可以进行,但是如果我在修改xml文件后使用它(例如,我添加了其他节点),然后将其写回到XML ,我得到的是空行,每次更新时,我都会得到越来越多的空行… 我的代码: 结果: 有什么建议 ? 谢谢。 问题答案: 我在这里找到了解决方案:http : //code.actives

  • 我的数据库中有一些值,如果还没有输入的话,它们可能为空。 但是当我在我的html中使用Thymeleaf时,它在解析空值时出错。 这个有什么办法处理吗

  • 问题内容: 我的Android应用程序需要使用中填充ListView数据ArrayList。 我这样做很麻烦。有人可以帮我提供代码吗? 问题答案: 你需要通过进行操作,以使ArrayList(或任何其他集合)适应布局中的项目(ListView,Spinner等)。 这是Android开发人员指南所说的: 一个管理任意对象数组支持的。默认情况下,此类期望提供的资源ID引用单个。如果要使用更复杂的布局

  • 第二类是GuiconTroller: 目前,我在fxml文件中将Listview的fxId设置为“AgentsList”。 每当我在IntelliJ中运行该程序时,它总是围绕“agentslist.setitems(_dolist);”返回一个java.lang.NullPointerException行。

  • 我使用的是spring boot 1.4, 当使用@SpringBootTest注释进行集成测试时,它会给出一个空指针。 对于主类: 然后在我的控制器中: HelloService 但在处理请求时,它会告诉helloService NullPointException。 我错过了什么?