简体   繁体   中英

Included ListView won't Populate

I am trying to include a List Fragment in another View, but the list doesn't appear to be populating. The Design view in Android Studio shows that the listView is included, but when the project is built, all I see is an unpopulated Main Activity.

I'm hoping someone can tell me why this is the case.

ListFragment class:

package com.starlineeducation.starlinemobile;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class ListActivityFragment extends ListFragment {

    static final String[] CLASSES =
            new String[] { "Government", "Hawaiian History", "Economics", "Psychology"};


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ClassAdapter(getActivity(), CLASSES));
    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(getActivity(), selectedValue, Toast.LENGTH_SHORT).show();

    }
}

Custom Adapter class:

package com.starlineeducation.starlinemobile;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ClassAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public ClassAdapter(Context context, String[] values) {
        super(context, R.layout.listitem_classes, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.listitem_classes, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(values[position]);

        // Change icon based on name
        String s = values[position];

        if (s.equals("Psychology")) {
            imageView.setImageResource(R.drawable.ic_psychology);
        } else if (s.equals("Government")) {
            imageView.setImageResource(R.drawable.ic_government);
        } else if (s.equals("Hawaiian History")) {
            imageView.setImageResource(R.drawable.ic_hawaiian_history);
        } else if (s.equals("Economics")){
            imageView.setImageResource(R.drawable.ic_economics);
        }

        return rowView;
    }
}

MainActivity XML:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.starlineeducation.starlinemobile.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/listview_classes" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_new_class" />

</android.support.design.widget.CoordinatorLayout>

ListView XML (listview_classes.xml):

 <?xml version="1.0" encoding="utf-8"?>
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ListActivityFragment"
    tools:listitem="@layout/listitem_classes"
    tools:showIn="@layout/activity_main">

</ListView>

Your ListActivityFragment should be like this (adding onCreateView() , overriding onActivityCreated() instead of onCreate() , and setting the item click listener) :

public class ListActivityFragment extends ListFragment implements OnItemClickListener{

    static final String[] CLASSES = { "Government", "Hawaiian History", "Economics", "Psychology"};
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_layout, container, false);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setListAdapter(new ClassAdapter(getActivity(), CLASSES));
        getListView().setOnItemClickListener(this);

    }

     @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // do dome thing with 'position'.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM