如何在片段内创建自定义列表视图

Posted

技术标签:

【中文标题】如何在片段内创建自定义列表视图【英文标题】:how do i create a custom listview inside a fragment 【发布时间】:2020-05-24 17:57:35 【问题描述】:

我目前想创建一个自定义列表视图,以放置在我创建的片段之一中,该片段目前是我的 Homefragment.java 文件,如下所示:

package sg.edu.rp.c346.a3pdwork;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


/**
 * A simple @link Fragment subclass.
 */
public class HomeFragment extends Fragment 


    public HomeFragment() 
        // Required empty public constructor
    


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        String[] menuItems = "Bob Contact: 99999999 ", "Alan Contact:  88888888", "Ryan Contact:   77777777";

        ListView listView = (ListView) view.findViewById(R.id.listViewDetails);

        ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
                getActivity(),
                android.R.layout.simple_list_item_1,
                menuItems
        );
        listView.setAdapter(listViewAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                if (position == 0) 
                    Toast.makeText(getActivity(),"Hi! You CLikced the first item", Toast.LENGTH_LONG).show();
                else if (position == 1) 
                    Toast.makeText(getActivity(),"Hi! You CLikced the Second item", Toast.LENGTH_LONG).show();
                else if (position == 2) 
                    Toast.makeText(getActivity(),"Hi! You CLikced the Third item", Toast.LENGTH_LONG).show();

                

            
        );

        // Inflate the layout for this fragment
        return view;
    


但我目前对是否正确编写它感到困惑,因为我不确定是否应该将自定义列表视图写入我的 HomeFragment.java 文件,还是应该将其写入我的 MainActivity.java 文件中?因为之前我尝试将自定义列表视图放入我的 MainActivity.java 文件时遇到了一个错误,如下所示,我不太明白。

 --------- beginning of crash
2020-02-09 11:10:56.883 14328-14328/sg.edu.rp.c346.a3pdwork E/AndroidRuntime: FATAL EXCEPTION: main
    Process: sg.edu.rp.c346.a3pdwork, PID: 14328
    java.lang.RuntimeException: Unable to start activity ComponentInfosg.edu.rp.c346.a3pdwork/sg.edu.rp.c346.a3pdwork.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at sg.edu.rp.c346.a3pdwork.MainActivity.onCreate(MainActivity.java:55)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

【问题讨论】:

显示列表视图所在位置的 xml,并显示您在 homefragment 中创建自定义列表视图时遇到的任何错误。 @HaroldSer 对不起,我对你的陈述感到困惑 【参考方案1】:

您正在通过 null ogject(即列表视图)调用 setAdapter()。我认为您的片段布局文件fragment_home.xml 中没有listViewDetails ListView。所以试着把你的列表视图放在你的布局文件中。

【讨论】:

您好抱歉问,但我已经创建了一个列表视图,这是我的 fragment_home.xml 文件,如下所示: ```schemas.android.com/apk/res/android" xmlns:tools="schemas.android.com/tools" android: layout_ android:layout_ android:orientation="vertical" tools:context=".HomeFragment"> 但我错过了其他地方吗?抱歉,因为我对此还是很陌生

以上是关于如何在片段内创建自定义列表视图的主要内容,如果未能解决你的问题,请参考以下文章

片段内 Listview 中动态视图的 onClick 侦听器

如何从使用视图寻呼机创建的选项卡片段转移到另一个片段

Android:在片段内膨胀自定义视图

如何从自定义列表视图中获取选定项目并在 toast 消息中打印?

自定义对话框片段内的进度条 - 如何从 AsyncTask 传递进度?

在片段中创建自定义列表视图时出错。必需的活动,找到的片段