2016-03-01 145 views
-1

我目前有一個方法,它從用戶輸入創建一個URL,發送一個HTTP請求來獲得JSONArray,然後將JSONArray轉換成一個ArrayList。出於某種原因,儘管事實上循環計數器沒有告訴它這樣做,但是爲了添加每個JSONObject之一的循環只複製同一個循環10次。很顯然,我失去了一些東西,但這裏是下面的代碼:循環遍歷一個JSONArray將字符串轉換爲對象

MainActivity

public ArrayList<Eatery> fillArray(String url) { 


    String line; 
    Eatery eatery = new Eatery("null","null","null","null","null", 
      "null","null","null","null","null"); 
    ArrayList<Eatery> eateryList = new ArrayList<>(); 

    if (getConnection() == true) { 
     try { 

      URL urlFinal = createURL(url); 
      HttpURLConnection postcodeConnection = (HttpURLConnection) urlFinal.openConnection(); 
      InputStreamReader isr = new InputStreamReader(postcodeConnection.getInputStream()); 
      BufferedReader bf = new BufferedReader(isr); 

      while ((line = bf.readLine()) != null) { 
       JSONArray ja = new JSONArray(line); 
       for (int i = 0; i < ja.length(); i++) { 

        JSONObject jo = ja.getJSONObject(i); 
        String id = jo.getString("id"); 
        String businessName = jo.getString("BusinessName"); 
        String addressLine1 = jo.getString("AddressLine1"); 
        String addressLine2 = jo.getString("AddressLine2"); 
        String addressLine3 = jo.getString("AddressLine3"); 
        String postcode = jo.getString("PostCode"); 
        String ratingValue = jo.getString("RatingValue"); 
        String ratingDate = jo.getString("RatingDate"); 
        String lati = jo.getString("Latitude"); 
        String longi = jo.getString("Longitude"); 
        eatery.setId(id); 
        eatery.setBusinessName(businessName); 
        eatery.setAddressLine1(addressLine1); 
        eatery.setAddressLine2(addressLine2); 
        eatery.setAddressLine3(addressLine3); 
        eatery.setPostcode(postcode); 
        eatery.setRatingValue(ratingValue); 
        eatery.setRatingDate(ratingDate); 
        eatery.setLati(lati); 
        eatery.setLati(longi); 
        eateryList.add(eatery); 
       } 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }catch (IndexOutOfBoundsException e){ 
      Toast nameToast = Toast.makeText(getApplicationContext(), "Invalid search", Toast.LENGTH_LONG); 
      nameToast.show(); 
     } 
    } else { 
     Toast nameToast = Toast.makeText(getApplicationContext(), "Error: No Active Connection", Toast.LENGTH_LONG); 
     nameToast.show(); 
    } 

    return (eateryList); 
} 

內部方法:以上

public boolean getConnection(){ 

    conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    networkInfo = conMgr.getActiveNetworkInfo(); 
    boolean connected; 
    if(networkInfo != null && networkInfo.isConnected()) 
    { 
     connected = true; 
    } 
    else 
    { 
     connected = false; 
    } 
    return connected; 
} 

public URL createURL(String searchType) { 

    String URLstart = "http://sandbox.kriswelsh.com/hygieneapi/hygiene.php?op="; 
    searchType = searchType.replace(" ", "%20"); 
    String finalURL = URLstart.concat(searchType); 
    URL nameURL = null; 
    try { 
     nameURL = new URL(finalURL); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
    return nameURL; 
} 

在裏面ActivityMain並正在從它接受一個片段名爲來自EditText框的用戶輸入,將其添加到fillArray方法,然後將結果返回到在Fragment上實例化的另一個ArrayList。

片段下面的代碼:

public class NameFragment extends Fragment { 

EditText enterName; 
Button searchNameButton; 
TableLayout nameTableLayout; 
TextView testTextView, tv; 
TableRow tr; 
ArrayList<Eatery> nameEateryList; 

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

    enterName = (EditText) rootView.findViewById(R.id.enterNameText); 
    searchNameButton = (Button)rootView.findViewById(R.id.searchNameButton); 
    testTextView = (TextView)rootView.findViewById(R.id.testTextView); 
    nameTableLayout = (TableLayout)rootView.findViewById(R.id.NameTableLayout); 

    searchNameButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      String nameSearch = enterName.getText().toString().trim(); 
      String nameURLKey = "s_name&name="; 
      nameURLKey = nameURLKey.concat(nameSearch); 

      //Grab ArrayList 
      nameEateryList = ((MainActivity)getActivity()).fillArray(nameURLKey); 

      for(int i = 0; i < nameEateryList.size(); i++) { 

       tv = new TextView(getActivity().getApplicationContext()); 
       tr = new TableRow(getActivity().getApplicationContext()); 

       //Create Table Counter 
       int turn = nameTableLayout.getChildCount(); 
       turn += 1; 
       String turnString = turn + ""; 

       //Add TextView 
       tv.setText(nameEateryList.get(i).eateryToString()); 
       tv.setPadding(15, 15, 15, 15); 
       tr.setPadding(5,5,5,5); 
       tv.setBackgroundColor(Color.parseColor("#557788")); 
       tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START); 
       tr.addView(tv); 
       nameTableLayout.addView(tr); 
      } 
     } 
    }); 
    return rootView; 
    } 

出於某種原因,當這個循環通過ArrayList和複製結果到我的片段所有的結果都是一樣的。代碼運行並且ArrayList被填充,但它似乎無法循環訪問ArrayList。一直卡在這一段時間,但我只是無法弄清楚是什麼導致ArrayList重複複製同一個對象,而不是循環所有的結果。我希望這是有道理的。如果需要進一步的信息,我當然可以提供更多的代碼。這是一個相當具體的問題,我無法確定具有這個確切問題的另一個線程,如果此答案存在於別處,則表示歉意。

回答

0

創建for循環的每次迭代像這樣的新Eatery對象:

for (int i = 0; i < ja.length(); i++) { 
    Eatery eatery = new Eatery("null","null","null","null","null", 
     "null","null","null","null","null"); 

    JSONObject jo = ja.getJSONObject(i); 
    String id = jo.getString("id"); 
    String businessName = jo.getString("BusinessName"); 
    String addressLine1 = jo.getString("AddressLine1"); 
    String addressLine2 = jo.getString("AddressLine2"); 
    String addressLine3 = jo.getString("AddressLine3"); 
    String postcode = jo.getString("PostCode"); 
    String ratingValue = jo.getString("RatingValue"); 
    String ratingDate = jo.getString("RatingDate"); 
    String lati = jo.getString("Latitude"); 
    String longi = jo.getString("Longitude"); 
    eatery.setId(id); 
    eatery.setBusinessName(businessName); 
    eatery.setAddressLine1(addressLine1); 
    eatery.setAddressLine2(addressLine2); 
    eatery.setAddressLine3(addressLine3); 
    eatery.setPostcode(postcode); 
    eatery.setRatingValue(ratingValue); 
    eatery.setRatingDate(ratingDate); 
    eatery.setLati(lati); 
    eatery.setLati(longi); 
    eateryList.add(eatery); 
} 

你所面對的問題是,因爲你只是添加單個對象列表,爲每一個循環迭代和值對象將是您設置爲Eatery對象的最後一個值。

希望這會有所幫助!

+0

謝謝!我敢肯定,作爲一個完全沒有認識到這個問題的白癡,我一直在尋找解決辦法,但昨天我一直在尋找一個解決方案几個小時無濟於事。非常簡單的解決方案,非常感謝您的回覆。祝你今天愉快! –