我有一个ListView
,当点击一个项目时,会启动一个新的活动。完成此活动后,根据结果,我想更改该ListView
项的背景颜色。我怎样才能从onActivityResult
执行此操作?我知道被点击项目的位置,但我无法弄清楚要执行此操作的步骤。如果我去自定义适配器路由,如何告诉适配器哪个项目已更改?我目前正在使用SimpleAdapter
。
到目前为止我的相关代码
// Get listview
lv = getListView();
// on selecting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String assemblyName = ((TextView) view.findViewById(R.id.assemblyName)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
ViewOrderlineAssemblyActivity.class);
// sending pid to next activity
in.putExtra("assemblyName", assemblyName);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user has successfully completed assembly.
//add assembly to our list of assemblies
Assembly assembly = (Assembly) data.getSerializableExtra("assembly");
//if the assembly is found in our list, replace it, if we get to the end of the list and it is not found, add it to list.
for(int i = 0;i<assemblies.size();i++){
if(assemblies.get(i).getName().equalsIgnoreCase(assembly.getName())){
assemblies.remove(i);
assemblies.add(assembly);
}
else if(i == assemblies.size()-1){
assemblies.add(assembly);
}
}
//change background color of listview item so we know it has been completed.
}
}
答案 0 :(得分:1)
在这个问题中找到答案:How can I update a single row in a ListView?
,解决方案就是这段代码
//change background color of listview item so we know it has been completed.
View v = lv.getChildAt(position - lv.getFirstVisiblePosition());
TextView assemblyText = (TextView) v.findViewById(R.id.assemblyName);
assemblyText.setBackgroundColor(Color.BLUE);