如何检查Recyclerviews是空的

时间:2017-06-13 20:55:00

标签: android json android-recyclerview fragment

您好我在cardviews中的回收者视图,我通过一个截击请求从服务器(JSONobjects)获取数据。如何检测我的recyclerview是否为空?我已经尝试检查ArrayList是否为空,但似乎不起作用,我该如何解决?感谢

这是我的片段:

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


private List<CespiteOgg> cespiteOggList;
private Context context;

public CespiteAdapter(List<CespiteOgg> cespiteOggList, Context context) {

    this.cespiteOggList = cespiteOggList;
    this.context = context;
}

public class ViewHolder extends RecyclerView.ViewHolder
{

    public CardView cv;
    public TextView txtNumInventario;
    public TextView txtNomeCespite;
    public TextView txtDtCatalogazione;
    public TextView txtAula;
    public TextView txtNomeUser;


    ViewHolder(View itemView)
    {

        super (itemView);
        //cv = (CardView) itemView.findViewById(R.id.cardView);
        txtNumInventario = (TextView) itemView.findViewById(R.id.txtNumeroInventario);
        txtNomeCespite = (TextView) itemView.findViewById(R.id.txtNomeCespite);
        txtDtCatalogazione = (TextView) itemView.findViewById(R.id.txtDataCatalogazione);
        txtAula = (TextView) itemView.findViewById(R.id.txtAula);
        txtNomeUser= (TextView) itemView.findViewById(R.id.txtNomeUser);


    }
}

它是适配器:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                       http://www.springframework.org/schema/cloud/aws/context
                       http://www.springframework.org/schema/cloud/aws/context/spring-cloud-aws-context-1.0.xsd">

    <aws-context:context-credentials>
        <aws-context:instance-profile-credentials/>
        <aws-context:simple-credentials access-key="${cloud.aws.credentials.accessKey:}" secret-key="${cloud.aws.credentials.secretKey:}"/>
    </aws-context:context-credentials>

    <aws-context:context-region region="${cloud.aws.region.static:}"/>
    <aws-context:context-resource-loader/>
</beans>

4 个答案:

答案 0 :(得分:1)

您可以覆盖适配器getItemCount(getItemCount())以返回List的大小,并从RecyclerView(getAdapter())获取适配器。

答案 1 :(得分:1)

将此部分添加到适配器:

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

你可以通过以下方式获得recyclerView的大小:

adapter.getItemCount()

答案 2 :(得分:0)

您可能需要覆盖onCreateViewHolderonBindViewHoldergetItemCount。值getItemCount返回等于RecyclerView项的计数。

答案 3 :(得分:0)

您可以使用getitemcount

if (adapter.getItemCount() == 0)
{
    Toast.makeText(getApplicationContext(), "NO Data Found",Toast.LENGTH_SHORT).show();
}
相关问题