JDev
JDev

Reputation: 2551

Android - ListView in Fragment does not show

Tell me what I did wrong?

I'm trying to fill the sheet. But the data does not appear in ListView.

For example, in Activity it is working. It is this code not tried it, but copied from where it worked perfectly.

The data is accurate, checked debugger.

public class TabFilter extends Fragment {

    private static String TAB_FILTER_LOG = "TabFilter: ";
    private ArrayList<HashMap<String, Object>> filterListData;
    private ListView filterListView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);

        View view = inflater.inflate(R.layout.tab_filter, container, false);

        filterListView = (ListView) view.findViewById(R.id.filtersListView);
        filterListData = new ArrayList<HashMap<String, Object>>();

        fillListView();

        SimpleAdapter filtersListAdapter = new SimpleAdapter(getActivity().getBaseContext(), filterListData, R.layout.filters_list_view_row, new String[] { "name",
                "nummer" }, new int[] { R.id.filterNameTv, R.id.filterInfoTv });

        filterListView.setAdapter(filtersListAdapter);
        filterListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        // filterListView.setOnItemClickListener(this);

        if (container == null) {
            return null;
        }
        return (LinearLayout) inflater.inflate(R.layout.tab_filter, container, false);
    }

    private HashMap<String, Object> fillListView() {
        HashMap<String, Object> fillMap = null;

        SQLiteDatabase db = DatabaseHelper.getInstance(getActivity().getBaseContext()).getWritableDatabase();
        Cursor cursor = db.query("filters", null, null, null, null, null, null);

        if (cursor.moveToFirst()) {

            int nameColIndex = cursor.getColumnIndex("name");
            int nummerColIndex = cursor.getColumnIndex("nummer");

            do {
                fillMap = new HashMap<String, Object>();

                fillMap.put("name", cursor.getString(nameColIndex));
                fillMap.put("nummer", cursor.getString(nummerColIndex));
                filterListData.add(fillMap);

            } while (cursor.moveToNext());

            cursor.close();
            db.close();
        } else {
            Toast.makeText(getActivity().getBaseContext(), "0 ROWS: ", Toast.LENGTH_LONG).show();
        }
        return fillMap;
    }
}

tab_filter.xm

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

    <ListView
        android:id="@+id/filtersListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

filters_list_view_row.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" 
    android:background="@color/BlueViolet">

    <TextView
        android:id="@+id/filterNameTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:gravity="center"
        android:background="@color/LightGrey"/>

    <TextView
        android:id="@+id/filterInfoTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/LightSkyBlue" />

</LinearLayout>

Upvotes: 0

Views: 4416

Answers (3)

sdqwerty
sdqwerty

Reputation: 1

I had equivalent problem of fragments do not appear. And a problem was the order of XML statements, so orientation SHOULD appear before "layouts":

<?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:background="@color/BlueViolet">
...

Upvotes: 0

Dixit Patel
Dixit Patel

Reputation: 3192

You inflating another LinearLayout and returning it. So you are getting empty screen Use this Method instead of Above

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setHasOptionsMenu(true);

    View view = inflater.inflate(R.layout.tab_filter, container, false);

    filterListView = (ListView) view.findViewById(R.id.filtersListView);
    filterListData = new ArrayList<HashMap<String, Object>>();

    fillListView();

    SimpleAdapter filtersListAdapter = new SimpleAdapter(getActivity().getBaseContext(), filterListData, R.layout.filters_list_view_row, new String[] { "name",
                 "nummer" }, new int[] { R.id.filterNameTv, R.id.filterInfoTv });

    filterListView.setAdapter(filtersListAdapter);
    filterListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    // filterListView.setOnItemClickListener(this);

    if(container == null) {
        return null;
    }
    return view;
}

Upvotes: 2

TNR
TNR

Reputation: 5869

You need to return the view object in onCreateView(). Please check your onCreateView() where you are creating View object in the first line and creating listview object with respected to the first view Object. Finally you are inflating another LinearLayout and returning it. So you are getting empty screen.

Replace the below code of your:

if(container == null) {
    return null;
}
return (LinearLayout)inflater.inflate(R.layout.tab_filter, container, false);

with this:

return view;

Upvotes: 4

Related Questions