我有一个3列的Listview,包含来自SQLite DB的数据和一个seachview窗口。我创建了一个自定义数据。搜索未正确执行

问题描述 投票:1回答:2

我创建了一个MultiColumn ListView,上面有一个SearchView。它从SQLite数据库中提取数据并显示在列表视图中。我创建了一个自定义的ArrayAdapter和java类。但是,我的3列自定义数组列表视图中的SearchView输出没有返回正确的数据,即它没有正确过滤。我已经尝试了一切来解决可能存在的问题,并在过去的3天里一直在讨论这个问题。我正在使用QueryTextChangeListerner。任何帮助都值得赞赏,因为我在应用程序开发中处于瓶颈状态。

main activity.Java:

        mSearchView = (SearchView) findViewById(R.id.search_view);
        listView = (ListView) findViewById(R.id.list_view);

        controller = new ResturantsDbHelper(this);

        userList = new ArrayList<>();

        Cursor resturantdata = controller.getResturantList();

        int numRows =resturantdata.getCount();
        if (numRows == 0) {
            Toast.makeText(SearchResturantListingX.this,"No Resturants matching your selection criteria",Toast.LENGTH_LONG).show();
        }
        else {
            while (resturantdata.moveToNext()) {
                user = new User(resturantdata.getString(1),resturantdata.getString(2),resturantdata.getString(3),resturantdata.getString(4),resturantdata.getString(5));

                userList.add(user);
            }
            //FINISH Populating the array variable with the string data from SQLIte database

            adapter = new FiveColumn_ListAdapter(this,R.layout.activity_search_main3_resturant_list,userList);

            listView.setAdapter(adapter);
        }

        listView.setTextFilterEnabled(true);
        setupSearchView();
    }

    private void setupSearchView() {
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setSubmitButtonEnabled(true);
        mSearchView.setQueryHint("Search Here By Name");
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }
}

这是我的Java类,它定义了我的ListItems:

public class User {
    private String resturantName;
    private String resturantType;
    private String resturantLocation;

    public User (String resturantName, String resturantType, String resturantLocation, String rOpening, String rClosing){
        super();
        this.resturantName = resturantName;
        this.resturantType = resturantType;
        this.resturantLocation = resturantLocation;
    }

    public String getResturantName() {
        return resturantName;
    }

    public void setResturantName (String resturantName) {
        this.resturantName = resturantName;
    }

    public String getResturantType() {
        return resturantType;
    }

    public void setType (String resturantType) {
        this.resturantType = resturantType;
    }

    public String getResturantLocation() {
        return resturantLocation;
    }

    public void setLocation (String resturantLocation) {
        this.resturantLocation = resturantLocation;
    }

    public User(String resturantName, String type, String location) {
        super();
        this.resturantName = resturantName;
    }

    @Override
    public String toString() {
        return  resturantName + " ";  
    }
}

这是我的自定义适配器:

public class FiveColumn_ListAdapter extends ArrayAdapter<User> {

    private LayoutInflater mInflater;
    private ArrayList<User> users;
    private int mViewResourceId;

    public FiveColumn_ListAdapter(Context context, int textViewResourceId, ArrayList<User> users) {
        super(context,textViewResourceId,users);
        this.users = users;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mViewResourceId = textViewResourceId;
    }

    public View getView(int position, View convertView, ViewGroup parents) {
        convertView = mInflater.inflate(mViewResourceId,null);

        User user =users.get(position);

        if (user != null) {
            TextView name=(TextView)convertView.findViewById(R.id.resturantName);
            TextView type=(TextView)convertView.findViewById(R.id.resturantType);
            TextView location=(TextView)convertView.findViewById(R.id.resturantLocation);

            if(name !=null) {
                name.setText((user.getResturantName()));
            }

            if(type !=null) {
                type.setText((user.getResturantType()));
            }

            if(location !=null) {
                location.setText((user.getResturantLocation()));
            }

        }
        return convertView;
    }
}
android arraylist multiple-columns searchview custom-adapter
2个回答
0
投票

FiveColumn_ListAdapter必须实施android.widget.Filterable

然后将这些代码添加到FiveColumn_ListAdapter

public class FiveColumn_ListAdapter extends ArrayAdapter<User> 
                                implements android.widget.Filterable {

private LayoutInflater mInflater;
private ArrayList<User> users;// this is filtered users a subset of AllUsers
private int mViewResourceId;
private ArrayList<User> AllUsers;// AllUsers is users without any filter

public FiveColumn_ListAdapter(Context context, int textViewResourceId, 
                                             ArrayList<User> users, String initialQuery) {
    super(context,textViewResourceId,users);
    this.AllUsers = users;
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mViewResourceId = textViewResourceId;
    getFilter().filter(initialQuery);// perform fiter to populate users filter may be empty String
}

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence query) {

                FilterResults filterResults = new FilterResults();
                if(AllUsers == null || AllUsers.size() == 0){
                     filterResults.count = 0;
                     filterResults.values = null;
                     return filterResults;
                }
                ArrayList<User> results = new ArrayList<>();
                for (User user : AllUsers) {
                      if(query.toString().equals(user.getResturantName())// do any filtering here
                         results.add(user);
                }
                filterResults.count = results.size();
                filterResults.values = results;
                return filterResults;
            }
            ///////////////////////////////
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                users = results.values// 
            }
        };
    }

0
投票
  1. 首先,您需要更改项目集合,以便一直进行排序。其中一个例子。 private Set<User> users; ..... users = new SortedSet<>();
  2. 改变你对User对象的实现,包含Comparable接口。对于前者下面。 class Movie implements Comparable<Movie> { private double rating; private String name; private int year; // Used to sort movies by year public int compareTo(Movie m) { return this.year - m.year; } // Constructor public Movie(String nm, double rt, int yr) { this.name = nm; this.rating = rt; this.year = yr; } // Getter methods for accessing private data public double getRating() { return rating; } public String getName() { return name; } public int getYear() { return year; } }

然后您可以配置所有排序,正如您在上面的方法compareTo中所期望的那样。查看有关Comparable Interface的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.