【发布时间】:2017-12-11 20:12:16
【问题描述】:
我有以下问题:我需要在 ListFragment 中显示从数据库中获取的数据。程序终于运行了,但 ListFragment 仍然是空的。 (收到的数据可以通过 logD 看到。) 这是我在这里的第一个问题,所以我希望我不会错过任何东西。我会很感激帮助。也很抱歉代码的 bat 格式有点苦恼。
我的文件:
1.) Main Activity(改变Fragment)
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.) 布局项列表
<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>
【问题讨论】:
-
在
onFragmentInteraction():除了记录之外,您还对List<Strecke> uri做其他事情吗? (也许您忘记发布所有相关部分?) -
我正在切换到应该在 ListFragment 中显示 ListArray 的片段。我猜数据来自StreckeAdapter。如果我错了,请纠正我。
-
但是 List (new ArrayList() ) 是空的。您如何将数据添加到此列表中?适配器只能显示您交给它的内容。 (TL;DR:将 uri 传递给 instance() 方法,以便您可以访问 Fragment 中的数据)
-
你指的是哪个 instance() 方法?数据已经在列表中,因为 logd 输出值。抱歉,这是我第一次为 android 编程。
-
您在 MainActivity 中记录值:"Log.d("StreckeMain", uri.toString());"但是您使用新的空列表创建了一个新的 Fragment 实例,然后您将其传递给 Fragment 的 onCreateView() 中的适配器。据我所知,您永远不会尝试将 List 从 MainActivity 传递给 Fragment。为了更好地理解我要告诉您的内容,请在片段代码中记录 streckeList.size()。从您在此处发布的内容来看,无论您将 Log.d(...) 语句放在何处,它都应该始终为零。
标签: java android android-fragments fragment adapter