走在路上的Echo 2017-10-08 17:08 采纳率: 100%
浏览 926
已采纳

麻烦各位大神帮帮忙!!!Fragment的ListView不能显示

 public class HistoryFragment extends Fragment {
    // 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";

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

    ListView listView;

    private OnFragmentInteractionListener mListener;

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

    public static HistoryFragment newInstance(String param1, String param2) {
        HistoryFragment fragment = new HistoryFragment();
        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 View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.fragment_history, container, false);

        String[] locations = {"广州市番禺区1", "广州市天河区2", "广州市海珠区3",
                "广州市越秀区4", "广州市南沙区5", "广州市佛山区6", "广州市番禺区7", "广州市天河区8",
                "广州市佛山区9", "广州市海珠区0", "广州市越秀区11", "广州市南沙区12",
                "广州市佛山区13", "广州市番禺区14", "广州市天河区15", "广州市海珠区16",
                "广州市越秀区17", "广州市南沙区18", "广州市中山大道19"};
        List<Map<String, Object>> items = new ArrayList<Map<String,Object>>();
        View view = inflater.inflate(R.layout.fragment_history,null);
        listView = view.findViewById(R.id.history_listview);

        Map<String, Object> item = new HashMap<String, Object>();
        for(int i=0;i < locations.length;i++) {
            item.put("location", locations[i]);
            items.add(item);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(),items,
                R.layout.history_list_item,new String[]{"locations"},new int[]{R.id.history_location});
        listView.setAdapter(simpleAdapter);
//        listView.setAdapter(new ArrayAdapter<>(view.getContext(), R.layout.history_list_item,locations));
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

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

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

----------------------------------------------------------------------------
public class MainActivity extends AppCompatActivity implements MapFragment.OnFragmentInteractionListener,
        HistoryFragment.OnFragmentInteractionListener,SettingsFragment.OnFragmentInteractionListener{

//    private TextView mTextMessage;


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Fragment fragment;
            switch (item.getItemId()) {
                case R.id.navigation_map:
//                    mTextMessage.setText(R.string.title_map);
                    fragment = MapFragment.newInstance("","");
                    fragmentTransaction.replace(R.id.content,fragment);
                    fragmentTransaction.commit();
                    return true;
                case R.id.navigation_history:
                    fragment = HistoryFragment.newInstance("","");
                    fragmentTransaction.replace(R.id.content,fragment);
                    fragmentTransaction.commit();
//                    mTextMessage.setText(R.string.title_history);
                    return true;
                case R.id.navigation_settings:
                    fragment = SettingsFragment.newInstance("","");
                    fragmentTransaction.replace(R.id.content,fragment);
                    fragmentTransaction.commit();
//                    mTextMessage.setText(R.string.title_settings);
                    return true;
            }
            return false;
        }

    };

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

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment;
        fragment = MapFragment.newInstance("","");
        fragmentTransaction.replace(R.id.content,fragment);
        fragmentTransaction.commit();

//        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setItemIconTintList(null);
//        navigation.setSelectedItemId(R.id.navigation_map);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}


运行结果:
图片说明

Fragment 的layout文件

 <FrameLayout 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.hp.fragment.HistoryFragment">

    <!-- TODO: Update blank fragment layout -->
    <!--<TextView-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent"-->
        <!--android:text="@string/hello_history_fragment" />-->
    <ListView
        android:id="@+id/history_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</FrameLayout>

list item layout文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/history_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

main activity layout 文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <!--<TextView-->
            <!--android:id="@+id/message"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_marginBottom="@dimen/activity_vertical_margin"-->
            <!--android:layout_marginLeft="@dimen/activity_horizontal_margin"-->
            <!--android:layout_marginRight="@dimen/activity_horizontal_margin"-->
            <!--android:layout_marginTop="@dimen/activity_vertical_margin"-->
            <!--android:text="@string/title_map" />-->

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />

</LinearLayout>
  • 写回答

2条回答 默认 最新

  • huangjingfeng2016 2017-10-16 15:35
    关注
        Map<String, Object> item = new HashMap<String, Object>();
        for(int i=0;i < locations.length;i++) {
            item.put("location", locations[i]);
            items.add(item);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(),items,
                R.layout.history_list_item,new String[]{"locations"},new int[]{R.id.history_location});
    
                                item添加时键值是location,给adapter指定的键值是locations,写错了
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多尺度特征图提取和融合
  • ¥15 微信小程序:渲染收货地址时页面不显示
  • ¥20 win7 64位DirectShow提示初始化失败如何解决?
  • ¥20 小规模孤立词识别系统设计
  • ¥15 关于Java对接海康威视车牌识别一体机SDK是否需要固定外网的IP?
  • ¥15 Linux扩容时,格式化卡住了:vgdispaly查看卷组信息,没有输出
  • ¥18 关于#ubuntu#的问题:使用背景-工作职责内有七八台ubuntu系统的电脑,平时需要互相调取资料,想实现把这几台电脑用交换机组成一个局域网,来实现指定文件夹的互相调取和写入
  • ¥20 求一个简易射频信号综测仪
  • ¥15 esp8266 tally灯 接收端改为发射端
  • ¥30 Labview代码调用access 数据库,相同代码其中一个调用不出来是为什么