过滤数组的最佳方法

时间:2016-04-12 11:16:22

标签: android listview

过滤此阵列的最佳方法是什么? 该数组包含int位置

我试过,但我认为我错了。看看:

    public class SearchActivity extends AppCompatActivity {

    private ListView searchList;
    private SearchListAdapter searchListAdapter;

    listContents listC[] = new listContents[]{

            new listContents(R.string.UtrujjTitle,R.string.UtrujjContent,R.drawable.image1),
            new listContents(R.string.AruzzTitle,R.string.AruzzContainer,R.drawable.image2),
            new listContents(R.string.ArzTitle,R.string.ArzContainer,R.drawable.image3)
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        searchList = (ListView)findViewById(R.id.searchList);

        searchListAdapter = new SearchListAdapter(this,listC);
        searchList.setAdapter(searchListAdapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search, menu);

        final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);


        SearchView searchView = (SearchView) menu.findItem(R.id.searchable).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                searchListAdapter.getFilter().filter(newText);

                List<listContents> listContentses = new ArrayList<listContents>();

                for (int i = 0; i < listC.length; i++) {

                   listContents filterd = listC[i];

                    int title = filterd.getTitleList();
                    int content = filterd.getContentList();

                    if (String.valueOf(title).contains(newText) || String.valueOf(content).contains(newText)) {

                       listContentses.add(filterd);


                    }


                }

                searchListAdapter.setFilter(listContentses);
                searchListAdapter.notifyDataSetChanged();

                return false;

            }
        });
        return true;
    }
}

适配器类:

    public class SearchListAdapter extends ArrayAdapter<listContents>  {

    private Context context;
    private listContents[] listContentsList;
    private List<listContents> listContentses;

    public SearchListAdapter(Context context, listContents[] listContentses ) {
        super(context,0,listContentses);
        this.context = context;
        this.listContentsList = listContentses;
    }


public void setFilter(List<listContents> lister){
    this.listContentses = lister;
}
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        listContents listContents = getItem(position);

        if (convertView == null) {

            convertView = LayoutInflater.from(getContext()).inflate(R.layout.listresults, parent, false);

        }
            TextView title = (TextView) convertView.findViewById(R.id.listTitle);
            title.setText(listContents.getTitleList());
           TextView content = (TextView)convertView.findViewById(R.id.listContain);
           content.setText(listContents.getContentList());
        ImageView imageView = (ImageView)convertView.findViewById(R.id.listImage);
        imageView.setImageResource(listContents.getImageList());

        return convertView;

        }
    }

0 个答案:

没有答案