mardi 13 janvier 2015

Refreshing cursor after on ClickMethod

I have some issues with next problem...method fillList() is populating a listview with my CustomAdapter...method is called from my MainActivity and it gets passed object from it i have to refresh my cursor after onClick method to fill list with data with different arguments public class FragmentChosenDiet extends ListFragment implements View.OnClickListener {



Button next, previous;
Calendar cal = Calendar.getInstance();
int DayOfTheWeek;
String formattedDate;
TextView date;
SimpleDateFormat df;
SQLController sqlC;
private ArrayList items;
private CustomAdapter cAdapter;


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

View v = inflater.inflate(R.layout.fragment_chosen_diet, container, false);

return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

//get current date
df = new SimpleDateFormat("dd.MM.yyyy EEEE");
formattedDate = df.format(cal.getTime());


date = (TextView) getActivity().findViewById(R.id.Datum);
next = (Button) getActivity().findViewById(R.id.bNext);
previous = (Button) getActivity().findViewById(R.id.bPrevious);
DayOfTheWeek=cal.get(Calendar.DAY_OF_WEEK);
date.setText(formattedDate + " " + DayOfTheWeek);


next.setOnClickListener(this);
previous.setOnClickListener(this);

}


filling List second argument in where clause of cursor is dayOfTheWeek from Calendar class of android



public void LoadGrid(Object obj) {
//


DayOfTheWeek=cal.get(Calendar.DAY_OF_WEEK);
sqlC = new SQLController(getActivity());
sqlC.open();


this is my cursor which is getting data from my database,with selected arguments: obj.getID() which i'm getting from mainActivity and DayofTheWeek which is number of the day of the week from calendar as you can see



DayOfTheWeek=cal.get(Calendar.DAY_OF_WEEK);
Cursor c = sqlC.readJelovnikTable(obj.getID(),DayOfTheWeek);
c.moveToFirst();
if (!c.isAfterLast()) {
do {

int tableID = c.getInt(c.getColumnIndex(SQLController.colJelPrehID_fk));
String col1Table1 = c.getString(c.getColumnIndex(SQLController.colJelvrID_fk));
String col2Table1 = c.getString(c.getColumnIndex(SQLController.colJelDorID_fk));


items.add(new Items(tableID,col1Table1,col2Table1))

} while (c.moveToNext());
}
c.close();
setListAdapter(cAdapter =new CustomAdapter(getActivity(), android.R.layout.simple_list_item_1,
items));
}


on Click method



@Override
public void onClick(View v) {

switch(v.getId()){


i'm changing date in my onClick method and my problem is i don't know how to refresh cursor with changed Date i.e. DayOfTheWeek..so it can populate //list view with new data, i can't call again my LoadGrid() method from here because i can't pass object to it...like i said before i'm getting my object from main activity



case R.id.bNext:
cal.add(Calendar.DATE, 1);
formattedDate = df.format(cal.getTime());
DayOfTheWeek=cal.get(Calendar.DAY_OF_WEEK);
date.setText(formattedDate + " " + DayOfTheWeek);
cAdapter.notifyDataSetChanged();

break;


same thing here for decreasing date

//previous date



case R.id.bPrevious:
cal.add(Calendar.DATE, -1);
formattedDate = df.format(cal.getTime());
date.setText(formattedDate + " " + cal.get(Calendar.DAY_OF_WEEK));

break;

}
}


and my cursor from SQLController



public Cursor readTable(int objID,int dayID){
database = dbHelper.getReadableDatabase();
String[] allColumns=new String[]{colID,col1,col2,col3,col4};
Cursor c=database.query(Table1,allColumns,col1ID+"=? AND "+col2ID_fk+"=?",
new String[]{String.valueOf(objID),String.valueOf(dayID)},null,null,null);
if (c != null) {
c.moveToFirst();
}
return c;
}

Aucun commentaire:

Enregistrer un commentaire