简体   繁体   English

从mainactivity访问searchview查询文本

[英]Accessing the searchview query text from mainactivity

I am planning to make a news app. 我打算制作一个新闻应用程序。 I have created a simple search view inside the action bar of my app. 我在应用程序的操作栏中创建了一个简单的搜索视图。

This is the Main activity 这是主要活动

package com.example.hp.simplenews;

import android.app.Activity;
import android.app.LoaderManager;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SearchView;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<News>> {
    private int Newsloaderid = 1;
    private Newsadapter newslistadapter;
    private String search_query_input = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView newslistview = findViewById(R.id.list);
        newslistadapter = new Newsadapter(this, new ArrayList<News>());
        newslistview.setAdapter(newslistadapter);
        LoaderManager loaderManager = getLoaderManager();
        loaderManager.initLoader(Newsloaderid, null, MainActivity.this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the options menu from XML
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String newtext) {
                search_query_input = newtext;
                return true;
            }
            @Override
            public boolean onQueryTextChange(String query) {
                return true;
            }
        });
        return true;
    }

    @Override
    public Loader<List<News>> onCreateLoader(int i, Bundle bundle) {
        return new NewsLoader(this, search_query_input);
    }

    @Override
    public void onLoadFinished(Loader<List<News>> loader, List<News> news) {
        newslistadapter.clear();
        if (news != null && !news.isEmpty()) {
            newslistadapter.addAll(news);
        }
    }

    @Override
    public void onLoaderReset(Loader loader) {
        newslistadapter.clear();
    }
}

The SearchResultsActivity handles the HTTP request and JSON parsing. SearchResultsActivity处理HTTP请求和JSON解析。

This is the SearchResultsActivity 这是SearchResultsActivity

    package com.example.hp.simplenews;

import android.app.Activity;
import android.app.LoaderManager;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.Loader;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class SearchResultsActivity extends Activity{
    String query_string = "";

    private static final String LOG_TAG = SearchResultsActivity.class.getName();

    public static URL createUrl(String query_url) throws MalformedURLException {
        URL url = null;
        String APIKey = "apiKey";
        String query = "q";
        try {
            final String base_url = "https://newsapi.org/v2/everything?";
            Uri final_url = Uri.parse(base_url).buildUpon()
                    .appendQueryParameter(query, query_url)
                    .appendQueryParameter(APIKey, "48f67c1e66994aa8a92f92a48b5f6581")
                    .build();
            url = new URL(final_url.toString());
            Log.i(LOG_TAG, "THE FINAL URL IS" + final_url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            Log.e(LOG_TAG, "the url couldn't be made");
        }

        return url;
    }

    private static final String HTTPREQUEST(URL url) throws IOException {
        String jsonresponse = "";
        if (jsonresponse == null) {
            return null;
        }
        HttpURLConnection httpURLConnection = null;
        InputStream inputStream = null;
        try {
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setConnectTimeout(150000);
            httpURLConnection.connect();
            if (httpURLConnection.getResponseCode() == 200) {
                inputStream = httpURLConnection.getInputStream();
                jsonresponse = readfromstream(inputStream);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
        }
        return jsonresponse;
    }

    private static final String readfromstream(InputStream inputStream) throws IOException {
        StringBuilder output = new StringBuilder();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String urlline = bufferedReader.readLine();
        while (urlline != null) {
            output.append(urlline);
            urlline = bufferedReader.readLine();
        }
        return output.toString();
    }

    private static List<News> JSONParser(String NewsJSON) throws JSONException {
        if (TextUtils.isEmpty(NewsJSON)) {
            return null;
        }
        List<News> news = new ArrayList<>();
        try {
            JSONObject basejson = new JSONObject(NewsJSON);
            JSONArray articles = basejson.getJSONArray("articles");
            for (int i = 0; i < articles.length(); i++) {
                News newss = new News();
                JSONObject c = articles.getJSONObject(i);
                JSONObject source = c.getJSONObject("source");
                newss.setMsource(source.getString("name"));
                newss.setMtitle(c.getString("title"));
                newss.setMdescription(c.getString("description"));
                newss.setMtime(c.getString("publishedAt"));
                String image = c.getString("urlToImage");
                if (image != null) {
                    newss.setmImage(c.getString("urlToImage"));
                }
                newss.setNewsURL(c.getString("url"));
                news.add(newss);
            }
        } catch (JSONException j) {
            Log.e(LOG_TAG, "couldn't parse jSON");
        }
        return news;
    }

    public static List<News> fetchnews(String search_query) throws IOException, JSONException {
        URL url = createUrl(search_query);
        String JSONRESPONSE = null;
        try {
            JSONRESPONSE = HTTPREQUEST(url);
        } catch (IOException j) {
            j.printStackTrace();
        }
        List<News> news = JSONParser(JSONRESPONSE);
        return news;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        try {
            handleIntent(intent);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    private void handleIntent(Intent intent) throws MalformedURLException {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            query_string = intent.getStringExtra(SearchManager.QUERY);
            Log.i(LOG_TAG, "The querytext is" + query_string);
        }

    }
}

This whole process of refreshing the list view is done by a AsyncTaskLoader 刷新列表视图的整个过程由AsyncTaskLoader完成

The custom Asyntaskloader class is copied below: 自定义Asyntaskloader类复制如下:

public class NewsLoader extends AsyncTaskLoader<List<News>> {
    private String mUrl;
    private static String LOG_TAG = NewsLoader.class.getName();

    public NewsLoader(Context context, String url) {
        super(context);
        mUrl = url;
    }

    @Override
    protected void onStartLoading() {
        forceLoad();
    }

    @Override
    public List<News> loadInBackground() {
        if (mUrl == null) {
            return null;
        }
        List<News> news = null;
        try {
            news = SearchResultsActivity.fetchnews(mUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return news;
    }
}

Now I have the search query entered by the user stored in the search_query_input variable in the MainActivity. 现在,我将用户输入的搜索查询存储在MainActivity的search_query_input变量中。

But the app just gets stuck when I press the submit button. 但是当我按下“提交”按钮时,该应用只是卡住了。 The override methods for the asynctaskloader are not getting executed at all. 完全不执行asynctaskloader的重写方法。

What is happening? 怎么了? Any help will be much appreciated. 任何帮助都感激不尽。

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
    onQueryTextChange(String newText){
      //when user type in searchview get string as newText parameter
      asynTaskMethod(newText);
    }
   onQueryTextSubmit(String query){
     //when user press submit button in searchview get string as query parameter
   asynTaskMethod(query);
   }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM