pial kanti pial kanti - 1 month ago 5
Android Question

ListView is not showing inside a Fragment

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?

Answer

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.

Comments