In my project, I have a Fragment
that is a ListFragment
. What I have done so far is filling the List with data from a SQLite
database; but now, I am facing this problem:
If I've no data in the database, my ListFragment
will be empty; that's why I thought to put an Image, in place of list.
Now, I am stuck on how to use onCreateView
in my ListFragment
.
One alternative to this, that I found is: "using Fragment
instead of ListFragment
"
Is there any other way to do this, without replacing ListFragment
with Fragment
?
This goes inside of doInBackGround
:
Conexion = new MarketSQLite(getActivity(), "market", null, 1);
mItems = new ArrayList<ListViewItem>();
db = Conexion.getReadableDatabase();
Cursor c;
c = db.rawQuery("Select NOM_OFER,PREU_OFERTA,DATA_F,FOTO,PERCENTDESCOMPTE from T_OFERTA", null);
c.moveToFirst();
// HERE'S NOT NULL, SO I CAN PUT SOME STUFF
if (c != null) {
do {
for (int i = 0; i < c.getColumnCount(); i++) {
Title = c.getString((c.getColumnIndex("NOM_OFER")));
Preu = c.getColumnIndex("PREU_OFERTA");
percent = c.getString((c.getColumnIndex("PERCENTDESCOMPTE")));
data_f = c.getString((c.getColumnIndex("DATA_F")));
URLTest = c.getString((c.getColumnIndex("FOTO")));
Log.e("", "" + c.getString(i));
// initialize and set the list adapter
// Toast.makeText(getActivity(), "Title" + Title + "Preu" + Preu + "Percent" + percent + "Cheese is " + data_f, Toast.LENGTH_LONG).show();
}
mItems.add(new ListViewItem(URLTest, Title, Preu.toString(), percent, data_f));
} while (c.moveToNext());
}
if (c== null) {
getListView().setEmptyView(getActivity().findViewById(R.id.layout_empty));
// HERE I WANT TO ADD IMAGEVIEW CAUSE C IS NULL THEN THE FRAGMENT WON'T BE EMPTY
}
c.close();
And this is the error what I'm facing :
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at info.androidhive.slidingmenu.MisOfertasFragment$MyAsyncTask.doInBackground(MisOfertasFragment.java:108)
at info.androidhive.slidingmenu.MisOfertasFragment$MyAsyncTask.doInBackground(MisOfertasFragment.java:86)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
I made some changes with the cursor. I deleted some data on my SQLite
to test if it works; but now, it doesn't show anything.
Cursor c;
c = db.rawQuery("Select NOM_OFER,PREU_OFERTA,DATA_F,FOTO,PERCENTDESCOMPTE from T_OFERTA", null);
//HERE'S NOT NULL, SO I CAN PUT SOME STUFF
if (c.moveToFirst()) {
do {
for (int i = 0; i < c.getColumnCount(); i++) {
Title = c.getString((c.getColumnIndex("NOM_OFER")));
Preu = c.getColumnIndex("PREU_OFERTA");
percent = c.getString((c.getColumnIndex("PERCENTDESCOMPTE")));
data_f = c.getString((c.getColumnIndex("DATA_F")));
URLTest = c.getString((c.getColumnIndex("FOTO")));
Log.e("", "" + c.getString(i));
// initialize and set the list adapter
// Toast.makeText(getActivity(), "Title" + Title + "Preu" + Preu + "Percent" + percent + "Cheese is " + data_f, Toast.LENGTH_LONG).show();
}
mItems.add(new ListViewItem(URLTest, Title, Preu.toString(), percent, data_f));
} while (c.isAfterLast());
}
if (!c.moveToFirst()) {
Log.d("No registres","pewpew");
// HERE I WANT TO ADD IMAGEVIEW CAUSE C IS NULL THEN THE FRAGMENT WON'T BE EMPTY
}
c.close();
Try replacing
Cursor c;
c = db.rawQuery("Select NOM_OFER,PREU_OFERTA,DATA_F,FOTO,PERCENTDESCOMPTE from T_OFERTA", null);
//HERE'S NOT NULL, SO I CAN PUT SOME STUFF
if (c.moveToFirst()) {
do {
for (int i = 0; i < c.getColumnCount(); i++) {
Title = c.getString((c.getColumnIndex("NOM_OFER")));
Preu = c.getColumnIndex("PREU_OFERTA");
percent = c.getString((c.getColumnIndex("PERCENTDESCOMPTE")));
data_f = c.getString((c.getColumnIndex("DATA_F")));
URLTest = c.getString((c.getColumnIndex("FOTO")));
Log.e("", "" + c.getString(i));
// initialize and set the list adapter
// Toast.makeText(getActivity(), "Title" + Title + "Preu" + Preu + "Percent" + percent + "Cheese is " + data_f, Toast.LENGTH_LONG).show();
}
mItems.add(new ListViewItem(URLTest, Title, Preu.toString(), percent, data_f));
} while (c.isAfterLast());
}
if (!c.moveToFirst()) {
Log.d("No registres","pewpew");
// HERE I WANT TO ADD IMAGEVIEW CAUSE C IS NULL THEN THE FRAGMENT WON'T BE EMPTY
}
c.close();
To
Cursor c;
c = db.rawQuery("Select NOM_OFER,PREU_OFERTA,DATA_F,FOTO,PERCENTDESCOMPTE from T_OFERTA", null);
//HERE'S NOT NULL, SO I CAN PUT SOME STUFF
if (c.moveToFirst()) {
do {
Title = c.getString((c.getColumnIndex("NOM_OFER")));
Preu = c.getColumnIndex("PREU_OFERTA");
percent = c.getString((c.getColumnIndex("PERCENTDESCOMPTE")));
data_f = c.getString((c.getColumnIndex("DATA_F")));
URLTest = c.getString((c.getColumnIndex("FOTO")));
mItems.add(new ListViewItem(URLTest, Title, Preu.toString(), percent, data_f));
} while (c.moveToNext());
}
if (!c.moveToFirst()) {
Log.d("No registres","pewpew");
// HERE I WANT TO ADD IMAGEVIEW CAUSE C IS NULL THEN THE FRAGMENT WON'T BE EMPTY
}
c.close();
and put this -
getListView().setEmptyView(findViewById(R.id.layout_empty));
before calling setListAdapter.
Lets see, if that works.