包含ListView不会填充

时间:2016-08-02 23:41:43

标签: java android xml listview

我正在尝试在另一个视图中包含列表片段,但列表似乎没有填充。 Android Studio中的“设计”视图显示包含listView,但是在构建项目时,我看到的只是未填充的主要活动。

我希望有人能告诉我为什么会这样。

ListFragment类:

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();

    }
}

自定义适配器类:

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>

1 个答案:

答案 0 :(得分:3)

ListActivityFragment 应该是这样的(添加onCreateView(),覆盖onActivityCreated()而不是onCreate(),并设置项目点击监听器):

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'.
    }
}