ListView返回空的onQueryTextSubmit()

时间:2019-08-13 20:41:39

标签: android json retrofit2 searchview

最近我一直在尝试将SearchView添加到我的应用中,因此我遵循this videothis documentation的建议。

到目前为止,我已采取以下步骤:

  • 添加了搜索菜单项:
        <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" />
  • 在MainActivity.java中的onCreateOptionsMenu()中添加了以下内容(但仍然不知道如何执行最终搜索):
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        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) {
                        Intent launchNewIntent = new Intent(MainActivity.this, SearchableActivity.class);
                        startActivityForResult(launchNewIntent, 0);
                        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));

        return true;
    }

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        ComponentName componentName = new ComponentName(getApplicationContext(), SearchableActivity.class);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));

        return true;
    }

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        ComponentName componentName = new ComponentName(getApplicationContext(), SearchableActivity.class);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
  • 创建了基本的searchable.xml配置:
        <?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>
  • 在清单文件中添加了可搜索的内容:
        <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>
  • 并使用查询处理程序的草图创建了SearchableActivity.java的草图(仍然不知道布局必须像主活动还是适配器接收器):
public class SearchableActivity extends AppCompatActivity {

    TextView tvSearch;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        handleIntent(intent);
    }

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

    private void doMySearch(String query) {
        tvSearch.setText(query);
    }

}

从现在开始,考虑到我的Adapter填充了ListView从服务器中保存的JSON文件中获取的数据,我不知道如何继续显示搜索结果。

除了SearchableActivity.java故障的草图之外,其他一切都还可以,但是由于仍然有很多空白,所以没有显示预期的结果。

我宁愿只使用MainActivity.java和当前的适配器来显示结果,但是现在我只想了解要使该工作至少要做些什么。

以防万一,我将适配器放在这里,以帮助您了解问题:

    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,
                        [...]
                );
            }

        }

    }

非常感谢您的推进,如果需要,我可以提供有关代码的更多详细信息。

0 个答案:

没有答案