Json解析数据的Searchview

时间:2018-09-18 07:29:13

标签: android json listview searchview

我已经解析了列表视图中的数据并显示出来。我必须在显示的数据中实现searchview。我有一个getmTitle()函数,该函数在listview中具有列表的标题。现在,如果我在searchview中编写,listview应该选择键入的标题并显示出来。

public class ImageListActivity {

private String id;
private String title;
private String url_s;

public ImageListActivity(String id, String title, String url_s) {
    this.id = id;
    this.title = title;
    this.url_s = url_s;
}

public String getmId() {
    return id;
}

public String getmTitle() {
    return title;
}

public String getmUrl_s() {return url_s;}
}

下面是我的适配器类:

public class AdapterActivity extends ArrayAdapter<ImageListActivity> {



public AdapterActivity(Activity context, ArrayList<ImageListActivity> image) {
    super(context, 0, image);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    View listItemView = convertView;

    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.default_layout, parent, false);
    }


    ImageListActivity currentWord = getItem(position);

    ImageView imageView = (ImageView) listItemView.findViewById(R.id.flickr_image);

    try {
        Picasso.get()
                .load(currentWord.getmUrl_s())
                .into(imageView);
    } catch(NullPointerException e) {
        /* sets image to default image

         */
        imageView.setImageResource(R.drawable.default_img);

    }

    TextView image_id = (TextView) listItemView.findViewById(R.id.flickr_id);
    image_id.setText(currentWord.getmId());

    return listItemView;


}


}

我不知道下面的代码该做什么:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Search Images");
    toolbar.setTitleTextColor(Color.parseColor("#ffffff"));

    searchView = (MaterialSearchView) findViewById(R.id.search_view);
    searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
        @Override
        public void onSearchViewShown() {

        }

        @Override
        public void onSearchViewClosed() {
            techListView.setAdapter(mAdapter);
            SearchActivity.SearchAsyncTask task = new SearchActivity.SearchAsyncTask();
            task.execute(USGS_REQUEST_URL);
        }
    });
    searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

        }
    });

这是我的项目链接https://github.com/sivaperumal644/Gallery

1 个答案:

答案 0 :(得分:0)

您需要做两件事。首先,在您的适配器中实现Filterable,您需要在其中实现过滤数据的逻辑。其次,您需要在onQueryTextChange()中调用adapter.getFilter()来触发过滤器。请检查此链接。

https://www.androidhive.info/2017/11/android-recyclerview-with-search-filter-functionality/