可以在 ListFragment 上以编程方式添加 ImageView 吗?

It's possible to add ImageView programatically on ListFragment?

在我的项目中,我有一个 FragmentListFragment。到目前为止,我所做的是用 SQLite 数据库中的数据填充列表;但是现在,我面临着这个问题:

如果数据库中没有数据,我的ListFragment将是空的;这就是为什么我想用图片代替列表的原因。

现在,我卡在如何在 ListFragment 中使用 onCreateView

我发现的另一种选择是:"using Fragment instead of ListFragment"

有没有其他方法可以做到这一点,而不用 Fragment 替换 ListFragment

编辑 1

这在 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.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.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.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor.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)

编辑 2

我用光标做了一些修改。我删除了 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();

如果我理解正确的话 - 你可以让你的布局看起来像这样。如果 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>

您好如果您扩展了列表片段并希望在列表视图为空时设置图像视图。 android提供设置空视图的方法,用于列表视图没有数据时显示视图available.see下面如何设置空视图。

以下是我的列表片段的布局布局内容当列表视图没有内容可显示时将显示的图像视图的空布局。实际上它总是隐藏在列表视图后面。

<?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));

尝试替换

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 之前。

让我们看看,是否有效。