2017-04-25 87 views
-1

我有一個recyclerview顯示來自api提要的數據,但它是添加空白行數據。我如何只添加所需的數據,如果不符合標準不添加空白行?Recycler查看項添加

這裏是我的數據適配器:

public class DataAdapter extends 
RecyclerView.Adapter<DataAdapter.ViewHolder>{ 

    private ArrayList<Included> includedData; 
    private Included matchDataIncluded; 
    private Data matchData; 


    public DataAdapter(ArrayList<Included> includedData) { 
     this.includedData = includedData; 
     this.matchDataIncluded = matchDataIncluded; 
    } 


    @Override 
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int i) { 

     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false); 
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
      if (Objects.equals(includedData.get(position).getType(), "competitions") && includedData.get(position).getId() != null) { 
       holder.tvCompetitionName.setText(includedData.get(position).getAttributes().); 
      } 
     } 


    } 

    @Override 
    public int getItemCount() { 
     return includedData.size(); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public int getItemViewType(int position) { 
     return position; 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder { 

     private TextView tvCompetitionName; 

     public ViewHolder(View itemView) { 
      super(itemView); 

      tvCompetitionName = (TextView) itemView.findViewById(R.id.competitionNameTV); 
     } 
    } 
} 

回答

0

看一看這個,

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
     if (Objects.equals(includedData.get(position).getType(), "competitions") && includedData.get(position).getId() != null) { 
      holder.tvCompetitionName.setText(includedData.get(position).getAttributes().); 
     } 
    } 


} 

由於recyclerview的大小爲「includedData」的大小和OnBindViewHolder你把條件由於其你正在獲得空行,只是刪除條件,數據將被顯示。

+0

謝謝,是這就是我原來有,但我需要的條件只需要顯示數據符合該條件,行是空白的,因爲這些對象有數據,但不匹配的條件,所以我需要排除那些不匹配的行條件 – MinatoN3886

+0

@ MinatoN3886,所以當條件滿足時,您必須在「includedData」中添加值,而不是在「OnBindViewholder」上創建檢查 –

+0

對不起,我不明白你的意思,你能給我一個例子嗎? – MinatoN3886