ListView未显示在Fragment中

时间:2016-02-14 10:26:53

标签: java android listview android-fragments

我正在构建一个自定义ListView,其中将显示一系列书籍。此ListView显示在此类的名为 ReadListFragment .Code的片段上:

public class ReadListFragment extends Fragment {
ListView list;
String[] bookISBN;
String[] bookTitle,bookImagePath;
DBadapter dBadapter;
Database db;
SQLiteDatabase sql;
ReadListAdapter readList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_readlist, container, false);
    list=(ListView)rootView.findViewById(R.id.listView);

    db = new Database(getActivity());
    sql = db.getWritableDatabase();
    dBadapter = new DBadapter(getActivity());

    List<String[]> thread = new ArrayList<>();
    bookISBN=new String[dBadapter.Size()];
    bookTitle=new String[dBadapter.Size()];
    bookImagePath=new String[dBadapter.Size()];
    bookISBN=dBadapter.getBookISBN();
    bookTitle=dBadapter.getBookTitle(bookISBN);
    bookImagePath=dBadapter.getBookImagePath(bookISBN);

    for (int i = 0; i < dBadapter.Size(); i++) {
        String[] temp = new String[3];
        temp[0] = bookISBN[i];
        Log.i("Test", bookISBN[i]);
        temp[1] = bookTitle[i];
        Log.i("Test",bookTitle[i]);
        temp[2] = bookImagePath[i];
        Log.i("Test",bookImagePath[i]);

        thread.add(temp);
    }

    readList= new ReadListAdapter(getActivity(), thread);
    list.setAdapter(readList);
    list.setVisibility(View.VISIBLE);

    return rootView;
}
}

fragment_readlist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:text="Hello world"
        android:textSize="20sp" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible" />

</LinearLayout>

ListView适配器类:

package com.example.pial_pc.instantbookreview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Pial-PC on 2/12/2016.
 */
public class ReadListAdapter extends ArrayAdapter<String[]> {
    Activity context;
    List<String[]> bookDetails;
    ImageView image1;
    TextView text1,isbn1,empty;
    Button moreButton1;
    ImageButton closeButton1;
    Bitmap bitmap1;
    BitmapDrawable bit1;


    public ReadListAdapter(Activity context, List<String[]> bookDetails) {
        super(context, R.layout.readlist_row);
        this.context=context;
        this.bookDetails = bookDetails;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View view = inflater.inflate(R.layout.readlist_row, null, false);
        empty=(TextView)view.findViewById(R.id.EmptyReadList);
        image1=(ImageView)view.findViewById(R.id.image1);

        text1=(TextView)view.findViewById(R.id.text1);

        isbn1=(TextView)view.findViewById(R.id.isbn1);

        moreButton1=(Button)view.findViewById(R.id.moreButton1);
        closeButton1=(ImageButton)view.findViewById(R.id.closeButton1);

        if(bookDetails.isEmpty()){
            empty.setText("No books are in the List.");
        }else{
            empty.setVisibility(View.GONE);
            String info[] = bookDetails.get(position);
            text1.setText(info[1]);
            Log.i("PiAL", info[1]);
            isbn1.setText(info[0]);
            Log.i("PiAL", info[0]);
            text1.setVisibility(View.VISIBLE);
            bitmap1 = BitmapFactory.decodeFile(info[2]);

            Log.i("PiAL", info[2]);
            bit1 = new BitmapDrawable(context.getResources(), bitmap1);
            image1.setVisibility(View.VISIBLE);
            image1.setBackgroundDrawable(bit1);
        }


        return view;
    }

}

这是它的行布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/closeButton1"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_marginTop="20dp"
        android:background="@mipmap/close_button" />

    <TextView
        android:id="@+id/EmptyReadList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/image1"
            android:layout_width="80dp"
            android:layout_height="100dp" />

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginRight="50dp"
            android:text="The Da Vinci Code"
            android:textSize="14sp"
            android:textStyle="bold" />


        <Button
            android:id="@+id/moreButton1"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:layout_marginTop="20dp"
            android:layout_gravity="right|end"
            android:background="#61C74F"
            android:text="More..."
            android:textAllCaps="false"
            android:textColor="#FFFFFF"
            android:textSize="12sp" />

    </LinearLayout>

    <TextView
        android:id="@+id/isbn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!--<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/image2"
            android:layout_width="130dp"
            android:layout_height="160dp" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="The Da Vinci Code"
            android:textSize="14sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/isbn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/moreButton2"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:layout_marginTop="10dp"
            android:background="#61C74F"
            android:text="More..."
            android:textAllCaps="false"
            android:textColor="#FFFFFF"
            android:textSize="12sp" />
    </LinearLayout>

    <ImageButton
        android:id="@+id/closeButton2"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_marginTop="20dp"
        android:background="@mipmap/close_button" />-->

</LinearLayout>

我使用 Log 测试了值,所有数据都出现在Adapter类中。但我不明白为什么我的listView没有显示。

我的代码有问题吗?

LogCat结果

02-14 17:21:43.940    9229-9229/com.example.pial_pc.instantbookreview I/Test﹕ 0307277674
02-14 17:21:43.940    9229-9229/com.example.pial_pc.instantbookreview I/Test﹕ The Da Vinci Code
02-14 17:21:43.940    9229-9229/com.example.pial_pc.instantbookreview I/Test﹕ /storage/emulated/0/ReadList_books_cover/The_Da_Vinci_Code_Dan_Brown_0307277674.jpg

for Adapter class我试图查看列表是否为空。

1 个答案:

答案 0 :(得分:1)

您的logcat返回

Math.round

图片路径有额外的空格。哪个无效。

<强>不

    02-14 16:12:08.239  29049-29049/com.example.pial_pc.instantbookreview I/Test﹕      1936305623
02-14 16:12:08.239  29049-29049/com.example.pial_pc.instantbookreview I/Test﹕ Gabriel's Inferno
02-14 16:12:08.239  29049-29049/com.example.pial_pc.instantbookreview I/Test﹕ /storage/emulated/0/ReadList_books_cover/Gabriel's Inferno_Sylvain Reynard_1936305623.jpg
02-14 16:12:08.240  29049-29049/com.example.pial_pc.instantbookreview I/Test﹕ 0552151696
02-14 16:12:08.240  29049-29049/com.example.pial_pc.instantbookreview I/Test﹕ Digital Fortress
02-14 16:12:08.240  29049-29049/com.example.pial_pc.instantbookreview I/Test﹕ /storage/emulated/0/ReadList_books_cover/Digital Fortress_Dan Brown_0552151696.jpg
02-14 16:12:08.241  29049-29049/com.example.pial_pc.instantbookreview I/Test﹕ 1416524797

<强>正确

Digital Fortress_Dan Brown_0552151696.jpg