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:
- https://developer.android.com/guide/topics/resources/string-resource#FormattingAndStyling
- https://developer.android.com/reference/android/content/Context#summary
- Android: How do I get string from resources using its name?
- 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