0

I'm having a bit of problem with RecyclerView in my app. I'll try to keep it short and simple.

Up to this point, my app shows no building errors and it doesn't crash. However, after adding some data in my database, RecyclerView remains empty. I know that SQLite database is running and can be populated.

What I suspect might be the cause is concatenating strings in ViewHolder, which according to what I've read (check the references on the bottom) should be accomplished using resource strings with placeholders, for example

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

should be replaced with

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

where string.xml includes <string name="setDescriptionTxt">Description: %s</string>.

However, calling the getString() produces an error because apparently, this method is unknown. It would be great if anyone is willing to help me out or at least point me in the right direction.

Here's the RecordsAdapter.java class:

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

References:

  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"

Update: here is my logcat displaying an error:

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
Pero Alex
  • 43
  • 1
  • 1
  • 6
  • where is your getter setter methods? – Masoom Badi May 13 '18 at 09:49
  • My getter and setter methods are stored in a separate class called `Records.java`. Shall I attach this class to my question? – Pero Alex May 13 '18 at 09:58
  • what error are you getting, can you post your logcat? viewholder part is good. may be something wrong with the main activity which you are setting adapter. – Masoom Badi May 13 '18 at 10:01
  • If I use `viewHolder.recordDescriptionTxtV.setText("Description" + records.getDescription());`, then I get no errors. However, using the second option I've listed returns error in the IDE. It says that it cannot resolve the method `getString()`. Posting `logcat` in a second. – Pero Alex May 13 '18 at 10:27
  • if you want to get string from **string.xml**, try like this : `String records = getResources().getString(R.string.mystring) + records.getDescription() ;` – Masoom Badi May 13 '18 at 10:34
  • I've tried your advice with `getResources()` method, but it signals "cannot resolve method` again! – Pero Alex May 13 '18 at 10:41
  • The error of rippledrawer has nothing to do with this i guess.. – Masoom Badi May 13 '18 at 10:43
  • You are mentioning `MainActivity`, but my code is actually located in another class called `RecordsAdapter`. Could this be the problem? Because I've tried calling `getString()` in `MainActivity` and there are no errors. – Pero Alex May 13 '18 at 10:46

1 Answers1

0

Here Solution:

Use Context reference(mContext) while accessing data from strings.xml in the adapter class.

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

                 //...
}

I think that might be the problem

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

Solution:

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

Community
  • 1
  • 1
Kush
  • 355
  • 2
  • 20
  • Tried your recommendation, but unfortunately no results. – Pero Alex May 13 '18 at 12:30
  • Tried it again, no effect. However, I've seen the following message regarding `final int position`: "Do not require position as fixed; only use immediately and call `viewHolder.getAdapterPosition()` to look it up later". – Pero Alex May 14 '18 at 17:55
  • What you've proposed in your edit is what I have done already when you first answered my question. I'm starting to think I messed up something else. Here's the link to my project files, if anyone is kind enough to check them out. One activity is missing because I haven't completed it yet, but the remaining files are in there. Here's the link: https://drive.google.com/open?id=1GNskm36CSB0Tu10xd8gWnIH0iOJO0kVO – Pero Alex May 16 '18 at 19:10
  • Also, thank you Kush Vatsa for taking your time and sticking here with me. I really appreciate that. Also, I'd like to thank everyone else for your time. – Pero Alex May 16 '18 at 19:12