Issue
I need to display in my RecyclerView some CardViews with a mapView in the card. So I follow some tutorial and I made it in my RecyclerView:
public class ShowParkAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
private List<Location> locations;
private Context context;
public ShowParkAdapter(List<Location> locations,Context context)
{
this.locations = locations;
this.context = context;
}
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
MapViewListItemView mapViewListItemView = new MapViewListItemView(context);
mapViewListItemView.mapViewOnCreate(null);
ParkAdapter parkHolder = new ParkAdapter(mapViewListItemView);
return parkHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
ParkAdapter mHolder = (ParkAdapter)holder;
StringBuilder type, createdAt, stateAt, segnalationAt;
String locate;
Location location = locations.get(position);
mHolder.setmMapViewListItemViewPutMarkers(location.getLatitude(),location.getLongitude(),location.getType());
mHolder.mapViewListItemViewOnResume();
locate = location.getType();
type = new StringBuilder(mHolder.tipologia.getText().toString().trim());
type.append(" ").append(locate);
mHolder.tipologia.setText(type.toString());
createdAt = new StringBuilder(mHolder.data.getText().toString().trim());
createdAt.append(" ").append(location.getCreatedAt());
mHolder.data.setText(createdAt.toString());
stateAt = new StringBuilder(mHolder.state.getText().toString().trim());
stateAt.append(" ").append("verificato");
mHolder.state.setText(stateAt.toString());
segnalationAt = new StringBuilder(mHolder.segnalations.getText().toString().trim());
segnalationAt.append(" ").append("0");
mHolder.segnalations.setText(segnalationAt.toString());
notifyDataSetChanged();
}
public int getItemCount()
{
return locations.size();
}
private class ParkAdapter extends RecyclerView.ViewHolder
{
private MapViewListItemView mMapViewListItemView;
TextView tipologia, data,state,segnalations;
public ParkAdapter(final MapViewListItemView mapViewListItemView)
{
super(mapViewListItemView);
mMapViewListItemView = mapViewListItemView;
tipologia = (TextView)mapViewListItemView.findViewById(R.id.type);
data = (TextView)mapViewListItemView.findViewById(R.id.addedAt);
state = (TextView)mapViewListItemView.findViewById(R.id.state);
segnalations = (TextView)mapViewListItemView.findViewById(R.id.signal);
state.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showErrorMessage(context,mapViewListItemView.getResources().getString(R.string.infoStato));
}
});
segnalations.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showErrorMessage(context,mapViewListItemView.getResources().getString(R.string.infoSign));
}
});
}
private void showErrorMessage(Context mContext,String message)
{
new AlertDialog.Builder(mContext)
.setMessage(message)
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
}).create().show();
}
public void setmMapViewListItemViewPutMarkers(double latitude, double longitude,String type)
{
if(mMapViewListItemView != null)
mMapViewListItemView.putMarkers(latitude,longitude,type);
}
public void mapViewListItemViewOnCreate(Bundle savedInstanceState) {
if (mMapViewListItemView != null) {
mMapViewListItemView.mapViewOnCreate(savedInstanceState);
Log.d("map","oncreate");
}
}
public void mapViewListItemViewOnResume() {
if (mMapViewListItemView != null) {
mMapViewListItemView.mapViewOnResume();
Log.d("map","onresume");
}
}
public void mapViewListItemViewOnPause() {
if (mMapViewListItemView != null) {
mMapViewListItemView.mapViewOnPause();
Log.d("map","onpause");
}
}
public void mapViewListItemViewOnDestroy() {
if (mMapViewListItemView != null) {
mMapViewListItemView.mapViewOnDestroy();
Log.d("map","ondestroy");
}
}
public void mapViewListItemViewOnLowMemory() {
if (mMapViewListItemView != null) {
mMapViewListItemView.mapViewOnLowMemory();
}
}
public void mapViewListItemViewOnSaveInstanceState(Bundle outState) {
if (mMapViewListItemView != null) {
mMapViewListItemView.mapViewOnSaveInstanceState(outState);
}
}
}
}
but, when I show the result, I get this error:
I try to use notifyDataSetChanged();
but I give the same errors.
How could I solve this problem?
Thanks
Solution
I think that the bug is here:
type = new StringBuilder(mHolder.tipologia.getText().toString().trim());
type.append(" ").append(locate);
mHolder.tipologia.setText(type.toString());
RecyclerView
will re-use the view. And here you take the previous text and adding something. So, when a View
is recycled, you append new data to the old data.
You can change to something like:
type = new StringBuilder("Base text: ");
type.append(locate);
mHolder.tipologia.setText(type.toString());
Or simply:
mHolder.tipologia.setText("Base text: " + type.toString());
And with intl:
mHolder.tipologia.setText(context.getString(R.string.my_base_text) + type.toString());
PS: the same for the others text
Answered By - Kevin Robatel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.