在内部片段类中使用ListView

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在内部片段类中使用ListView相关的知识,希望对你有一定的参考价值。

我正在尝试将一个arrayadapter设置为来自firebase的listview,但它不起作用。我正在使用一个片段,它告诉我“v”AKA我的视图是从内部类中访问的,需要声明为final。我已经尝试将“final”放在行的开头,但它不起作用。我是android Studio的新手,非常感谢您的帮助!

这是我的代码:

public class Mandag extends Fragment 

private ListView listView;

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) 
    View v = inflater.inflate(mandag, container, false);


    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("Skular");

    final SharedPreferences skuule = getActivity().getSharedPreferences("skule", Context.MODE_PRIVATE);
    String Skule = skuule.getString("skule", "");

    final SharedPreferences klaasse = getActivity().getSharedPreferences("klasse", Context.MODE_PRIVATE);
    String Klasse = klaasse.getString("klasse", "");




    myRef.child(Skule).child(Klasse).child("Fag").child("Mandag").addListenerForSingleValueEvent(new ValueEventListener() 
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) 
            final List<String> timar = new ArrayList<String>();

            for (DataSnapshot areaSnapshot : dataSnapshot.getChildren()) 
                String skuleName = areaSnapshot.getValue(String.class);
                timar.add(skuleName);
            

            ArrayAdapter<String> skularAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.activity_list_item, timar);

            final ListView = (ListView) v.findViewById(R.id.mList);
            listView.setAdapter(skularAdapter);
        

        @Override
        public void onCancelled(DatabaseError databaseError) 
        
    );

    return v;

我的数据库:

"Skular" : 
"Kvåle_Skule" : 
  "9A" : 
    "Fag" : 
      "Mandag" : 
        "Fag1" : "Engelsk",
        "Fag2" : "Engelsk",
        "Fag3" : "Matte",
        "Fag4" : "Kosetime:)",
        "Fag5" : "KRLE",
        "Fag6" : "Naturfag"
      
    ,
    "Lekser" : 
      "Naturfag" : "Les side 31 - 34!",
      "Norsk" : "Gjer nåke",
      "Samfunsfag" : "Sjå Ein Debatt 8 ganger"
    
  ,
  "9B" : 
    "Fag" : 
      "Mandag" : 
        "Fag1" : "Norsk",
        "Fag2" : "Norsk",
        "Fag3" : "Matte",
        "Fag4" : "Matte",
        "Fag5" : "Gym",
        "Fag6" : "Gym"
      
    ,
    "Lekser" : 
      "Naturfag" : "Les side 31 - 34!",
      "Norsk" : "Gjer nåke",
      "Samfunsfag" : "Sjå Ein Debatt 2 ganger"
    
  ,
  "9C" : 
    "Fag" : 
      "Mandag" : 
        "Fag1" : "Norsk",
        "Fag2" : "Engelsk",
        "Fag3" : "Matte",
        "Fag4" : "Samfunsfag",
        "Fag5" : "KRLE",
        "Fag6" : "Naturfag"
      
    ,
    "Lekser" : 
      "Naturfag" : "Les side 31 - 34!",
      "Norsk" : "Gjer nåke",
      "Samfunsfag" : "Sjå Ein Debatt"
    
  ,
  "Picker1" : [ "8", "9", "10" ],
  "Picker2" : [ "A", "B", "C" ]

,
,

谢谢你能提供帮助!

编辑:我的错误:

    10-02 21:45:05.714 5935-5935/com.heli.minvekeplan E/ArrayAdapter: You must supply a resource ID for a TextView
10-02 21:45:05.714 5935-5935/com.heli.minvekeplan D/AndroidRuntime: Shutting down VM
 10-02 21:45:05.717 5935-5935/com.heli.minvekeplan E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: com.heli.minvekeplan, PID: 5935
                                                                java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
                                                                    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
                                                                    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
                                                                    at android.widget.AbsListView.obtainView(AbsListView.java:2347)
                                                                    at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
                                                                    at android.widget.ListView.onMeasure(ListView.java:1182)
                                                                    at android.view.View.measure(View.java:17637)
                                                                    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
                                                                    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
                                                                    at android.view.View.measure(View.java:17637)
                                                                    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1629)
                                                                    at android.view.View.measure(View.java:17637)
                                                                    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
                                                                    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
                                                                    at android.view.View.measure(View.java:17637)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                    at android.view.View.measure(View.java:17637)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
                                                                    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                    at android.view.View.measure(View.java:17637)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                    at android.view.View.measure(View.java:17637)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
                                                                    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                    at android.view.View.measure(View.java:17637)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2618)
                                                                    at android.view.View.measure(View.java:17637)
                                                                    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2019)
                                                                    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1177)
                                                                    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1383)
                                                                    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1065)
                                                                    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5901)
                                                                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                                    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                                                                    at android.view.Choreographer.doFrame(Choreographer.java:550)
                                                                    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                    at android.os.Looper.loop(Looper.java:211)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5389)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
                                                                 Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
                                                                    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
                                                                    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
                                                                    at android.widget.AbsListView.obtainView(AbsListView.java:2347) 
                                                                    at android.widget.ListView.measureHeightOfChildren(ListView.java:1270) 
                                                                    at android.widget.ListView.onMeasure(ListView.java:1182) 
                                                                    at android.view.View.measure(View.java:17637) 
                                                                    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728) 
                                                                    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464) 
                                                                    at android.view.View.measure(View.java:17637) 
                                                                    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1629) 
                                                                    at android.view.View.measure(View.java:17637) 
                                                                    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728) 
                                                                    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464) 
                                                                    at android.view.View.measure(View.java:17637) 
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536) 
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) 
                                                                    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) 
                                                                    at android.view.View.measure(View.java:17637) 
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536) 
                                                                    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 
                                                                    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) 
                                                                    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) 
                                                                    at android.view.View.measure(View.java:17637) 
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536) 
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) 
                                                                    at android.view.View.measure(View.java:17637) 
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536) 
                                                                    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 
                                                                    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) 
                                                                    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) 
                                                                    at android.view.View.measure(View.java:17637) 
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536) 
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) 
                                                                    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2618) 
                                                                    at android.view.View.measure(View.java:17637) 
                                                                    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2019) 
                                                                    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1177) 
                                                                    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1383) 
                                                                    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1065) 
                                                                    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5901) 
                                                                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 
                                                                    at android.view.Choreographer.doCallbacks(Choreographer.java:580) 
                                                                    at android.view.Choreographer.doFrame(Choreographer.java:550) 
                                                                    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 
                                                                    at android.os.Handler.handleCallback(Handler.java:739) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                    at android.os.Looper.loop(Looper.java:211) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5389) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 
答案

您在行中的匿名类中使用变量v

final ListView = (ListView) v.findViewById(R.id.mList);

另外,我没有看到你的ListView变量的名称,它应该是这样的:

final ListView listView = (ListView) v.findViewById(R.id.mList);

将该行移到匿名类之外,例如在此行下面:

String Klasse = klaasse.getString("klasse", "");

并保留最终,就像你拥有它或你可以使你的View决赛如下:

final View v = inflater.inflate(mandag, container, false);

以上是关于在内部片段类中使用ListView的主要内容,如果未能解决你的问题,请参考以下文章

从片段类中的对话框添加项目到recyclerview。

片段的视图模型而不是访问活动视图模型?

为啥流行的框架在内部使用字节码操作?

SqlDataAdapter 如何在内部工作?

为啥 ArrayList 在内部使用 Object[](而不是 E[])? [复制]

如何在内部函数中使用类变量?