gpt4 book ai didi

android - fragment 内的空白 Recyclerview

转载 作者:行者123 更新时间:2023-11-29 14:13:17 32 4
gpt4 key购买 nike

我同时使用了 fragment 和 recyclerview。我也有数据库,我想查询来自数据库的结果并在 Activity 中显示结果。

然而,每次我尝试运行应用程序并切换到获取和查看结果的部分时,我似乎什么也得不到。根本没有结果只是空白。我不知道为什么它没有出现。

这是我的完整代码

示例类

public class Sample extends Fragment {
private View rootView;

public Sample() {}

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

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

rootView = inflater.inflate(sample, container, false);

RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView_sample);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setHasFixedSize(true);

DatabaseHandler db= new DatabaseHandler(getActivity());
List<SampleModel> list = db.getResults();

SampleAdapter sampleAdapter = new SampleAdapter(list);
mRecyclerView.setAdapter(sampleAdapter);

return rootView;
}
}

适配器类

public class SampleAdapter extends RecyclerView.Adapter <SampleAdapter.ViewHolder> {

private List<SampleModel> list;
private Context mContext;

public SampleAdapter (List<SampleModel> list) {
list = list;
}

@Override
public SampleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.sample_item, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(SampleAdapter.ViewHolder holder, int position) {
SampleModel sample = list.get(holder.getAdapterPosition());
holder.title.setText(sample.getTitle())
}

@Override
public int getItemCount() {
return (list != null? list.size():0);
}

public class ViewHolder extends RecyclerView.ViewHolder {

public TextView title;

public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
}
}
}

XML

示例.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true">

<include layout="@layout/sample_recyclerview" />

</android.support.design.widget.CoordinatorLayout>

sample_recyclerview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"/>

</RelativeLayout>

sample_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:focusable="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="#808080">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff" />

</LinearLayout>

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

最佳答案

在您的SampleAdapter 中更改

list = list;

this.list = list;

关于android - fragment 内的空白 Recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40530446/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com