2015-11-17 73 views
1

從服務器獲得響應後,我在我的整數數組中存儲點,我試圖在我的水平滾動視圖中添加該數組,但它給了我循環中的錯誤,以下是我的代碼誰能幫助?謝謝提前近此行不能解決方法get(int)

錯誤

tv.setText(points.get(i)); 

JAVA

protected ArrayList<HashMap<String, String>> doInBackground(
       String... args) { 
      ServiceHandler sh = new ServiceHandler(); 

      // Making a request to url and getting response 

      String jsonStr = sh.makeServiceCall(PLACE_URL, ServiceHandler.GET); 

      Log.d("Response: ", "> " + jsonStr); 

      if (jsonStr != null) { 
       try { 
        placejsonObj = new JSONArray(jsonStr); 
        // state_list = jsonObj.getJSONArray(COUNTRY_LIST); 
        // looping through All Contacts 

         jobject = placejsonObj.getJSONObject(0); 
         msgs=jobject.getString("user_status"); 

         pointsarray=placejsonObj.getJSONArray(1); 
         // points=pointsarray.getString("point"); 
        System.out.println("Kya yar" + "Failure"+pointsarray); 
        points = new int[pointsarray.length()]; 
        for(int m=0; m<pointsarray.length(); m++) { 
         points[m] = pointsarray.getJSONObject(m).getInt("point"); 
        } 
        System.out.println("array contains" + points.length + " elements"); 


       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } else { 
       Log.e("ServiceHandler", "Couldn't get any data from the url"); 
      } 
      return null; 
     } 


protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 

      super.onPostExecute(result); 
      pDialog.dismiss(); 




     for (int i = 0; i < points.length; i++) { 
      tv = new TextView(getActivity()); 
      tv.setText(points[i]+","); 
      tv.setTag(points[i]); 
      yourLayout.addView(tv); 
     } 

     tv.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       int point = (int) view.getTag(); 
       Toast.makeText(getActivity(),point,Toast.LENGTH_SHORT).show(); 
      } 
     }); 


     } 
    } 

回答

2

array所以不喜歡

tv.setText(""+points[i]); 
+0

感謝它workd ....這樣的sillly錯誤 – albert

1

Array獲得項目使用points[i]並從List獲得項目使用points.get(i);

+0

感謝幫助.. – albert

+0

dhaval你可以告訴如何獲得對textview點擊監聽器選定的點? – albert

+0

@Jolly你可以使用TextView的setTag和getTag方法。 tv.setTag(points.get(I));並在onClick方法,使用int point =(int)view.getTag() –

1

你有2個問題,我看到的。首先是嘗試使用int設置文本,第二步是嘗試訪問數組中數據的方式(您將它用作列表)。嘗試像這樣:

tv.setText(points[i] + ""); 
+0

編輯出點,謝謝@MD –

+0

感謝幫助.... – albert

相關問題