【问题标题】:recyclerview not displaying sqlite data in fragmentrecyclerview 没有在片段中显示 sqlite 数据
【发布时间】:2018-11-29 13:11:16
【问题描述】:

代码

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;
    }
}

适配器

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();
}

模态

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;
}

}

问题是它没有显示任何东西......虽然数据存在,但当我打开它时片段是空白的......知道为什么它现在显示任何数据吗?我没有得到任何错误只是数据没有显示...................................... ....................................

根据答案修改后报错

自定义

<?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>

【问题讨论】:

  • 你不能将数据从 SQLite 添加到你的适配器
  • 其复杂的@pskink
  • 新的UsageHelper(名称,发送,接收,总计,时间);这行不添加@VladyslavMatviienko
  • 不,它只是创建一个实例。它不会在任何地方添加任何内容。 Java 不够聪明,无法神奇地理解 yu 为什么创建该对象以及您想如何使用它

标签: java android sql database sqlite


【解决方案1】:

usage 列表始终为空,因为您没有将您从光标创建的新“UsageHelper”添加到列表中。

你需要做的是:

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

    ...

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

    ...

}
mAdapter.notifyDataSetChanged();

当你完成向列表添加元素时,你想通知你的适配器列表有新的项目,所以你必须调用mAdapter.notifyDataSetChanged();,新的元素就会显示出来

【讨论】:

  • @NikkiZepher 发布您的布局代码。你确定那些 id 的 TextView 存在吗?
  • 好吧,我的错误是我试图 setText 一个 int 值......但仍然没有解决问题......见编辑
  • 在此布局custom_activity_usage 中,您应该在 ViewHolder 中使用具有正确 id 的所有 TextView,如果不这样做,它将失败。请上传该布局的代码@NikkiZepher
【解决方案2】:

添加 mAdapter.notifyDataSetChanged();在 while 条件下或在模型类中添加数据之后。

【讨论】:

  • 在 xml 文件中获取 R.id.sent
【解决方案3】:

onCreateView()while 循环中,将数据库中的每一行添加到列表中:

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

而不仅仅是:

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

什么都不做。
循环结束后:

mAdapter.notifyDataSetChanged();

因此 RecyclerView 将所有添加的行加载到列表中。
编辑
替换

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

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

因为usageHelper.getSent()int,它被视为资源ID。

【讨论】:

    猜你喜欢
    • 2015-04-03
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    相关资源
    最近更新 更多