gpt4 book ai didi

java - 包含的 ListView 不会填充

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:03 28 4
gpt4 key购买 nike

我试图在另一个 View 中包含一个列表 fragment ,但该列表似乎没有被填充。 Android Studio 中的设计 View 显示包含了 listView,但是在构建项目时,我看到的只是一个未填充的 Main Activity。

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

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;
}
}

主要 Activity 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>

最佳答案

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

关于java - 包含的 ListView 不会填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38732151/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com