【问题标题】:RecyclerView Doesn't Display Items Stored In SQLite DatabaseRecyclerView 不显示存储在 SQLite 数据库中的项目
【发布时间】:2018-10-23 04:37:45
【问题描述】:

我的应用程序中的 RecyclerView 有点问题。我会尽量保持简短。

到目前为止,我的应用没有显示任何构建错误,也没有崩溃。但是,在我的数据库中添加一些数据后,RecyclerView 仍然为空。我知道 SQLite 数据库正在运行并且可以填充。

我怀疑的原因可能是在 ViewHolder 中连接字符串,根据我所阅读的内容(检查底部的引用)应该使用带有占位符的资源字符串来完成,例如

viewHolder.recordDescriptionTxtV.setText("Description" + records.getDescription());

应该替换为

String description = getString(R.string.setDescriptionTxt, records.getDescription());
viewHolder.recordDescriptionTxtV.setText(description);

其中string.xml 包括<string name="setDescriptionTxt">Description: %s</string>

但是,调用getString() 会产生错误,因为显然这种方法是未知的。如果有人愿意帮助我或至少为我指明正确的方向,那就太好了。

这是RecordsAdapter.java 类:

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

class RecordsAdapter extends RecyclerView.Adapter<RecordsAdapter.ViewHolder> {
    private List<Records> mRecordsList;
    private Context mContext;
    private RecyclerView mRecyclerView;

    public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView recordDescriptionTxtV;
    public TextView recordLocationTxtV;
    public TextView recordDayTxtV;
    public TextView recordMonthTxtV;
    public TextView recordYearTxtV;
    public TextView recordStartTxtV;
    public TextView recordFinishTxtV;
    public TextView recordCommentTxtV;

    public View layout;

    public ViewHolder(View view) {
        super(view);
        layout = view;
        recordDescriptionTxtV = (TextView) view.findViewById(R.id.description);
        recordLocationTxtV = (TextView) view.findViewById(R.id.location);
        recordDayTxtV = (TextView) view.findViewById(R.id.day);
        recordMonthTxtV = (TextView) view.findViewById(R.id.month);
        recordYearTxtV = (TextView) view.findViewById(R.id.year);
        recordStartTxtV = (TextView) view.findViewById(R.id.start);
        recordFinishTxtV = (TextView) view.findViewById(R.id.finish);
        recordCommentTxtV = (TextView) view.findViewById(R.id.comments);
    }
    }

    public void add(int position, Records records) {
    mRecordsList.add(position, records);
    notifyItemInserted(position);
    }
    public void remove(int position) {
    mRecordsList.remove(position);
    notifyItemRemoved(position);
    }

    public RecordsAdapter(List<Records> myDataset, Context context, RecyclerView recyclerView) {
    mRecordsList = myDataset;
    mContext = context;
    mRecyclerView = recyclerView;
    }

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

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
    final Records records = mRecordsList.get(position);

    viewHolder.recordDescriptionTxtV.setText("Description" + records.getDescription());
    viewHolder.recordLocationTxtV.setText("Location: " + records.getLocation());
    viewHolder.recordDayTxtV.setText("Day: " + records.getDay());
    viewHolder.recordMonthTxtV.setText("Month: " + records.getMonth());
    viewHolder.recordYearTxtV.setText("Year: " + records.getYear());
    viewHolder.recordStartTxtV.setText("Started at: " + records.getStart());
    viewHolder.recordFinishTxtV.setText("Finished at: " + records.getFinish());
    viewHolder.recordCommentTxtV.setText("Comments: " + records.getComments());

    viewHolder.layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("Choose option");
            builder.setMessage("Update or delete user?");

            builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    goToUpdateActivity(records.getId());
                }
            });

            builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                  RecordsDBHelper dbHelper = new RecordsDBHelper(mContext);
                  dbHelper.deleteRecordsRecords(records.getId(), mContext);
                  mRecordsList.remove(position);
                  mRecyclerView.removeViewAt(position);
                  notifyItemRemoved(position);
                  notifyItemRangeChanged(position, mRecordsList.size());
                  notifyDataSetChanged();
                }
            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            builder.create().show();
        }

    });
    }

    private void goToUpdateActivity(long recordID) {
    Intent goToUpdate = new Intent(mContext, UpdateRecordActivity.class);
    goToUpdate.putExtra("USER_ID", recordID);
    mContext.startActivity(goToUpdate);
    }

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

参考资料:

  1. https://developer.android.com/guide/topics/resources/string-resource#FormattingAndStyling
  2. https://developer.android.com/reference/android/content/Context#summary
  3. Android: How do I get string from resources using its name?
  4. Android TextView : "Do not concatenate text displayed with setText"

更新:这是我的logcat 显示错误:

05-13 12:31:39.797 15831-15831/com.example.benignfella.projectworkinghours E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering

【问题讨论】:

  • 你的 getter setter 方法在哪里?
  • 我的 getter 和 setter 方法存储在一个名为 Records.java 的单独类中。我应该将此课程附加到我的问题中吗?
  • 你遇到了什么错误,你能发布你的 logcat 吗?观众部分很好。您设置适配器的主要活动可能有问题。
  • 如果我使用viewHolder.recordDescriptionTxtV.setText("Description" + records.getDescription());,则不会出现错误。但是,使用我列出的第二个选项会在 IDE 中返回错误。它说它无法解析方法getString()。马上发布logcat
  • 如果你想从 string.xml 获取字符串,试试这样:String records = getResources().getString(R.string.mystring) + records.getDescription() ;

标签: android android-recyclerview concatenation textview


【解决方案1】:

这里解决方案:

从适配器类中的strings.xml访问数据时使用上下文引用(mCo​​ntext)。

class RecordsAdapter extends RecyclerView.Adapter<RecordsAdapter.ViewHolder> {
private List<Records> mRecordsList;
private Context mContext;

                 //...

String descriptionTxt = mContext.getString(R.string.setDescriptionTxt);
String description = descriptionTxt + records.getDescription();
viewHolder.recordDescriptionTxtV.setText(description);

                 //...
}

我认为这可能是问题所在

&lt;string name="setDescriptionTxt"&gt;Description: %s&lt;/string&gt;

解决方案:

&lt;string name="setDescriptionTxt"&gt;Description&lt;/string&gt;

【讨论】:

  • 试过你的推荐,但很遗憾没有结果。
  • 又试了一次,没有效果。但是,我看到了以下关于final int position 的消息:“不需要固定位置;仅立即使用并稍后调用viewHolder.getAdapterPosition() 查找”。
  • 您在编辑中提出的建议是您第一次回答我的问题时我已经完成的。我开始认为我搞砸了别的东西。这是我的项目文件的链接,如果有人愿意检查它们。缺少一项活动,因为我还没有完成它,但剩余的文件在那里。这是链接:drive.google.com/open?id=1GNskm36CSB0Tu10xd8gWnIH0iOJO0kVO
  • 另外,感谢 Kush Vatsa 抽出时间和我在一起。我真的很感激。另外,我要感谢其他人的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多