2014-06-20 26 views
0
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
@Override 
public void onItemClick(AdapterView<?> parent, final View view,int position, long id) { 
    //btnsub is button 
    btnsub.setEnabled(true); 
    Toast.makeText(getApplicationContext(), "before start()", Toast.LENGTH_SHORT).show(); 
    for(int i=0;i<j;i++){ 
     //lst is ListView Object 
     View vtmp = lst.getChildAt(i); 
     if(vtmp !=null){ 
     if(i==lst.getCheckedItemPosition()){ 
      //cindex is int var 
      cindex=lst.getCheckedItemPosition(); 
      }else{ } 
     } 
    } 

    new Thread() { 
     public void run() { 
      Toast.makeText(getApplicationContext(), "in Thread ", Toast.LENGTH_LONG).show(); 
      try{ 
        Thread.sleep(1000); 
        lst.getChildAt(cindex).setBackgroundColor(Color.BLUE); 
      }catch(Exception e){ 
       Toast.makeText(getApplicationContext(), "Thread Generate: "+e.getMessage(), Toast.LENGTH_LONG).show(); 
       } 
      } 
    }.start(); 


    Toast.makeText(getApplicationContext(), "after start()", Toast.LENGTH_SHORT).show(); 
    } 
}); 
+3

土司線程是錯誤的。在ui線程上顯示吐司。也設置背景顏色。爲什麼你需要線程在第一位?併發布stacktrace – Raghunandan

+0

我已經設置listView項目背景顏色直接在if條件,但這不工作正常多數民衆贊成爲什麼我使用線程的任何其他選項,然後建議我... –

+0

嘗試更改項目背景時,特定項目CheckBox檢查更改適配器getView()。用CheckBox檢查更改監聽器。 –

回答

0

您無法更新工作線程中的UI。修改代碼:

new Thread() { 
     public void run() { 

      try{ 
        Thread.sleep(1000); 
        mActivity.runOnUiThread(new Runnable() { 
      public void run() { 
       Toast.makeText(getApplicationContext(), "in Thread ", Toast.LENGTH_LONG).show(); 
       lst.getChildAt(cindex).setBackgroundColor(Color.BLUE); 
      } 
      }); 
      }catch(Exception e){ 

       } 
      } 
    }.start(); 

一個更好的辦法是使用處理程序,你可以閱讀 - Communicating with the UI Thread

0

通過下面的代碼替換該螺紋部分:

runOnUiThread(new Runnable() { 
        @Override 
         public void run() { 
            Toast.makeText(getApplicationContext(), "in Thread ", Toast.LENGTH_LONG).show(); 
      try{ 
        Thread.sleep(1000); 
        lst.getChildAt(cindex).setBackgroundColor(Color.BLUE); 
      }catch(Exception e){ 
       Toast.makeText(getApplicationContext(), "Thread Generate: "+e.getMessage(), Toast.LENGTH_LONG).show(); 
       } 
      } 
         } 
        });