2016-10-06 65 views
1

我需要在我的RecyclerView中顯示一些卡片中的mapView。所以我遵循一些教程和我在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); 
     } 
    } 
} 
} 

,但是,當我告訴了結果,我得到這個錯誤: enter image description here

我嘗試使用notifyDataSetChanged();,但我給了同樣的錯誤。 我該如何解決這個問題? 感謝

+0

*我如何解決這個問題?*通過分析ng **你的** ......你將新的文字追加到舊的每一次查看被重用......它根本沒有做出任何的感覺 – 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("Base text: " + type.toString()); 

並與國際:

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

PS:同爲他人文字

+0

我嘗試你的解決方案..以前我明白,StringBuilder無法追加新的字符串在以前... – nani

+0

我有同樣的問題...我用你的解決方案,但它繼續追加其他字符串 – nani

+1

最後我解決了問題,你給我一個好主意,謝謝! – nani

相關問題