2015-08-25 25 views
-1

我有活動A與listView和活動B與窗體。我接觸到來自活動A的項目並從活動B中調用表單。在填寫完表格並完成活動後,我觸摸一個項目「Laura」並打開一個關於她的表格。我返回字符串「Laura」,並且想要更改Laura的Item ListView顏色。我怎樣才能做到這一點?如何使用onActivityResult更改Item ListView顏色?

+0

嘗試檢查這個StackOverflow的鏈接出:對於答案http://stackoverflow.com/questions/5564789/change-listviews-textcolor – jcameron47

回答

1

您應該使用一個布爾檢查,如果你使勞拉帳戶的一些改變,所以它會是這個樣子:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
// Check which request we're responding to 
if (requestCode == REQUEST_ACCOUNT_LAURA) { 
    // Make sure the request was successful 
    if (resultCode == RESULT_OK) { 
     // The user changed Laura account. 
     boolean isAccountChanged= data.getBooleanExtra("isAccountChanged", false); 
     if (isAccountChanged) { 
      ListView listView = new ListView(this); 
      // Get the view associated with Laura, and change the background color. 
      ((View) listView.getItemAtPosition(position)).setBackgroundColor(R.color.black); 
       listView.getAdapter().notifyDataSetChanged(); 
    } 

     // Do something with the contact here (bigger example below) 
    } 
    } 
} 

您也可以使用這onListItemCLick,你可以在你的ListView設置:

View lastTouchedView; 

@Override 
public void onListItemClick(ListView parent, View v, int position, long id) 
lastTouchedView.setBackgroundColor(Color.WHITE); 
v.setBackgroundColor(Color.CYAN); 
lastTouchedView = v; 
} 
+0

感謝名單,但我不能在onActivityResult中不改變listView顏色,你試試看簡單的listView.setBackgroundColor(R.color.black),你不會得到它! – daniel12345smith

+0

您需要重置列表視圖適配器。可以使用 adapter.notifyDataSetChanged()或者只使用listView.setAdapter(new Adapter()...) – risto

0

除了「Laura」之外還會返回其他內容。也許返回一個包中的對象? OnActivityResult()不僅限於基元類型。實驗與,看看你能做到什麼=)