RecyclerView不会显示我的数据库内容

时间:2017-04-12 21:57:45

标签: android database sqlite android-fragments android-recyclerview

我有一个用户数据库,我试图使用RecyclerView来显示我拥有的每个用户条目。从我的日志中,我可以看出我的RecyclerView甚至没有与之互动。这是我称之为的课程:

public class WelcomePage extends AppCompatActivity {

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

    FragmentManager manager = getFragmentManager();
    RecyclerFragment mRecyclerFragment = new RecyclerFragment();
    if(mRecyclerFragment == null) {
        manager.beginTransaction()
                .add(R.id.recycler, mRecyclerFragment)
                .commit();
    }
}
}

这是我的持有人类:

public class RecyclerHolder extends RecyclerView.ViewHolder{

private static final String TAG = "RecyclerHolder";

private TextView mFullName;
private TextView mBirthDate;
private TextView mHomeTown;
private TextView mBio;
User mUser;

RecyclerHolder(View itemView){
    super(itemView);

    mFullName = (TextView)itemView.findViewById(R.id.text_view_name);
    mBirthDate = (TextView)itemView.findViewById(R.id.text_view_birthday);
    mHomeTown = (TextView)itemView.findViewById(R.id.text_view_hometown);
    mBio = (TextView)itemView.findViewById(R.id.text_view_bio);

public void bind(User user) {
    mUser = user;
    mFullName.setText(user.getFullName());
    mBirthDate.setText(user.getBirthDate().toString());
    mHomeTown.setText(user.getHomeTown());
    mBio.setText(user.getBio());
}

}

这是我的适配器类:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerHolder> {
private ArrayList<User> mUsers;

public RecyclerAdapter(ArrayList<User> user) {
    mUsers = user;
}

@Override
public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.view_recycler, parent, false);
    RecyclerHolder holder = new RecyclerHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(RecyclerHolder holder, int position){
    holder.bind(mUsers.get(position));

}

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

}

这是我的片段类:

public class RecyclerFragment extends Fragment {

private RecyclerView mRecyclerView;
DBCursorWrapper db;

public RecyclerFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_recycler, container, false);
    mRecyclerView = (RecyclerView)view.findViewById(R.id.recycler);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    RecyclerAdapter adapter = new RecyclerAdapter(db.getUserList());
    mRecyclerView.setAdapter(adapter);

    return view;

}

}

以下是我的持有人的XML:

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

<TextView
    android:id="@+id/text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    />


</LinearLayout>

这是我的片段的XML:

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.csc214.just4kas.project02.RecyclerFragment">

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

这是我用来调用我的RecyclerView的XML:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame_layout_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.csc214.just4kas.project02.WelcomePage">

</FrameLayout>

getUserList()已经过单独测试,效果很好!

任何帮助都将非常感激!我只想要一个包含我数据库中所有内容的RecyclerView !!谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您在替换时可能会使用错误的布局将其添加到主活动中。尝试(R.id.frame_layout_recycler_view)或R.id.recycler。

或者在此上下文中使用数据库时可能存在问题。