2015-08-25 28 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()不仅限于基元类型。实验与,看看你能做到什么=)