lundi 29 décembre 2014

How to update SQLite database in fragment? Android

I display my database in listview in a fragment. Also I add a new data in a dialog. I have a little problem about updating database when I add some data in dialog and when I return to my fragment which displays my database in listview, there is no data which I add recently. When I close and reopen my app, data displays in listview.


I guess you understand my issue.


Here is my codes. This my fragment where I display my database in listview.



public class JanuaryFragment extends Fragment {

private DB myDB;
private List<Person> people;
private ListView listView;
private Context mContext;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.january_fragment, container, false);

mContext = getActivity();

myDB = new DB(mContext);
people = getThisMonthsPeople( myDB.getAllPeople());

listView = (ListView)rootView.findViewById(R.id.listViewOfBirthdays);
listView.setAdapter(new PersonAdapter(mContext, people));

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
EditBirthdayDialog dialog = new EditBirthdayDialog(mContext, people.get(position));
dialog.show();
}
});

return rootView;
}

public List<Person> getThisMonthsPeople(List<Person> allPeople){
List<Person> resultPeople = new LinkedList<Person>();
for(int i = 0; i< allPeople.size(); i++){
if(allPeople.get(i).getMonth()==1){
resultPeople.add(allPeople.get(i));
}
}
return resultPeople;
}
}


Here is my dialog which add a new data.



public class AddBirthdayDialog extends Dialog {

private Context context;
String name;
String sname;
private int day;
private int month;
private int year;

final EditText textName;
final EditText textSname;
final DatePicker birthDate;

private DB myDB;
private SQLiteDatabase database;
private Person person = new Person();

public AddBirthdayDialog(Context context) {
super(context);
this.context = context;

setCanceledOnTouchOutside(false);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.addbirthday_layout);


myDB = new DB(context);


textName = (EditText) findViewById(R.id.editTextname);
textSname = (EditText) findViewById(R.id.editTextsname);
birthDate = (DatePicker) findViewById(R.id.datePicker);



Button cancelButton = (Button) findViewById(R.id.cancelbutton);
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clearForm();
dismiss();

}
});

Button save = (Button) findViewById(R.id.buttonadd);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

name = textName.getText().toString();
sname = textSname.getText().toString();

day = birthDate.getDayOfMonth();
month = birthDate.getMonth()+1;
year = birthDate.getYear();
final String dayStr = String.valueOf(day);
final String monthStr = String.valueOf(month+1);
final String yearStr = String.valueOf(year);

person.setName(name);
person.setSname(sname);
person.setDay(day);
person.setMonth(month);
person.setYear(year);
person.setDateStr(dayStr + "/" + monthStr + "/" + yearStr);

myDB.addBirthday(person);
clearForm();
dismiss();
}
});
}

public void setDatePicker(int day, int month, int year){
birthDate.updateDate(day,month,year);
}

//Cleans the form
public void clearForm(){
textName.setText("");
textSname.setText("");
textSname.setText("");
}
}


Thank you in advance!


Aucun commentaire:

Enregistrer un commentaire