在这里,我现在有listView和2个项目,如下图所示
这是用于SimpleAdapter
ListView
ListView listProductSup;
int listItemPos;
SimpleAdapter pdtAdapter;
pdtAdapter = new SimpleAdapter(
ProductProgramActivity.this, pdtDataList,
R.layout.product_supp_list, new String[]{"pdtSupName", "strKey", "strServQuan", "strMilkQuan", "strWaterQuan", "strDecocQuan", "strPowderQuan"},
new int[]{R.id.txt_list_pdt_supp, R.id.key_set, R.id.serve_quan_set, R.id.milk_quan_set, R.id.wat_quan_set, R.id.decoc_quan_set, R.id.pow_quan_set});
listProductSup.setAdapter(pdtAdapter);
listProductSup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent in = null;
listItemPos = position;
if (categIn.equals("Machine Installation"))
in = new Intent(ProductProgramActivity.this, ProgramSettingActivity.class);
else if (categIn.equals("Machine Disconnection"))
in = new Intent(ProductProgramActivity.this, MachineDisconActivity.class);
in.putExtra("idOfIncident", idOfIncident);
in.putExtra("orgId", orgId);
in.putExtra("assetId", assetId);
in.putExtra("pdtId", pdtIdArr.get(position));
in.putExtra("pdtSupId", pdtSupIdArr.get(position));
startActivityForResult(in,1);
}
});
点击列表项后,它会转到next activity
。我在该活动中获取textFields
的所有值。从那里onBackPressed
我发回所有指定的值,如此处
public void backWithData() {
Intent in = new Intent();
in.putExtra("pdtSupId", pdtSupId);
in.putExtra("strKey", strKey);
in.putExtra("strServQuan", strServQuan);
in.putExtra("strMilkQuan", strMilkQuan);
in.putExtra("strWaterQuan", strWaterQuan);
in.putExtra("strDecocQuan", strDecocQuan);
in.putExtra("strPowderQuan", strPowderQuan);
setResult(RESULT_OK, in);
}
@Override
public void onBackPressed() {
backWithData();
finish();
}
在上一个活动中,我通过覆盖onActivityResult
来获取所有值。
在这里,我尝试更新ListView的单个项目,该项目被单击以提供所有值。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
pdtSupId = data.getStringExtra("pdtSupId");
strKey = data.getStringExtra("strKey");
strServQuan = data.getStringExtra("strServQuan");
strMilkQuan = data.getStringExtra("strMilkQuan");
strWaterQuan = data.getStringExtra("strWaterQuan");
strDecocQuan = data.getStringExtra("strDecocQuan");
strPowderQuan = data.getStringExtra("strPowderQuan");
int first = listProductSup.getFirstVisiblePosition();
int last = listProductSup.getLastVisiblePosition();
for (int i=0; i<=pdtDataList.size(); i++)
if (pdtSupIdArr.get(i).equals(pdtSupId))
if (i == listItemPos) {
View view = listProductSup.getChildAt(i);
listProductSup.getAdapter().getView(i, view, listProductSup);
((TextView)view.findViewById(R.id.key_set)).setText(strKey);
((TextView)view.findViewById(R.id.serve_quan_set)).setText(strServQuan);
((TextView)view.findViewById(R.id.milk_quan_set)).setText(strMilkQuan);
((TextView)view.findViewById(R.id.wat_quan_set)).setText(strWaterQuan);
((TextView)view.findViewById(R.id.decoc_quan_set)).setText(strDecocQuan);
((TextView)view.findViewById(R.id.pow_quan_set)).setText(strPowderQuan);
break;
}
pdtAdapter.notifyDataSetChanged();
}
}
}
但我不知道为什么没有更新。
要更新ListView
的单个项目,我参考了SO
的其他答案。像Answer 1和其他答案一样。
答案 0 :(得分:1)
嗨更新列表onActivity结果中的相应对象然后调用notifyDatasetChanged(),它会自动更新ui ...
答案 1 :(得分:0)
您需要更改for循环
for (int i=0; i<=pdtDataList.size()/.length; i++)
<强>更新强>
for (int i=0; i<=pdtDataList.length; i++)
if (pdtSupIdArr.get(i).equals(pdtSupId))
if (i == listItemPos) {
View view = listProductSup.getChildAt(i);
listProductSup.getAdapter().getView(i, view, listProductSup);
((TextView)view.findViewById(R.id.key_set)).setText(strKey);
((TextView)view.findViewById(R.id.serve_quan_set)).setText(strServQuan);
((TextView)view.findViewById(R.id.milk_quan_set)).setText(strMilkQuan);
((TextView)view.findViewById(R.id.wat_quan_set)).setText(strWaterQuan);
((TextView)view.findViewById(R.id.decoc_quan_set)).setText(strDecocQuan);
((TextView)view.findViewById(R.id.pow_quan_set)).setText(strPowderQuan);
break;
}
pdtAdapter.notifyDataSetChanged();
}
答案 2 :(得分:0)
好吧,在onBackPressed
更改相应对象中的数据并调用notifyDataSetChanged()
或者,您可以使用这样的方法(来自我的项目的示例)
private void updateEventInList(boolean likeState, boolean followedState, int comments, int viewsNr, ViewHolder views) {
if (parentFragment != null && parentFragment instanceof EventListFragment) {
Log.i(TAG, "Position in list : " + positionInList + "\nlike / follow states : " + likeState + "/" + followedState + "\ncomments number : " + comments + "\nviews number : " + viewsNr);
((EventListFragment) parentFragment).updateList(positionInList, likeState, followedState, comments, viewsNr);
}
}
其中updateList
是这样的:
public void updateList(int positionInList, boolean likeState, boolean followedState, int numberOfComments, int numberOfViews) {
eventsResponses.get(positionInList).setLiked(likeState);
eventsResponses.get(positionInList).setFollowed(followedState);
eventsResponses.get(positionInList).setComments(numberOfComments);
eventsResponses.get(positionInList).setViews(numberOfViews);
adapter.notifyDataSetChanged();
Util.hideKeyboard(activity);
}
注意我打电话给adapter.notifyDataSetChanged();
。这是listview的适配器。希望这会对你有所帮助