2016-12-05 205 views
-1

我想在500毫秒後點擊更改背景。它現在所做的只是黑色。它甚至不會僅僅以黑屏顯示視圖。顏色列表來自數據庫,JSON數據被解析爲顏色值。我使用的是Android API 6 23.更改背景顏色不起作用

public class CycleColors extends AppCompatActivity { 

    private static final String TAG  = CycleColors.class.getName(); 

    static List<String> colorsList = new ArrayList<>(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_cycle_colors); 

     Button doneBtn = (Button)findViewById(R.id.btnDone); 

     doneBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       clickDoneButton(); 
      } 
     }); 

     getData(); 
     Intent intent = getIntent(); 
     new cycle(this).execute(); 

    } 

    public void clickDoneButton() 
    { 
     finish(); 
    } 

    class cycle extends AsyncTask<Void, Void, Void> 
    { 
     private Activity activity; 
     View    view; 

     public cycle(Activity a) 
     { 
      activity = a; 
      view  = activity.getWindow().getDecorView(); 
     } 

     protected Void doInBackground(Void... param) 
     { 
      activity.runOnUiThread(new Runnable() 
      { 
       @Override 
       public void run() 
       { 
        while(true) 
        { 
         for (String c: colorsList) 
         { 
          int color = Color.parseColor(c); 
          Log.d(TAG, color+""); 
          view.setBackgroundColor(color); 
          SystemClock.sleep(500); 
         } 
        } 
       } 
      }); 
      Log.d(TAG, "returned null"); 
      return null; 
     } 
    } 



    private void getData() { 

     final String   serverURL; 
     final JsonArrayRequest request; 
     final RequestQueue  queue; 

     serverURL = "https://api.mlab.com/api/1/databases/comp3717final/collections/colours?apiKey=qR2ag5UaRrHBxDm6KEyg95EESmfY5Bcf"; 
     queue  = Volley.newRequestQueue(this); 
     request  = new JsonArrayRequest(serverURL, 
       new onJSONResponse(), 
       new onJSONError()); 
     queue.add(request); 
    } 

    private class onJSONResponse implements Response.Listener<JSONArray> 
    { 
     @Override 
     public void onResponse(JSONArray response) 
     { 
      final int length; 
      int i; 
      i = 0; 
      length = response.length(); 

      try { 
       for (; i < length; i++) { 
        final JSONObject colorObject; 
        final JSONObject hexObject; 
        final String colorName; 
        final String hexCode; 

        colorObject = response.getJSONObject(i); 
        colorName = colorObject.getString("color"); 
        hexCode = colorObject.getString("value"); 
        Log.d(TAG, colorName + " => " + hexCode); 
        colorsList.add(colorName); 
       } 
      } catch (final JSONException ex) { 
       Log.d(TAG, "Error getting json object: " + i, ex); 
       colorsList.clear(); 
      } 
      Log.d(TAG, "working"); 
     } 
    } 

    private static class onJSONError implements Response.ErrorListener 
    { 
     @Override 
     public void onErrorResponse(VolleyError error) 
     { 
      Log.d(TAG, "JSON ERROR"); 
     } 
    } 
} 

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.peymantp.androidfinal.CycleActivity"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Done" 
     android:id="@+id/button6" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginBottom="32dp" /> 
</RelativeLayout> 
+0

你不需要'AsyncTask'來改變背景顏色,而'while(true)'是一個無限循環。如果你想推遲一些代碼,我會推薦'Handler.postDelayed' http://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android –

+0

我試過了你鏈接到什麼。我不得不添加'Looper.prepare()',否則應用程序會崩潰。與之前不同,我現在可以看到視圖,但它仍然不會循環顯示顏色。繼承人更新後的代碼。 'Looper.prepare(); final Handler handler = new Handler(); handler.postDelayed(new Runnable(){@Override public void run() {for(String c:colorsList){int color = Color.parseColor(c); Log.d(TAG,color +「」); view .setBackgroundColor(color); } } },100);' – noname

回答

0

的getData具有網絡電話和執行的AsyncTask同時發生,這可能會導致colorList初始化的AsyncTask完成後。您需要在AsysncTask訪問colorList之前以同步方式調用getData。

+0

我該如何以同步的方式做到這一點?你能給個例子嗎? – noname

+0

用簡單的方法,當您在onResponse回調中的colorList中完成項目的添加後,將一個布爾變量設置爲true。在doInBackgroiund中,無限期地檢查布爾變量。 – Vinodh