活动A 中有listView
和添加button
。从listView
检索MySQL
数据。
[![http://i.stack.imgur.com/wVmSK.png][1]][1]
点击列表时,会将数据传递给活动B ,如果点击添加button
,则会转到活动B 以供用户使用添加数据。我使用mClickedPosition
来区分点击list
或点击button
。
活动A
ListView listViewUpdate;
ListAdapter adapter;
Button add;
String ID, iD;
public static final int PROJECT_REQUEST_CODE = 1;
public static final int CAMERA_REQUEST_CODE = 2;
int mClickedPosition;
String ReceiveProject, ReceiveDescription, ReceiveTimeIn, ReceiveTimeOut;
Integer ReceiveProgress;
ArrayList<DetailsBean> results = new ArrayList<DetailsBean>();
String myJSON;
JSONArray details = null;
ArrayList<HashMap<String, String>> EditDetails;
listViewUpdate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
mClickedPosition = position; // listView click
HashMap<String, String> clickedItem = EditDetails.get(position);
iD = clickedItem.get(Configs.TAG_ID);
Intent intent = new Intent(getActivity(), Edit_Details.class);
intent.putExtra("iD", iD);
intent.putExtra("ID", ID);
intent.putExtra("mClickedPosition",mClickedPosition);
//Toast.makeText(getActivity(),"This is"+iD+ID,Toast.LENGTH_LONG).show();
startActivityForResult(intent, PROJECT_REQUEST_CODE);
}
});
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mClickedPosition = -1; // if button clicked
Intent intent = new Intent(getActivity(), Edit_Details.class);
intent.putExtra("ID", ID);
startActivity(intent);
}
});
假设用户点击添加button
。(mClickedposition == - 1)
活动B
点击保存button
后,数据将插入MySQL
,因为 mClickedPosition == - 1 之后,我希望新数据返回到活动A并添加A中的新列表,表示活动A现在应该有2个列表。我怎样才能做到这一点?
save.setOnClickListener(new View.OnClickListener() { // if save button clicked
@Override
public void onClick(View v) {
Intent returnIntent = new Intent();
project1 = Project2.getSelectedItem().toString();
description = Description.getText().toString();
progress = seekBar.getProgress();
timeIn = TimeIn.getText().toString();
timeOut = TimeOut.getText().toString();
if(mClickedPosition==-1)
{
Add(project1,description,progress,timeIn,timeOut);
}
else
{
Update(project1, description, progress, timeIn, timeOut);
}
returnIntent.putExtra("project1", project1);
returnIntent.putExtra("description", description);
returnIntent.putExtra("progress", progress);
returnIntent.putExtra("timeIn", timeIn);
returnIntent.putExtra("timeOut", timeOut);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
活动A onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PROJECT_REQUEST_CODE) {
ReceiveProject = data.getStringExtra("project1");
ReceiveDescription = data.getStringExtra("description");
ReceiveProgress = data.getIntExtra("progress", 0);
ReceiveTimeIn = data.getStringExtra("timeIn");
ReceiveTimeOut = data.getStringExtra("timeOut");
Toast.makeText(getActivity(),ReceiveProject+ReceiveDescription+ReceiveProgress+ReceiveTimeIn+ReceiveTimeOut,Toast.LENGTH_LONG).show();
if(mClickedPosition==-1)
{ // add list
HashMap<String, String> clickedItem = new HashMap<>();
clickedItem.put(Configs.TAG_PROJECT, ReceiveProject);
clickedItem.put(Configs.TAG_WORKDESCRIPTION, ReceiveDescription);
clickedItem.put(Configs.TAG_PERCENTAGE, ReceiveProgress + "");
clickedItem.put(Configs.TAG_IN, ReceiveTimeIn);
clickedItem.put(Configs.TAG_OUT, ReceiveTimeOut);
EditDetails.add(clickedItem);
((BaseAdapter) adapter).notifyDataSetChanged();
}
else
{ // update list
HashMap<String, String> clickedItem =EditDetails.get(mClickedPosition);
clickedItem.put(Configs.TAG_PROJECT, ReceiveProject);
clickedItem.put(Configs.TAG_WORKDESCRIPTION, ReceiveDescription);
clickedItem.put(Configs.TAG_PERCENTAGE, ReceiveProgress + "");
clickedItem.put(Configs.TAG_IN, ReceiveTimeIn);
clickedItem.put(Configs.TAG_OUT, ReceiveTimeOut);
((BaseAdapter) adapter).notifyDataSetChanged();
}
//((BaseAdapter) adapter).notifyDataSetChanged();
}
}
}
}
现在我可以看到活动A中添加了一个新列表。但是当我点击新列表时 列表并转到活动B,不传递任何数据。但如果我点击旧的 列表,我可以看到传递的数据...
答案 0 :(得分:1)
您需要在列表中添加或更新新数据,然后调用notifiDataSetChanged()
;
更改活动A的onActivity结果方法中的代码,如下所示 -
HashMap<String, String> item = new HashMap<>();
item.put(Configs.TAG_PROJECT, ReceiveProject);
item.put(Configs.TAG_WORKDESCRIPTION, ReceiveDescription);
item.put(Configs.TAG_PERCENTAGE, ReceiveProgress + "");
item.put(Configs.TAG_IN, ReceiveTimeIn);
item.put(Configs.TAG_OUT, ReceiveTimeOut);
if(mClickedPosition==-1)
{ // add list
EditDetails.add(item);
}
else
{ // update list
EditDetails.remove(mClickedPosition);
mEventsList.add(mClickedPosition,item)
}
((BaseAdapter) adapter).notifyDataSetChanged();