简体   繁体   English

recyclerview不显示片段中的sqlite数据

[英]recyclerview not displaying sqlite data in fragment

Code

public class UsageSettings2 extends Fragment {
MainData mDatabaseHelper;
RecyclerView mRecyclerView;
UserDataHelper mHelper;
private List<UsageHelper> usage = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private UsageAdapter mAdapter;
SQLiteDatabase db;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView =  inflater.inflate(R.layout.settings_usage2, container, false);

    final MainData myDBHlpr = new MainData(getActivity());
    db = myDBHlpr.getWritableDatabase();

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.usagelist);
        //Add the data first
        linearLayoutManager = new LinearLayoutManager(getActivity());
        //and then create a object and pass the lis
        mAdapter = new UsageAdapter(usage);

    mRecyclerView.setLayoutManager(linearLayoutManager);
    mRecyclerView.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();

    Cursor csr = myDBHlpr.getAllQuestions2(getActivity());
    while (csr.moveToNext()) {

        String name = csr.getString(csr.getColumnIndex("Name"));
        int sent = csr.getInt(csr.getColumnIndex("MessagesSent"));
        int recieved = csr.getInt(csr.getColumnIndex("MessagesRecieved"));
        int total = csr.getInt(csr.getColumnIndex("Messages"));
        String time = csr.getString(csr.getColumnIndex("TimeSpent"));

        new UsageHelper(name,sent,recieved,total,time);


        int profile_counts = myDBHlpr.getProfilesCount2();
        Log.d("FFF", String.valueOf(profile_counts));


    }



        return rootView;
    }
}

Adapter 适配器

public class UsageAdapter extends RecyclerView.Adapter<UsageAdapter.UsageViewHolder>{

private List<UsageHelper> mUsageHelper;

public UsageAdapter(List<UsageHelper> UsageAdapter) {
    this.mUsageHelper = UsageAdapter;
}

public class UsageViewHolder extends RecyclerView.ViewHolder{
    public TextView mName;
    public TextView mSent;
    public TextView mRecieved;
    public TextView mTotal;
    public TextView mTime;

    public UsageViewHolder(View view) {
        super(view);
        mName = (TextView)view.findViewById(R.id.name);
        mSent = (TextView)view.findViewById(R.id.sent);
        mRecieved = (TextView)view.findViewById(R.id.recieved);
        mTotal = (TextView)view.findViewById(R.id.total);
        mTime = (TextView)view.findViewById(R.id.time);
    }
}

@Override
public UsageAdapter.UsageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_activity_usage,parent,false);
    return new UsageAdapter.UsageViewHolder(V);
}

@Override
public void onBindViewHolder(final UsageViewHolder holder, int position) {
    final UsageHelper usageHelper = mUsageHelper.get(position);

    holder.mName.setText(usageHelper.getName());
    holder.mSent.setText(usageHelper.getSent());
    holder.mRecieved.setText(usageHelper.getRecieved());
    holder.mTotal.setText(usageHelper.getTotal());
    holder.mTime.setText(usageHelper.getTime());

}


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

Modal 语气

public class UsageHelper {
private String Name;
private int Sent;
private int Recieved;
private int Total;
private String Time;

public UsageHelper() {
}

public UsageHelper(String Name, int Sent ,int Recieved, int Total, String Time) {

    this.Name = Name;
    this.Sent = Sent;
    this.Recieved = Recieved;
    this.Total = Total;
    this.Time = Time;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    this.Name = name;
}

public int getSent() {
    return Sent;
}

public void setSent(int sent) {
    this.Sent = sent;
}

public int getRecieved() {
    return Recieved;
}

public void setRecieved(int recieved) {
    this.Recieved = recieved;
}

public String getTime() {
    return Time;
}

public void setTime(String time) {
    this.Time = time;
}

public int getTotal() {
    return Total;
}

public void setTotal(int total) {
    this.Total = total;
}

} }

The problem is that its not showing anything... the fragment is blank when i open it although the data is present... Any idea why its now showing any data? 问题在于它没有显示任何内容...尽管有数据,但当我打开它时该片段为空白...知道为什么它现在显示任何数据吗? Im not getting any error just that the data isnt showing.......................................................................... 我没有任何错误,只是数据没有显示..................................... ...................................

Got an error after making changes according to answers 根据答案进行更改后出现错误

Custom 习惯

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/usage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black">

<RelativeLayout
    android:weightSum="1"
    android:id="@+id/linear"
    android:padding="1dp"
    android:background="@color/white"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:textSize="15dp"
        android:layout_toLeftOf="@+id/mid"
        android:text="Name"
        android:layout_alignParentStart="true"
        android:id="@+id/name"
        android:paddingVertical="10dp"
        android:background="@color/black"
        android:gravity="center_horizontal"
        android:textColor="@color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />



    <TextView
        android:id="@+id/sent"
        android:layout_toRightOf="@id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:gravity="center_horizontal"
        android:paddingVertical="10dp"
        android:text="Sent"
        android:textColor="@color/white"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/recieved"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/sent"
        android:background="@color/black"
        android:gravity="center_horizontal"
        android:paddingVertical="10dp"
        android:text="Recieved"
        android:textColor="@color/white"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/total"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/recieved"
        android:background="@color/black"
        android:gravity="center_horizontal"
        android:paddingVertical="10dp"
        android:text="Total"
        android:textColor="@color/white"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/recieved"
        android:background="@color/black"
        android:gravity="center_horizontal"
        android:paddingVertical="10dp"
        android:text="Time \nSpent"
        android:textColor="@color/white"
        android:textSize="15dp" />


</RelativeLayout>

In the while loop of onCreateView() , add each row from the db to the list: onCreateView()while循环中,将db中的每一行添加到列表中:

usage.add(new UsageHelper(name,sent,recieved,total,time));

instead of just: 不仅仅是:

new UsageHelper(name,sent,recieved,total,time)

which does nothing. 什么都不做。
And after the loop finishes: 循环结束后:

mAdapter.notifyDataSetChanged();

so the RecyclerView loads all the added rows to the list. 因此RecyclerView会将所有添加的行加载到列表中。
Edit 编辑
Replace 更换

holder.mSent.setText(usageHelper.getSent());

with

holder.mSent.setText("" + usageHelper.getSent());

because usageHelper.getSent() is int and it is treated as a resource id. 因为usageHelper.getSent()int ,因此被视为资源ID。

The usage list is always empty because you are not adding the new "UsageHelper" you are creating from your Cursor into the list. usage列表始终为空,因为您没有将要从游标创建的新“ UsageHelper”添加到列表中。

What you need to do is the following: 您需要执行以下操作:

Cursor csr = myDBHlpr.getAllQuestions2(getActivity());
while (csr.moveToNext()) {

    ...

    UsageHelper newListItem = new UsageHelper(name,sent,recieved,total,time);
    usage.add(newListItem);

    ...

}
mAdapter.notifyDataSetChanged();

When you finish adding elements to the list, you want to notify your adapter that the list has new items, so you have to call to mAdapter.notifyDataSetChanged(); 完成向列表中添加元素后,您想通知适配器该列表中有新项目,因此您必须调用mAdapter.notifyDataSetChanged(); and the new elements will be displayed 然后将显示新元素

add mAdapter.notifyDataSetChanged(); 添加mAdapter.notifyDataSetChanged(); in while condition or after adding your data in model class. 在while条件中或在模型类中添加数据之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM