0

I am trying to make a listview using this example. But the problem is that listview is not showing at all. I am trying to show listview in a Fragment. The listview showing code is below: BookListFragment.java

public class BookListFragment extends Fragment {
private String[] booktitle;
private String[] call_no;
private String[] due_date;
private String[] renewal;
ArrayList<String[]> bookDetails;
ListView bookList;
BookdetailsAdapter bookdetailsAdapter;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_booklist,
            container, false);

    bookList = (ListView) view.findViewById(R.id.BooklistView);


    DBadapter dBadapter = new DBadapter(getActivity().getApplicationContext());
    booktitle = dBadapter.getBookName();
    call_no = dBadapter.getCallNo();
    due_date = dBadapter.getDue();
    renewal = dBadapter.getRenew();

    bookDetails = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        String[] temp = new String[4];
        temp[0] = booktitle[i];
        temp[1] = call_no[i];
        temp[2] = due_date[i];
        temp[3] = renewal[i];

        bookDetails.add(temp);
    }

    bookdetailsAdapter = new BookdetailsAdapter(getActivity(), bookDetails);

    bookList.setAdapter(bookdetailsAdapter);

    return view;
}}

And the Adapter for showing listview is BookdetailsAdapter.java

public class BookdetailsAdapter extends ArrayAdapter<String> {
Activity context;
ArrayList<String[]> book;

public BookdetailsAdapter(Activity context, ArrayList<String[]> book) {
    super(context, R.layout.booklistview);
    this.context = context;
    this.book = book;
}

static class ViewHolderItem {
    TextView bookTitle, call_no, due, renewal;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolderItem viewHolder;
    if (convertView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.booklistview, null, true);
        viewHolder = new ViewHolderItem();
        viewHolder.bookTitle = (TextView) convertView.findViewById(R.id.BookTitle);
        viewHolder.call_no = (TextView) convertView.findViewById(R.id.Call_no);
        viewHolder.due = (TextView) convertView.findViewById(R.id.Due_Date);
        viewHolder.renewal = (TextView) convertView.findViewById(R.id.Remaining_renewal);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolderItem) convertView.getTag();
    }

    String bookInfo[] = book.get(position);
    viewHolder.bookTitle.setText("Pial");
    viewHolder.call_no.setText("dada");
    viewHolder.due.setText("dadada");
    viewHolder.renewal.setText("dadadada");

    return convertView;
}}

Both layout files are here fragment_booklist.xml and booklistview.xml

How can i show this listview?

Pial Kanti
  • 1,550
  • 2
  • 13
  • 26
  • try with giving static height of list view and check that array list has data, you may find help using https://www.tutorialspoint.com/android/android_list_fragment.htm and http://stackoverflow.com/questions/22512833/create-listview-in-fragment-android – AmeeJoshi Oct 20 '16 at 14:39
  • Possible duplicate of [custom adapter isn't showing any items](http://stackoverflow.com/questions/29829252/custom-adapter-isnt-showing-any-items) – Mike M. Oct 20 '16 at 14:43

2 Answers2

1

Either pass your data to the super Adapter class, or override getCount() method of adapter and give it the number of books.

Your adapter is not providing the information about how many items it has.

elmorabea
  • 3,243
  • 1
  • 14
  • 20
  • Thanks, overriding getCount() method solves the problem. I actually don't know about this. Is this method has to be override for any kind of listview adapter? – Pial Kanti Oct 20 '16 at 14:54
  • Yes, basically all the adapters you use on Android needs to provide the count of their underlying dataset – elmorabea Oct 20 '16 at 14:57
0

The xmls look great.

Maybe put

bookList = (ListView) getView().findViewById(R.id.BooklistView);


DBadapter dBadapter = new DBadapter(getActivity().getApplicationContext());
booktitle = dBadapter.getBookName();
call_no = dBadapter.getCallNo();
due_date = dBadapter.getDue();
renewal = dBadapter.getRenew();

bookDetails = new ArrayList<>();
for (int i = 0; i < 3; i++) {
    String[] temp = new String[4];
    temp[0] = booktitle[i];
    temp[1] = call_no[i];
    temp[2] = due_date[i];
    temp[3] = renewal[i];

    bookDetails.add(temp);
}

bookdetailsAdapter = new BookdetailsAdapter(getActivity(), bookDetails);

bookList.setAdapter(bookdetailsAdapter);

into fragment's onViewCreated

cherry-wave
  • 731
  • 2
  • 6
  • 21