RecyclerView不显示SQLite数据库中存储的项目

时间:2018-05-13 09:36:06

标签: android android-recyclerview concatenation textview

我的应用中遇到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"
  5. 更新:这是我的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 个答案:

答案 0 :(得分:0)

此处解决方案:

在访问适配器类的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);

                 //...
}

我认为这可能是问题

<string name="setDescriptionTxt">Description: %s</string>

<强>解决方案:

<string name="setDescriptionTxt">Description</string>