在我的项目中,我的Fragment
是ListFragment
。到目前为止,我所做的是使用来自SQLite
数据库的数据填充List;但现在,我正面临着这个问题:
如果我的数据库中没有数据,我的ListFragment
将为空;这就是为什么我想用一张图片来代替列表。
现在,我仍然坚持如何在onCreateView
中使用ListFragment
。
我发现的一个替代方案是:“使用Fragment
代替ListFragment
”
有没有其他方法可以做到这一点,而没有用ListFragment
替换Fragment
?
这属于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();
这就是我所面临的错误:
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)
我用光标做了一些更改。我删除了SQLite
上的一些数据,以测试它是否有效;但现在,它没有显示任何内容。
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();
答案 0 :(得分:1)
尝试替换
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();
要
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();
并把它 -
getListView().setEmptyView(findViewById(R.id.layout_empty));
在调用setListAdapter之前。
让我们看看,如果可行的话。
答案 1 :(得分:0)
如果我理解正确 - 你可以让你的布局看起来像这样。如果listview为空,则具有此ID的ImageView将显示在页面的中心
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:dividerHeight="1px"
android:paddingLeft="3dp"
android:paddingRight="3dp">
</ListView>
<ImageView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<!-- Maybe some your stuff here -->
</LinearLayout>
答案 2 :(得分:0)
嗨如果扩展列表片段并希望在列表视图为空时设置图像视图。 android提供set empty view方法,用于在列表视图没有数据时显示视图。请参阅下面的如何设置空视图。
下面是我的列表片段的布局布局内容图像视图的空布局,当列表视图没有要显示的内容时将显示。实际上它总是隐藏在列表视图后面。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"/>
<include layout="@android:layout/empty_content" />
</LinearLayout>
empty_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_app_logo" />
</LinearLayout>
在java代码中打开列表片段,在片段中添加以下行。
getListView().setEmptyView(findViewById(R.id.layout_empty));