Listview 项未显示在 Fragment Android 中 [英] Listview item not Display in the Fragment Android

查看:55
本文介绍了Listview 项未显示在 Fragment Android 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带 Fragment 的导航抽屉.我想在导航抽屉上打开 Fragment.代码中没有任何错误,但片段中不显示 Listview 项.只显示空白页.遵循代码.请帮帮我.

I have Navigation Drawer with Fragment. I want to Open Fragment on Navigation Drawer. No any Error comes in code but Listview Item Not Display in Fragment. Only Blank Page Display. Following Code. Plzz Help Me.

fragment_blank_fragment_trip2.xml

fragment_blank_fragment_trip2.xml

<LinearLayout 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:orientation="vertical"
tools:context="com.example.administrator.vtsapplication.BlankFragmentTrip2">

<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

Activity_listview.xml

Activity_listview.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >

BlankFragmenttrip2.java

BlankFragmenttrip2.java

package com.example.administrator.vtsapplication;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class BlankFragmentTrip2 extends Fragment {
public BlankFragmentTrip2() {
    // Required empty public constructor
}
ViewGroup vg;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X"};
    vg = (ViewGroup) inflater.inflate(R.layout.fragment_blank_fragment_trip2, container, false);
    ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.activity_listview, mobileArray);

    ListView listView = (ListView) vg.findViewById(R.id.mobile_list);
    listView.setAdapter(adapter);
    return inflater.inflate(R.layout.fragment_blank_fragment_trip2, container, false);
}

}

推荐答案

你应该使用你的 Fragment 作为这个.您没有返回您的 View.

You should use your Fragment as this. You are not returning your View.

public class BlankFragmentTrip2 extends Fragment {

    String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X"};

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_blank_fragment_trip2, container, false);
        ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.activity_listview, mobileArray);

        ListView listView = (ListView) rootView.findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);
        return rootView;
    }

}

这篇关于Listview 项未显示在 Fragment Android 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆