gpt4 book ai didi

java - listview 在 fragment 中看不到 propaly

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:52 25 4
gpt4 key购买 nike

我的导航栏中有一个 fragment ,其中有一个 ListView ,其中应包含可供选择的项目,但运行应用程序时我会看到“思考屏幕”

这里是FragmentClub.java

public class FragmentClubs extends ListFragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

ListView listView;
String[] names;
final String LOG_TAG = "myLogs";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public FragmentClubs() {
// Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static FragmentClubs newInstance(String param1, String param2) {
FragmentClubs fragment = new FragmentClubs();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;}@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public void onDetach () {
super.onDetach();
mListener = null;
}



@Override
public void onActivityCreated(Bundle savedInstanceState) {
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
super.onCreate(savedInstanceState);
listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {


@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.list:
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.array.typesoffood, menu);
return true;
}


@Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}


public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

});

}

这里是 FragmentClubs.xml

<RelativeLayout 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"
tools:context="com.example.vlad.navig2.fragments.FragmentClubs"
android:weightSum="1"
android:clickable="true"
android:contextClickable="true"
android:nestedScrollingEnabled="true">

<!-- TODO: Update blank fragment layout -->

<TextView
android:layout_width="wrap_content"
android:layout_height="72dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/texttoeat"
android:id="@+id/textView2"
android:layout_toStartOf="@+id/button2"
android:textSize="20sp"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2"
android:layout_alignParentTop="true"
android:elegantTextHeight="false"
android:textAlignment="center"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"
android:id="@+id/button2"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="54dp"
android:layout_marginEnd="54dp"
android:layout_marginBottom="77dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_alignTop="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Введіть..." />

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
android:layout_above="@+id/button2"
android:layout_toRightOf="@+id/editText3"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2"
android:layout_below="@+id/textView2" />

这是我在运行时得到的图像 That is而我 eatmenu.xml eatmenu

最佳答案

首先 - 请参阅您在 'onActivityCreated() 中调用的 super.onCreate(savedInstanceState);' 的 Fragment 生命周期,该周期恰好发生在 onCreateViewonCreate() 方法之后,这会导致您的应用陷入无限循环,并且您没有在必须在onCreateView()中完成的 fragment 中膨胀布局,这会导致您的 fragment 显示默认的空 ListView ,而不是您想要的

关于java - listview 在 fragment 中看不到 propaly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36436162/

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