如何连接不在主布局中的 ListView?

How to connect a ListView that's not in the main layout?

我正在 android 工作室进行我的第一个相当大的项目,过去三天我一直在为此苦苦挣扎:

我的应用程序有一个底部导航组件,可以打开其他 3 个片段,我想知道如何在这 3 个片段之一中实现 ListView。我设法将 ListView 放在主布局中 (activity_main.xml),但是一旦我将 ListView 放入不是主布局的布局中,我就会收到 NPE。我想那是因为如果我尝试使用 findViewById(R.id.ListView) link ListView 到它的数据,它会在 activity_main 中寻找 ListView。尝试将 ListView link 到布局文件中的组件的代码写在 mainActivity.java 的 onCreate 中。我如何告诉程序在 layout_With_List.xml 中查找 ListView?

我读到另一个问题,我需要将 onCreate 中的 setContentView(R.layout.activity_main) 更改为 setContentView(R.layout.layout_With_List),但这只会产生另一个错误。

那么我怎样才能正确地将 ListView 放入另一个片段中,这样当我使用底部导航更改选项卡并使其显示数据时它不会在主布局中浮动?

我遇到的错误:

Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

这是相关代码位:

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv_productlist = findViewById(R.id.lv_productlist);

    showFoodOnListView(db_helper);
}

将数据传递给 ListView 的代码:

    private void showFoodOnListView(DB_Helper db_helper2) {
    food_ArrayAdapter = new ArrayAdapter<Product_Model>(MainActivity.this, android.R.layout.simple_list_item_1, db_helper2.getEveryone());
    lv_productlist.setAdapter(food_ArrayAdapter);
}

layout_With_List.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView 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:id="@+id/lv_productlist"
    android:name="com.ocdm.prepper.ListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="LinearLayoutManager"
    tools:context=".ListFragment"
    tools:listitem="@layout/fragment_list" />

===========================解决方案================= ==============

感谢 [https://whosebug.com/users/4039784/hayssam-soussi]

我能想出来。

        lv_productlist = (ListView) view.findViewById(R.id.R.id.lv_productlist);

        showFoodOnListView(db_helper);

我不得不将上面的代码移动到相应的布局中 java class,例如 fragment_A.java.

然后我不得不将方法 showFoodOnListView 移动到 fragment_A.java 并将上下文设置为 getActivity():.

food_ArrayAdapter = new ArrayAdapter<Product_Model>(getActivity(), android.R.layout.simple_list_item_1, db_helper2.getEveryone());

假设您需要将 ListView 添加到 片段 A

首先您必须为片段 A 创建布局(即 fragment_a.xml)

将您的 ListView 添加到 fragment_a.xml

那么您的 FragmentA.java 代码应该如下所示:

public class FragmentA extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_a, container, false);
        // get the reference of ListView
        lv_productlist = (ListView) view.findViewById(R.id.lv_productlist);

        showFoodOnListView(db_helper);

        return view;
    }
}