当前位置: 首页 > 知识库问答 >
问题:

RecycerView不显示存储在SQLite数据库中的项

钦楚青
2023-03-14

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

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

应替换为

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

其中string.xml包括 描述:%s

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:如何使用字符串名称从资源中获取字符串?
  4. Android TextView:“不要用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

共有1个答案

严昀
2023-03-14

在适配器类中从strings.xml访问数据时使用上下文引用(mContext)。

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

                 //...
}

我想这可能是问题所在

说明:%s

 类似资料:
  • 在调试我的应用程序时,我注意到我的RecycerView显示与提供的数据不一致,即。 > 如果我设置了一个闹钟(RecycerView中的TextView设置了日期),然后滚动我的RecycerView,日期显示在错误的位置。例如,如果我在第4项设置了日期,那么由于某种原因,第3项也设置了日期 我已经查看了文档,但不确定如何相应地修补。你能帮我吗? 我的OnBindViewholder:

  • 我想在我的应用程序中显示来自sqlite数据库的图像。我的要求是显示文本视图及其相关图像。我将我的数据库文件保存在资产文件夹中。但实际上我的图像存储在服务器的“allimages”文件夹中。 我的数据库列(我保留的assets文件夹中的db)如下所示: S.无描述 1 div style=“文本对齐:对齐;” 2 div style="文本对齐:对齐;" 现在我的问题是在我的数据库中,图像路径存储

  • 我试图显示firebase realtime数据库中的项,我在数据库中有两个节点,我在适配器类函数上使用了Log方法,它显示有2个,但它没有显示在我的UI中,两项视图正在生成,但没有显示带有名称和mob的内容。

  • 问题内容: 如何在SQLite数据库中存储JSON对象?正确的方法是什么? 一个地方是Blob类型列。如果我可以将JSON对象转换为字节数组并使用Fileoutputstream 另一个想法是将文本列存储为字符串 问题答案: 将JSONObject转换为String并另存为TEXT / VARCHAR。 在检索同一列时,将String转换为JSONObject。 例如 写入数据库 从数据库读取

  • 我正在根据下面显示的代码从数据库填充ListView,代码片段实现了CursorLoader。早些时候,出于同样的目的,我使用SimpleCrsorAdapter,但出于某些原因,我不得不改为ArrayAdapter。 数据库只有两列: _id(整数主键自动增量)和消息(字符串非空) ListView工作正常,但问题是listview中显示的字符串会自动按字母顺序排序,而不是按存储顺序排序。而我在