2016-10-22 37 views
0

我有一個應用程序,其中我發送了一個服務器請求,並在響應服務器發送給我一組數組,並從該組數組中提取一個名爲「dealname」的字符串,之後我有一個textview,我想要每5秒後顯示一次這個字符串,比如使用Thread.sleep(),當arraycome結束時,我想從第一個數組開始。我該怎麼做才能幫上忙。如何在指定的時間間隔內在textview中顯示arrayList中的字符串?

這裏是我想

private CDealAppDatastorage item; 
private String TAG = MainActivity.class.getSimpleName(); 
private ArrayList<CDealAppDatastorage> s_oDataset; 

try{ 
    String json; 
    JSONObject jsonObject = new JSONObject(); 
    jsonObject.put("dealcategory","DEALTEST"); 
    json = jsonObject.toString(); 
    Log.e(TAG,"Server Reqeust::-"+json); 

    final String m_szTicker = "http://14.192.16.1555:8080/rest/json/metallica/getDealListsJSON?"; 
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, m_szTicker,jsonObject, new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      Log.e(TAG,"Server Response::"+response); 
      try{ 

       if (response.getString("resultdescription").equalsIgnoreCase("Transaction Successful")){ 
        JSONArray posts = response.optJSONArray("dealList");// get Deal list in array from r 
        for (int i = 0; i < posts.length(); i++) {// loop for counting deals from server 
         JSONObject post = posts.getJSONObject(i);// counting deal based on index 
         item = new CDealAppDatastorage();// creating object of DealAppdata storage 
         item.setM_szHeaderText(post.getString("dealname"));// get deal name from response 
         s_oDataset.add(item);// add all items in ArrayList 

        } 
        int i; 
        for (i=0;i<s_oDataset.size();i++){ 
         Log.e(TAG,"Data Size:"+i); 

         final int finalI = i; 
         new Handler().postDelayed(new Runnable() { 
          @Override 
          public void run() { 
           CDealAppDatastorage m = s_oDataset.get(finalI); 
           m_Text.setText(m.getM_szHeaderText()); 
          } 
         },6000); 

        } 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 
    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());// creating object of Request queue 
    requestQueue.add(jsonObjectRequest);// add json Object request to request queue 
}catch (Exception e){ 
    e.printStackTrace(); 

回答

0

改變循環邏輯,所以它會如果列表結束重新開始的代碼。

int i; 
int size = s_oDataset.size(); 
for (i=0;;i++){ 
        Log.e(TAG,"Data Size:"+i); 

        final int finalI = i % size; //this will loop again from start if lists ends 
        new Handler().postDelayed(new Runnable() { 
         @Override 
         public void run() { 
          CDealAppDatastorage m = s_oDataset.get(finalI); 
          m_Text.setText(m.getM_szHeaderText()); 
         } 
        },6000); 

       } 
+0

相同爲是否有圖像,而不是TextView的 – Raghav

+0

可以解釋小大約爲(I = 0 ;;我++) – Raghav

+0

這裏沒有環路終止條件。 '我'會繼續增加,但'我%大小'將始終給出一個在列表大小範圍內的否。 – Sanjeet

相關問題