WorkDetails 中有listView
。单击列表时,它将意图 EditDetails 。单击 EditDetails 中的保存按钮后,它会将更新后的值返回工作详细信息。
EditDetails
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();
}
});
WorkDetails
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 listView以便显示它 从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)
答案 0 :(得分:1)
EditDetails
何时更新adapter
中的数据,请使用adapter.notifyDataSetChanged()
中的onActivityResult
答案 1 :(得分:0)
@Override
protected void onRestart() {
super.onRestart();
// call your adapter from here
obj = new CustomBaseAdapter(getActivity(), results, listViewUpdate);
listViewUpdate.setAdapter(obj);
}
然后再次从onRestart()设置适配器并检查。
答案 2 :(得分:0)
尝试以下代码 -
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();
}
答案 3 :(得分:0)
更改onItemClick。
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);
}
});