走在路上的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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器