gpt4 book ai didi

android - 修改listview tem OnActivityResult

转载 作者:行者123 更新时间:2023-11-30 03:04:05 25 4
gpt4 key购买 nike

我有一个 ListView,当单击一个项目时会启动一个新的 Activity。完成此 Activity 后,根据结果,我想更改该 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.

}

}

最佳答案

找到这个问题的答案: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);

关于android - 修改listview tem OnActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22103441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com