在FragmentList中显示ArrayList

时间:2017-12-11 20:12:17

标签: java android android-fragments fragment adapter

我有以下问题:我需要显示从ListFragment中的数据库获取的数据。程序最终运行但ListFragment仍为空。 (收到的数据可以通过logD查看。) 这是我的第一个问题,所以我希望我不要错过任何东西。我非常感谢你的帮助。也很抱歉蝙蝠格式化代码有点像这样。

我的档案:

1。)主要活动(更改片段)

public void onFragmentInteraction(List<Strecke> uri) {    
    Log.d("StreckeMain", uri.toString());

    Fragment Itemfragment  = new ItemFragment();
    FragmentTransaction transactionsearch = getSupportFragmentManager().beginTransaction();
    transactionsearch.replace(R.id.content, Itemfragment);
    transactionsearch.addToBackStack(null);
    transactionsearch.commit();
}

2。)碎片

public class ItemFragment extends Fragment {    
    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    // TODO: Customize parameters
    private int mColumnCount = 1;
    private OnListFragmentInteractionListener mListener;
    private StreckeAdapter mAdapter;
    private List<Strecke> streckeList = new ArrayList<>();
    private RecyclerView recyclerView;
    private LinearLayoutManager mLinearLayoutManager;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public ItemFragment() {
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static ItemFragment newInstance(int columnCount) {
        ItemFragment fragment = new ItemFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mAdapter = new StreckeAdapter(streckeList);
        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item_list, container, false);

        mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);

        recyclerView.setLayoutManager(mLinearLayoutManager);
        recyclerView.setAdapter(mAdapter);

        return view;

        // Set the adapter
        /*if (view instanceof RecyclerView) {
            Context context = view.getContext();
            RecyclerView recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view);
            mAdapter = new StreckeAdapter(streckeList);
            if (mColumnCount <= 1) {
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
            } else {
                recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
            }
            //recyclerView.setAdapter((RecyclerView.Adapter) mListener);
            recyclerView.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
        }
        return view;*/
    }

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

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


    public interface OnListFragmentInteractionListener {
        // TODO: Update argument type and name
        void onListFragmentInteraction(DummyItem item);
    }
}

3。)适配器

 public class StreckeAdapter extends 
    RecyclerView.Adapter<StreckeAdapter.MyViewHolder> {
            public List<Strecke> streckeList;

            public class MyViewHolder extends RecyclerView.ViewHolder {
                public TextView zeitAbfahrt, Ziel, zeitZiel, tel, mail;

                public MyViewHolder(View view) {
                    super(view);
                    zeitAbfahrt = (TextView) view.findViewById(R.id.i_zeit);
                    Ziel = (TextView) view.findViewById(R.id.i_ziel);
                    zeitZiel = (TextView) view.findViewById(R.id.i_zielZeit);
                    tel = (TextView) view.findViewById(R.id.i_tel);
                    mail = (TextView) view.findViewById(R.id.i_mail);
                }
            }    

            public StreckeAdapter(List<Strecke> streckeList) {
                this.streckeList = streckeList;
                Log.d("SAdapter", "StreckeAdapter enterd ");
            }


            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.fragment_item, parent, false);

                return new MyViewHolder(itemView);
            }

            @Override
            public void onBindViewHolder(MyViewHolder holder, int position) {
                Strecke strecke = streckeList.get(position);
                holder.zeitAbfahrt.setText(strecke.getZeitAbfahrt());
                holder.Ziel.setText(strecke.getZiel());
                holder.zeitZiel.setText(strecke.getZeitZiel());
                holder.tel.setText(strecke.gettel());
                holder.mail.setText(strecke.getmail());

            }

            @Override
            public int getItemCount() {
                return streckeList.size();
            }
        }

4.。)项目的布局

    <?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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/i_zeit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="Zeit Hier"
                android:textAppearance="?attr/textAppearanceListItem" />

            <TextView
                android:id="@+id/i_ziel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="Ziel"
                android:textAppearance="?attr/textAppearanceListItem" />

            <TextView
                android:id="@+id/i_zielZeit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="Zeit Ziel"
                android:textAppearance="?attr/textAppearanceListItem" />

            <TextView
                android:id="@+id/i_tel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="Telefonnummer"
                android:textAppearance="?attr/textAppearanceListItem" />

            <TextView
                android:id="@+id/i_mail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="e-mail"
                android:textAppearance="?attr/textAppearanceListItem" />

        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

5.。布局ItemList

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

好的问题确实是Array(uri)没有传递给Fragment。我解决了这个问题,但不是很好。 刚在ActivityMain中添加了一个getter,然后在Fragment中访问它。