在WorkDetails中有一个listView
。单击列表时,它将意图编辑详细信息。单击EditDetails中的保存按钮时,会将更新后的值返回到WorkDetails
编辑详细信息
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();
sqlcon.open();
sqlcon.Update(ID, project1, description);
returnIntent.putExtra("project1", project1);
returnIntent.putExtra("description", description);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
工作详细信息
ListView listViewUpdate;
CustomBaseAdapter obj;
ArrayList<DetailsBean> results = new ArrayList<DetailsBean>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
listViewUpdate = (ListView) edit_details.findViewById(R.id.listViewEdit);
obj = new CustomBaseAdapter(getActivity(), results, listViewUpdate);
listViewUpdate.setAdapter(obj);
listViewUpdate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
mClickedPosition = position; // intent to EditDetils
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this listview_item_row in the database.
ID =
cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
Intent intent = new Intent(getActivity(), Edit_Details.class);
intent.putExtra("ID", ID);
startActivityForResult(intent, PROJECT_REQUEST_CODE);
}
});
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("project");
ReceiveDescription = data.getStringExtra("description");
if (mClickedPosition == -1) {
Toast.makeText(getActivity(), "A", Toast.LENGTH_LONG).show();
} else {
Log.e("R",results+"");
Log.e("P",mClickedPosition+"");
results.get(mClickedPosition).setProject(ReceiveProject);
results.get(mClickedPosition).setDescription(ReceiveDescription);
obj.notifyDataSetChanged();
}
}
}
}
CustomBaseAdpter
public class CustomBaseAdapter extends BaseAdapter{ // for ListView
private static ArrayList<DetailsBean> searchArrayList;
private LayoutInflater mInflater;
ListView listview;
public CustomBaseAdapter(Context context, ArrayList<DetailsBean> results,ListView listview) {
searchArrayList = results;
this.listview=listview;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
DetailsBean
public class DetailsBean {
private String project="";
private String description="";
public void setProject(String project){
this.project=project;
}
public String getProject()
{
return project;
}
public void setDescription(String description){
this.description=description;
}
public String getDescription()
{
return description;
}
}
如何刷新WorkDetails列表视图,使其能够显示是否从EditDetails返回更新的值?当我单击WorkDetails中的列表时,它会显示更新后的值。
WorkDetails扩展到Fragment,EditDetails扩展到Activity
LogCat错误
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.project.myapplication.GUI.Edit_WorkDetails.onActivityResult(Edit_WorkDetails.java:123)
- 如果您确定
EditDetails
何时更新adapter
中的数据,请使用onActivityResult
中的adapter.notifyDataSetChanged()
更改项目单击。
listViewUpdate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
mClickedPosition = position; // intent to EditDetils
DetailsBean detailsBean=results.get(position);
// Get the state's capital from this listview_item_row in the database.
ID =detailsBean.getID;
Intent intent = new Intent(getActivity(), Edit_Details.class);
intent.putExtra("ID", ID);
startActivityForResult(intent, PROJECT_REQUEST_CODE);
}
});
@Override
protected void onRestart() {
super.onRestart();
// call your adapter from here
obj = new CustomBaseAdapter(getActivity(), results, listViewUpdate);
listViewUpdate.setAdapter(obj);
}
然后再次从onRestart()设置适配器并进行检查
尝试以下代码-
public void changeItem(String Project,String Description)
{
DetailsBean obj = new DetailsBean();
obj.setProject(" "+Project);
obj.setDescription(" "+" Work Description : " + " "+Description);
searchArrayList.add(obj);
add(obj);
notifyDataSetChanged();
}