2014-09-21 155 views
1

我是新來的Android Studio,並有一個簡單的Android視圖,我正在努力。點擊按鈕會調用foursquare API,並獲取我解析的位置周圍的星巴克的結果,並嘗試將其設置爲適用於相同視圖上的列表框的適配器。如果我在OnPostExecute()將斷點我看到mFoursquare適配器,我爲ListView設置有兩個JSON字符串導致mFoursquareAdapter,我甚至稱之爲Android listview沒有填充數據

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

    } 

} 

}

+0

請查看本教程,瞭解如何創建ListView:http://www.android-ios-tutorials.com/ 770 /創建自定義列表查看在Android/ – Houcine 2014-09-21 01:06:24

+0

您需要添加到您的'列表'不'適配器' – ElefantPhace 2014-09-21 01:34:54

回答

0

mFoursquareAdapter.add(ItemStr); 

應該

foursquareList.add(ItemStr) 

你還需要聲明foursquareList正確(如一個字段)。 你也應該聲明你的Adapter作爲一個字段變量,以防萬一你需要稍後參考它

+0

嗨ElefantPhace我按照你的建議和宣佈私人列表 foursquareList;在頂部並將其設置爲OnCreateView函數中的數據設置爲foursquareList = new ArrayList (Arrays.asList(data));並在OnPostExecute像你提到的foursquareList.add(ItemStr),但它仍然沒有刷新與Listbox內容的屏幕,我可以看到一個斷點,它有兩個項目,但沒有工作 – user3294878 2014-09-21 05:06:59