I've just followed the advices provided by this video and this documentation as I've been trying to add a SearchView to my app lately.

I took the following steps so far:

  • added the search menu item:

    <item
        android:id="@+id/action_search"
        android:title="Buscar"
        android:icon="@drawable/ic_arrow_upward_black_24dp"
        android:iconifiedByDefault="true"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="androidx.appcompat.widget.SearchView" />
    
  • added the following pieces to the onCreateOptionsMenu() in my MainActivity.java (but still don't know how to perform the final search):

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView)
            MenuItemCompat.getActionView(searchItem);
    
    searchView.setOnQueryTextListener(
            new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String s) {
                    return true;
                }
    
                @Override
                public boolean onQueryTextChange(String s) {
                    return true;
                }
            }
    );
    
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    ComponentName componentName = new ComponentName(getApplicationContext(), SearchableActivity.class);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
    
  • created a basic searchable.xml configuration:

    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name"
        android:hint="Buscar" >
    </searchable>
    
  • added a searchable acvitity to the manifest file:

    <activity android:name=".ui.activity.SearchableActivity"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
    
  • and created a sketch of the SearchableActivity.java with a sketch of a handler for the query:

    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            doMySearch(query);
        }
    }
    

From this point forward, I don't know how to proceed to exhibit the search results considering my Adapter which fills a ListView getting data from a JSON file kept in a server.

Apart from the sketch of the SearchableActivity.java malfunction, everything is okay, but showing no result as expected since there are still many blanks.

I'd rather just using the MainActivity.java and the current adapter to show the results, but now I'd just want to understand what I must do to make this work at least.

Just in case, I'm putting here the adapter so as it helps you to understand the issue:

public class MyListAdapter extends ArrayAdapter<Subreddit> {

    List<Subreddit> subreddits;
    Context context;
    private LayoutInflater mInflater;
    private SubredditList subredditList;

    // Constructors
    public MyListAdapter(Context context, SubredditList subredditList, String sortingColumn, boolean reverse) {
        super(context, 0, subredditList.getSubreddits(sortingColumn, reverse));
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.subredditList = subredditList;
        subreddits = subredditList.getSubreddits(sortingColumn, reverse);
    }

    public static String getDate(String stringData) throws ParseException {
        SimpleDateFormat inputDate = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy", Locale.getDefault());
        Date date = inputDate.parse(stringData);
        SimpleDateFormat outputDate = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
        System.out.println(date);
        return outputDate.format(date);
    }

    @Override
    public Subreddit getItem(int position) {
        return subreddits.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            View view = mInflater.inflate(R.layout.layout_row_view, parent, false);
            viewHolder = ViewHolder.create((LinearLayout) view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Subreddit item = getItem(position);

        if (subreddits.get(position).getIcon().equals("")) { //url.isEmpty()
            GlideApp.with(context)
                    [...]
        } else {
            GlideApp.with(context)
                    [...]
        }
        viewHolder.textViewName.setText(item.getName());
        viewHolder.textViewDescription.setText(item.getDescription());
        viewHolder.textViewActivity.setText(String.valueOf(item.getTotalActivity(subredditList)));
        DecimalFormat formatter = new DecimalFormat("###,###,###");
        String decimalFormat = formatter.format(item.getMembers()).replace(",", ".");
        viewHolder.textViewMembers.setText(decimalFormat);
        try {
            viewHolder.textViewAge.setText(getDate(new Date(item.getAge() * 1000).toString()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        [...]
        if (subreddits.get(position).getNSFW()) {
            [...]
        } else {
            [...]
        }

        if (position % 2 == 1) {
            [...]
        } else {
            [...]
        }

        return viewHolder.rootView;

    }

    private static class ViewHolder {
        public final LinearLayout rootView;
        public final FrameLayout frameIcon;
        public final ImageView imageViewIcon;
        [...]

        private ViewHolder(LinearLayout rootView,
                           FrameLayout frameIcon,
                           ImageView imageViewIcon,
                           [...]
        ) {
            this.rootView = rootView;
            this.frameIcon = frameIcon;
            this.imageViewIcon = imageViewIcon;
            [...]
        }

        public static ViewHolder create(LinearLayout rootView) {
            FrameLayout frameIcon = rootView.findViewById(R.id.frameIcon);
            ImageView imageViewIcon = rootView.findViewById(R.id.imageViewIcon);
            [...]

            return new ViewHolder(rootView,
                    frameIcon,
                    imageViewIcon,
                    [...]
            );
        }

    }

}

I thank you advance and I'm available to show more details about the code if needed.

Related posts

Recent Viewed