在recyclerview上获取项目计数不起作用

时间:2019-01-14 09:20:46

标签: android android-recyclerview

我只是想知道如何在我的recyclerview上获取商品计数,并尝试使用此代码,但是对我来说不起作用。您能帮我弄清楚我的代码有什么问题吗?

我的活动:

    public class NavDrawerFleet extends AppCompatActivity
            {
        private List<NavDrawerFleetGetterSetter> navList= new ArrayList<>();
        private RecyclerView recyclerView;
        private NavDrawerFleetAdapter mAdapter;

            protected void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_nav_drawer_fleet);
            Toolbar toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            Textview sucessCount=findViewById(R.id.success_count);
            recyclerView =findViewById(R.id.nav_sent);
            mAdapter = new NavDrawerFleetAdapter(navList);
            RecyclerView.LayoutManager mLayoutManager = new 
            LinearLayoutManager(getApplicationContext());
            recyclerView.setLayoutManager(mLayoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.setAdapter(mAdapter);

            //item count in recyclerview 
            sucessCount.setText(String.valueOf(mAdapter.getItemCount()));
            }
         }

我的XML:

               <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/success_count"
                android:textSize="15dp"
                android:text="00000"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="260dp">
               </Textview>

myConstructor

public class NavDrawerFleetGetterSetter {
String lat,lon,dateTime;

public NavDrawerFleetGetterSetter(String lat, String lon, String dateTime) {
    this.lat = lat;
    this.lon = lon;
    this.dateTime = dateTime;
}

public String getLat() {
    return lat;
}

public void setLat(String lat) {
    this.lat = lat;
}

public String getLon() {
    return lon;
}

public void setLon(String lon) {
    this.lon = lon;
}
enter code here
public String getDateTime() {
    return dateTime;
}

public void setDateTime(String dateTime) {
    this.dateTime = dateTime;
}

}

这些是我的输出:

enter image description here

如果您注意到,发送失败的邮件未在我的recyclerview上计数cardview数据。

1 个答案:

答案 0 :(得分:0)

摘自Recyclerview.Adapters Android文档(https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter#getitemcount)getItemCount()计算适配器保存的数据集中的项目总数。

由于我看不到代码中的任何项目添加到您新创建的navList中,因此getItemCount()似乎正确为零。您必须将项目添加到列表中才能计算在内!