Android searchView直到完全滚动才搜索列表

时间:2016-03-09 00:08:48

标签: android listview filter adapter searchview

我有一个带有自定义适配器的listview,我试图将SearchView与CustomFilter一起使用。但搜索并不完全"工作

当我搜索列表视图的可查看区域中的某些内容时,它可以搜索,并且搜索中不包含所有不可查看的区域。

以下是有关最新情况的视频:

  

https://youtu.be/2Z9FZMlNmGw

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_board_game_list, container, false);
        this.listView = (ListView) view.findViewById(R.id.listView);

        DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this.getContext());
        databaseAccess.open();

        List<String> boardgamesNames = databaseAccess.getNames();
        List<String> urls = databaseAccess.getUrls();

        adapter = new bgAdapter(getContext(), R.layout.row_layout);
        adapterOriginal = new bgAdapter(getContext(), R.layout.row_layout);
        databaseAccess.close();
        listView.setAdapter(adapter);

        int i = 0;
        for(String name: boardgamesNames) {

            boardgameListRow data = new boardgameListRow(urls.get(i), boardgamesNames.get(i));
            i++;
            adapter.add(data);
            adapterOriginal.add(data);
        }

        listView.setDivider(null);
        listView.setDividerHeight(0);

        searchView = (SearchView)view.findViewById(R.id.searchId);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                if (newText.length() > 0) {
                    adapter.getFilter().filter(newText);
                }

                return false;
            }
        });

        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {

                BoardGameListFragment fragment= new BoardGameListFragment();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container,fragment);
                fragmentTransaction.commit();
                //adapter = adapterOriginal;
                return true;
            }
        });
        // Inflate the layout for this fragment
        return view;
    }
}

这是适配器:

  

https://github.com/Shank09/AndroidTemp/blob/master/bgAdapter.java

3 个答案:

答案 0 :(得分:0)

我认为你需要在onQueryTextChange中调用notifyDataSetChanged()

答案 1 :(得分:0)

我修复了它,我在bgAdapter中使用了错误的变量。如果可能,请删除此问题。

答案 2 :(得分:0)

public class Listbyoperator extends Activity {

    ListView lstdetail;
    Activity act;
    EditText search;

    ArrayList<DetailModel> detail=new ArrayList<DetailModel>();
    DetailaAdapter dadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listbyoperator);
        act=this;
        lstdetail=(ListView) findViewById(R.id.Listbyoperator_detaillist);
        search=(EditText) findViewById(R.id.editsearch);
        search.setPadding(10, 0, 0, 0);
        search.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            String text = search.getText().toString().toLowerCase(Locale.getDefault());

            dadapter.filter(text);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
            int arg2, int arg3) {
            // TODO Auto-generated method stub
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
            // TODO Auto-generated method stub
            }
            });

            DetailModel d=new  DetailModel("1","HARDIP","saahi");
            detail.add(d);
            DetailModel d1=new  DetailModel("2","jalpa","sadfsadf");
            detail.add(d1);

            dadapter=new  DetailaAdapter(act, detail);
            lstdetail.setAdapter(dadapter);
            dadapter.notifyDataSetChanged();
            lstdetail.setEnabled(true);

    }

}
/*Detail Model*/
public class DetailModel 
{
    public String d_id,d_name,d_decription;

    public String getD_id() {
        return d_id;
    }

    public void setD_id(String d_id) {
        this.d_id = d_id;
    }

    public String getD_name() {
        return d_name;
    }

    public void setD_name(String d_name) {
        this.d_name = d_name;
    }

    public String getD_decription() {
        return d_decription;
    }

    public void setD_decription(String d_decription) {
        this.d_decription = d_decription;
    }

    public DetailModel(String s1,String s2,String s3)
    {
        this.d_id=s1;
        this.d_name=s2;
        this.d_decription=s3;
    }

}

/*detail adapter */
public class DetailaAdapter extends BaseAdapter{

    Context mContext;
    private List<DetailModel> data=null;
    private ArrayList<DetailModel> arraylist;
    private static LayoutInflater inflater=null;
    private static String String=null,valid;
    public boolean flag=true;

    public DetailaAdapter(Context context,List<DetailModel> data)
    {
        mContext = context;
        this.data = data;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<DetailModel>();
        this.arraylist.addAll(data);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi=convertView;
         if(convertView==null)  
              inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                vi = inflater.inflate(R.layout.list_detail, null);

                final   TextView t1,t2,t3,t4,t5;

                t1=(TextView)vi.findViewById(R.id.list_detail_text1);
                t2=(TextView)vi.findViewById(R.id.list_detail_textview2);
                t3=(TextView)vi.findViewById(R.id.list_detail_text2);

                DetailModel da =new DetailModel(String, String,String);
                da=data.get(position);

               final String a1,a2,a3;

                a1=da.d_id;
                a2=da.d_name;
                a3=da.d_decription;

                t2.setText(a3);//description
                t3.setText(a2);//name

        return vi;

    }

    public void filter(String charText)
    {
        charText = charText.toLowerCase(Locale.getDefault());
        data.clear();
        if (charText.length() == 0) {
        data.addAll(arraylist);
    }
    else
    {
        for (DetailModel wp : arraylist)
    {
    if (wp.getD_decription().toLowerCase(Locale.getDefault()).contains(charText) || wp.getD_name().toLowerCase(Locale.getDefault()).contains(charText))
    {
        data.add(wp);
    }
    }
    }
    notifyDataSetChanged();
    }

}