Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.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/8/magento/5.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 Android listview未填充数据_Java_Android_Android Fragments_Android Studio - Fatal编程技术网

Java Android listview未填充数据

Java Android listview未填充数据,java,android,android-fragments,android-studio,Java,Android,Android Fragments,Android Studio,我是Android Studio的新手,有一个简单的Android视图。单击按钮调用foursquare API,并获取星巴克在我解析的位置周围的返回结果,我正在尝试为同一视图上的列表框设置适配器。如果我在OnPostExecute中放置断点,我会看到我为listview设置的mFoursquare适配器在mFoursquareAdapter中有两个json字符串结果,我甚至调用 mFoursquareAdapter.notifyDataSetChanged(); 但视图不会使用结果刷新。我已

我是Android Studio的新手,有一个简单的Android视图。单击按钮调用foursquare API,并获取星巴克在我解析的位置周围的返回结果,我正在尝试为同一视图上的列表框设置适配器。如果我在OnPostExecute中放置断点,我会看到我为listview设置的mFoursquare适配器在mFoursquareAdapter中有两个json字符串结果,我甚至调用

mFoursquareAdapter.notifyDataSetChanged();
但视图不会使用结果刷新。我已经把代码贴在下面了。请任何人指出我做错了什么或需要改变,因为我已经有了结果,需要完成这项工作…非常感谢您的帮助和反馈!谢谢

public class FoursquareInfoFragment extends android.app.Fragment {
private ArrayAdapter<String> mFoursquareAdapter;

public FoursquareInfoFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Dummy data for the ListView. Here's the sample weekly forecast
    String[] data = {
            "Sample Foursquare Data",
    };

    List<String> foursquareList = new ArrayList<String>(Arrays.asList(data));

    mFoursquareAdapter = new ArrayAdapter<String>(
            getActivity(),  // the current context ie the activity
            R.layout.fragment_my, // the name of the layout Id
            R.id.textViewFoursquare, // the Id of the TextView to populate
            foursquareList);

    View rootView = inflater.inflate(R.layout.fragment_my, container, false);
    //View resultsView = inflater.inflate(R.layout.results, container, false);

    View resultsView = inflater.inflate(R.layout.fragment_my, container, false);

    ListView listView = (ListView) resultsView.findViewById(R.id.listview_FoursquareInfo);
    listView.setAdapter(mFoursquareAdapter);

    Button btnGetFoursquareData = (Button) rootView.findViewById(R.id.btnFoursquare);
    btnGetFoursquareData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FetchFoursquareDataTask fetch = new FetchFoursquareDataTask();
            fetch.execute("Starbucks");
        }
    });

    return rootView;
}


public class FetchFoursquareDataTask extends AsyncTask<String, Void, String[]> {
    private final String LOG_TAG = FetchFoursquareDataTask.class.getSimpleName();

    @Override
    protected void onPostExecute(String[] result) {
        if (result != null) {
            mFoursquareAdapter.clear();
            for (String ItemStr : result) {
                mFoursquareAdapter.add(ItemStr);
            }

            mFoursquareAdapter.notifyDataSetChanged();
        }
    }


    @Override
    protected String[] doInBackground(String... params) {

        // If there's no venue category, theres nothing to look up. Verify the size of the params.
        if (params.length == 0) {
            return null;
        }
        // These two need to be declared outside the try/catch
        // so that they can be closed in the finally block.
        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;

        // Will contain the raw JSON response as a string.
        String foursquareJsonStr = null;

        try {
            // Build Foursquare URI with Parameters
            final String FOURSQUARE_BASE_URL =
                    "https://api.foursquare.com/v2/venues/search";
            final String client_id = "client_id";
            final String client_secret = "client_secret";
            final String v = "20130815";
            final String near = "Dunwoody, Ga";
            final String query = "Starbucks";
            final String limit = "2";

            Uri builtUri = Uri.parse(FOURSQUARE_BASE_URL).buildUpon()
                    .appendQueryParameter("client_id", client_id)
                    .appendQueryParameter("client_secret", client_secret)
                    .appendQueryParameter("v", v)
                    .appendQueryParameter("near", near)
                    .appendQueryParameter("query", query)
                    .appendQueryParameter("limit", limit)
                    .build();

            URL url = new URL(builtUri.toString());

            // Create the request to Foursquare, and open the connection
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            // Read the input stream into a String
            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            if (inputStream == null) {
                // Nothing to do.
                return null;
            }
            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {

                buffer.append(line + "\n");
            }

            if (buffer.length() == 0) {
                // Stream was empty.  No point in parsing.
                foursquareJsonStr = null;
                return null;
            }
            foursquareJsonStr = buffer.toString();

            Log.v(LOG_TAG, "Foursquare JSON String: " + foursquareJsonStr);
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error ", e);
            // If the code didn't successfully get the fpursquare data, there's no point in attempting
            // to parse it.
            return null;
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException e) {
                    Log.e("PlaceholderFragment", "Error closing stream", e);
                }
            }
        }

        String[] list = new String[]{"", ""};
        try {

            JSONObject foursquareJson = new JSONObject(foursquareJsonStr);
            JSONObject responseObject = (JSONObject) foursquareJson.get("response");
            JSONArray foursquareArray = responseObject.getJSONArray("venues");
            list = new String[foursquareArray.length()];

            for (int i = 0; i < foursquareArray.length(); i++) {

                list[i] = foursquareArray.get(i).toString();
            }

            return list;
        } catch (JSONException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
            e.printStackTrace();
        } finally {
            Log.e(LOG_TAG, "ba");
            return list;
        }

    }

}
}这个

mFoursquareAdapter.add(ItemStr);
应该是

foursquareList.add(ItemStr)
您需要将foursquareList正确地声明为一个字段。
您还应该将适配器声明为字段变量,以防以后需要引用它

查看本教程中有关如何创建ListView的内容:您需要向列表中添加非AdapterHi ElefantPhace我按照您的建议并声明了私有列表foursquareList;在顶部,将其设置为OnCreateView函数中的数据,并将其设置为foursquareList=new arraylistarays.asListdata;在OnPostExecute中,就像你提到的foursquareList.addItemStr,但它仍然没有用列表框内容刷新屏幕,我可以在断点上看到它有两个项目,但这不起作用