如何使用onActivityResult更改Item ListView颜色?

时间:2015-08-25 21:54:54

标签: android listview android-listview

我有活动A和listView,活动B有表格。我从活动A中触摸一个项目并从活动B中调用该表格。例如。我触摸一个项目“Laura”并打开一个关于她的表格,在我填写表格并完成活动B后,我返回字符串“Laura”,我想从Laura更改Item ListView颜色。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

您应该使用布尔值来检查您是否在Laura帐户上进行了一些更改,因此它看起来像这样:

@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;
}

答案 1 :(得分:0)

除了" Laura"之外还有别的东西。可能会返回捆绑中的对象? OnActivityResult()并不仅限于原始类型。试验一下,看看你能做些什么=)