2016-10-06 2 views
1

RecyclerView에 카드의 mapView가있는 일부 CardViews를 표시해야합니다. 그래서 몇 가지 튜토리얼을 따라 내 RecyclerView에서 그것을 만든 :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); 
     } 
    } 
} 
} 

을하지만, 내가 결과를 보여줄 때,이 오류 얻을 : 나는 notifyDataSetChanged();를 사용하려고 enter image description here

을하지만 난 같은 오류를 제공 . 이 문제는 어떻게 해결할 수 있습니까? 감사

+0

에 대해 동일 *이 문제를 어떻게 해결할 수 있습니까? ng ** your ** ... 뷰가 재사용 될 때마다 새 텍스트를 낡은 것에 추가하고 있습니다 ... 전혀 감각을 만들지 못합니다 – Selvin

+0

그래서 당신은 천재적입니다! 그리고 나는 어디에 새로운 텍스트를 추가 하는가? – nani

+0

코드를 이해하고 있습니까? 디버깅 오리를 사용하여 줄 단위로 설명하십시오. – Selvin

답변

1

나는 버그가 여기에 있다고 생각 :

type = new StringBuilder(mHolder.tipologia.getText().toString().trim()); 
type.append(" ").append(locate); 
mHolder.tipologia.setText(type.toString()); 

RecyclerView 뷰를 다시 사용합니다. 그리고 여기에서 이전 텍스트를 가져 와서 뭔가 추가합니다. 따라서 View을 재활용하면 이전 데이터에 새 데이터를 추가합니다.

당신은 같은 변경할 수 있습니다

: 단순히

type = new StringBuilder("Base text: "); 
type.append(locate); 
mHolder.tipologia.setText(type.toString()); 

또는 :

mHolder.tipologia.setText(context.getString(R.string.my_base_text) + type.toString()); 

PS :

mHolder.tipologia.setText("Base text: " + type.toString()); 

그리고 국제와

다른 텍스트

+0

나는 당신의 솔루션을 시도 .. 이전에 StringBuilder 이전에 새로운 문자열을 추가 할 수 없다는 것을 이해했다 ... – nani

+0

나는 당신의 솔루션을 사용하지만 그것은 다른 문자열을 추가하는 것을 계속한다 – nani

+1

마침내 나는 문제를 해결한다. , 나 한테 좋은 생각을 해줘, 고마워! – nani