Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ListView不显示ArrayList中的数据_Java_Android - Fatal编程技术网

Java ListView不显示ArrayList中的数据

Java ListView不显示ArrayList中的数据,java,android,Java,Android,我目前正在使用Yummly API,它以JSON的形式返回配方数据。用户输入一种配料,应用程序应显示可由该配料制成的配方。到目前为止,搜索工作正常,我已经成功地检索到一个配方名称列表,其中我已添加到ArrayList中 我能够在onPostExecute()方法中将ArrayList的所有内容打印到终端,因此ArrayList中肯定有数据。但是由于某些原因,它没有显示在ListView中!我可以到达结果页,但我得到的只是一张空白页 Search.java public class Search

我目前正在使用Yummly API,它以JSON的形式返回配方数据。用户输入一种配料,应用程序应显示可由该配料制成的配方。到目前为止,搜索工作正常,我已经成功地检索到一个配方名称列表,其中我已添加到ArrayList中

我能够在
onPostExecute()
方法中将ArrayList的所有内容打印到终端,因此ArrayList中肯定有数据。但是由于某些原因,它没有显示在ListView中!我可以到达结果页,但我得到的只是一张空白页

Search.java

public class Search extends Fragment {

EditText ingredient;
Activity mActivity;
ArrayList<String> pairs = new ArrayList<>();
private static final String TAG_TOTAL_MATCH = "totalMatchCount";
private static final String TAG_MATCHES = "matches";
private static final String TAG_NAME = "recipeName";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.searchpage, container, false);

    Button btnSearch = (Button) rootView.findViewById(R.id.search_button);

    btnSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ingredient = (EditText) getView().findViewById(R.id.search_box);
            // search returns something
            new SearchRecipe().execute();
        }
    });

    return rootView;
}

    class SearchRecipe extends AsyncTask<String, String, ArrayList<String>> {

    private String search_string;
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();

    /**
     * Searching for recipes
     * */
    protected ArrayList<String> doInBackground(String... args) {


        String full_url = url_search_recipe + search_string;

        JSONObject json = jsonParser.makeHttpRequest(full_url, "GET", params);

        // check log cat for JSON response
        Log.d("Response", json.toString());

        // check for success tag
        try {
            int count = json.getInt(TAG_TOTAL_MATCH);
            JSONArray matches = json.getJSONArray(TAG_MATCHES);

            if (count > 0) {
                int i;

                //populate array with recipe
                for (i=0; i<matches.length(); i++){
                    JSONObject js = matches.getJSONObject(i);
                    String name = js.getString(TAG_NAME);
                    pairs.add(name);

                }
                // return json.getString(TAG_MESSAGE);

            } else {
                // no results found
                // Toast.makeText(mActivity.getApplicationContext(), "There were: " + count + " recipes found", Toast.LENGTH_LONG).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(ArrayList<String> menu) {
        // dismiss the dialog once done
        pDialog.dismiss();
        //print recipes to terminal
        for(int j =0; j<pairs.size(); j++) {
            System.out.println(pairs.get(j));
        }
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.container, new Results());
        ft.commit();
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_resultspage, null);
        ListView myListView = (ListView) v.findViewById(R.id.resultsView);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, pairs);
        myListView.setAdapter(arrayAdapter);

    }

}
}
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_resultspage, container, false);
        return rootView;
    }
fragment\u resultspage.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#9ad979"
        android:id="@+id/resultsView"
        android:scrollbars="vertical"
        android:choiceMode="singleChoice" />

</RelativeLayout>


我想我显然错过了一些东西,但我不知所措。它没有显示任何内容的原因可能是什么?只包含了我的Search.java文件的一部分,因为它超过200行,所以我只包含了相关部分

在结果片段中执行此操作。您需要首先将从服务获得的数据传递给结果片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View rootView = inflater.inflate(R.layout.fragment_resultspage, container, false);
    ListView myListView = (ListView) rootView.findViewById(R.id.resultsView);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), 
                                     android.R.layout.simple_list_item_1, 
                                                               YOUR_RESULT);
    myListView.setAdapter(arrayAdapter);
    return rootView;
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
super.onCreate(savedInstanceState);
视图根视图=充气机。充气(R.layout.fragment\u结果页面,容器,false);
ListView myListView=(ListView)rootView.findViewById(R.id.resultsView);
ArrayAdapter ArrayAdapter=新的ArrayAdapter(getActivity(),
android.R.layout.simple\u list\u item\u 1,
你的结果);
设置适配器(arrayAdapter);
返回rootView;
}

在结果片段中执行此操作。您需要首先将从服务获得的数据传递给结果片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View rootView = inflater.inflate(R.layout.fragment_resultspage, container, false);
    ListView myListView = (ListView) rootView.findViewById(R.id.resultsView);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), 
                                     android.R.layout.simple_list_item_1, 
                                                               YOUR_RESULT);
    myListView.setAdapter(arrayAdapter);
    return rootView;
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
super.onCreate(savedInstanceState);
视图根视图=充气机。充气(R.layout.fragment\u结果页面,容器,false);
ListView myListView=(ListView)rootView.findViewById(R.id.resultsView);
ArrayAdapter ArrayAdapter=新的ArrayAdapter(getActivity(),
android.R.layout.simple\u list\u item\u 1,
你的结果);
设置适配器(arrayAdapter);
返回rootView;
}

您正在片段中显示结果。您必须将获得的数据传递给该片段并从中填充列表。当您像这样添加片段时,它不会立即显示。然后,当您在
onPostExecute
中膨胀视图时,不会将其添加到层次结构中,因此它永远不会显示。一种更常见的模式是添加要显示在
ResultFragment
中的内容,通常使用片段参数在工厂方法中添加,然后将内容放在
onCreateView
中的视图中,使用
android:layout\u height=“match\u parent”
而不是
android:layout\u height=“wrap\u content”
对于ListView高度:)返回对;onPostExecute的正确用法是使用它的参数来代替doInBackground()中的return null,该参数是doInBackground中的返回值,您正在片段中显示结果。您必须将获得的数据传递给该片段并从中填充列表。当您像这样添加片段时,它不会立即显示。然后,当您在
onPostExecute
中膨胀视图时,不会将其添加到层次结构中,因此它永远不会显示。一种更常见的模式是添加要显示在
ResultFragment
中的内容,通常使用片段参数在工厂方法中添加,然后将内容放在
onCreateView
中的视图中,使用
android:layout\u height=“match\u parent”
而不是
android:layout\u height=“wrap\u content”
对于ListView高度:)返回对;onPostExecute的正确用法是使用它的参数,即doInBackgroundWorked!中的返回值,而不是doInBackground()中的返回null!谢谢你,先生!!工作!谢谢你,先生!!