In Edit_WorkDetails, it has a listView used to populate the data retrieved from SQLite.
ListView listViewUpdate;
CustomBaseAdapter obj;
private SimpleCursorAdapter dataAdapter;
Long ID, iD;
ArrayList<DetailsBean> results = new ArrayList<DetailsBean>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View edit_details = inflater.inflate(R.layout.edit_work_details, container, false);
dbHelper = new MyDatabaseHelper(getActivity());
sqlcon = new WorkDetailsAPI(getContext());
listViewUpdate = (ListView) edit_details.findViewById(R.id.listViewEdit);
obj = new CustomBaseAdapter(getActivity(), results, listViewUpdate);
listViewUpdate.setAdapter(obj);
Bundle bundle = this.getArguments();
if (getArguments() != null) {
ID = bundle.getLong("ID"); // receive ID from other activity, Assume is ID=1
BuildEditDetails(ID);
}
public void BuildEditDetails(long ID) {
final long id = ID;
Toast.makeText(getActivity(), id + "", Toast.LENGTH_LONG).show();
sqlcon.open();
Cursor cursor = sqlcon.readData(id);
String[] columns = new String[]{MyDatabaseHelper.Project, MyDatabaseHelper.WorkDescription};
int[] to = new int[]
{
R.id.Project, R.id.Description };
dataAdapter = new SimpleCursorAdapter(getActivity(), R.layout.retrieve_details, cursor, columns, to, 0);
listViewUpdate.setAdapter(dataAdapter);
}
When the list is clicked,it will go to Edit_Details for edit.
listViewUpdate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
mClickedPosition = position;
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
iD = cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
Toast.makeText(getActivity(), iD + "", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getActivity(), Edit_Details.class);
intent.putExtra("ID", iD);
startActivityForResult(intent, PROJECT_REQUEST_CODE);
}
});
iD represent the id in listView and value 2 is displayed.
Edit_Details
All the data which belong to iD (2) will be displayed on Edit_Details. When the save button is clicked, updated value will be saved to SQLite and return to Edit_WorkDetails listView.
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); // update the latest value in SQLite
returnIntent.putExtra("project1", project1);
returnIntent.putExtra("description", description);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
And this is the OnActivityResult for Edit_WorkDetails
@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("Project");
ReceiveDescription = data.getStringExtra("Description");
if (mClickedPosition == -1) { // if icon clicked
if (obj != null) {
Toast.makeText(getActivity(), "S", Toast.LENGTH_LONG).show();
}
} else {
if (obj != null) // obj is CustomBaseAdapter object
obj.changeItem(mClickedPosition, ReceiveProject, ReceiveDescription);
}
}
}
}
CustomBaseAdapter
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 void changeItem(int m,String P,String D)
{
DetailsBean obj = new DetailsBean();
obj.setProject(" "+P);
obj.setDescription(" "+" Work Description : " + " "+D);
searchArrayList.set(m,obj);
this. notifyDataSetChanged();
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
DetailsBean
private String project="";
private String description="";
public class DetailsBean {
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;
}
}
When save button in Edit_Details is clicked, app crashed.
LogCat Error
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.set(ArrayList.java:481)
at com.example.project.myapplication.Adapter.CustomBaseAdapter.changeItem(CustomBaseAdapter.java:47)
at com.example.project.myapplication.GUI.Edit_WorkDetails.onActivityResult(Edit_WorkDetails.java:125)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156)
Can someone help me?
These two codes have error
obj.changeItem(mClickedPosition, ReceiveProject, ReceiveDescription);
and
searchArrayList.set(m,obj);
Aucun commentaire:
Enregistrer un commentaire