Android列表视图没有填充数据

时间:2014-09-21 00:26:42

标签: java android android-fragments android-studio

我是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;
        }

    }

}

}

1 个答案:

答案 0 :(得分:0)

mFoursquareAdapter.add(ItemStr);

应该是

foursquareList.add(ItemStr)

您需要正确声明foursquareList(作为字段)。 您还应该将Adapter声明为字段变量,以防以后需要引用它