我必须从远程网络调用中显示 recyclerview 上的项目列表。我已经初始化了 recyclerview 并正确设置了它的布局管理器和适配器,但列表没有显示。
这是我的适配器类:
public class DashboardAdapter extends RecyclerView.Adapter<HomeDashboardHolder> {
private final Context context;
private List<HomeDashboard> itemsList;
public DashboardAdapter(Context context, List<HomeDashboard> itemsList) {
this.context = context;
this.itemsList = itemsList;
}
@Override
public HomeDashboardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_dashboard_items_layout, parent, false);
return new HomeDashboardHolder(view);
}
@Override
public void onBindViewHolder(HomeDashboardHolder viewholder, int position) {
HomeDashboard dashboard = itemsList.get(position);
viewholder.dashTitle.setText(dashboard.getDashTitle());
Picasso.with(context)
.load(dashboard.getDashIcon())
.placeholder(R.drawable.noimage)
.into(viewholder.dashIcon);
}
@Override
public int getItemCount() {
if (itemsList == null) {
return 0;
}
return itemsList.size();
}
}
到目前为止,这是我的活动代码:
public class HomeActivity extends AppCompatActivity {
private static final String TAG = HomeActivity.class.getSimpleName();
private Toolbar toolbar;
private SwipeRefreshLayout swipeRefresh;
private RecyclerView dashboardRV, recyclerDrawer;
private DrawerLayout drawerLayout;
private List<DrawerItems> itemsList = new ArrayList<>();
private List<HomeDashboard> dashboardsList = new ArrayList<>();
private RecyclerView.LayoutManager layoutManager;
private RecyclerDrawerAdapter drawerAdapter;
private DashboardAdapter dashboardAdapter;
private String slug;
private int cartLength;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
init();
setSupportActionBar(toolbar);
initDrawer();
setUpDashboard();
}
private void initDrawer() {
recyclerDrawer.setHasFixedSize(true);
recyclerDrawer.setLayoutManager(new LinearLayoutManager(this));
DrawerItems drawerItems = new DrawerItems("Products", R.drawable.basket);
itemsList.add(drawerItems);
DrawerItems drawerItems1 = new DrawerItems("Cart", R.drawable.cart);
itemsList.add(drawerItems1);
DrawerItems drawerItems2 = new DrawerItems("Checkout", R.drawable.check_out);
itemsList.add(drawerItems2);
DrawerItems drawerItems3 = new DrawerItems("Profile", R.drawable.profile);
itemsList.add(drawerItems3);
DrawerItems drawerItems4 = new DrawerItems("Info", R.drawable.info);
itemsList.add(drawerItems4);
DrawerItems drawerItems5 = new DrawerItems("About", R.drawable.about);
itemsList.add(drawerItems5);
drawerAdapter = new RecyclerDrawerAdapter(this, itemsList);
recyclerDrawer.setAdapter(drawerAdapter);
toolbar.setNavigationIcon(R.drawable.burger_menu);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
drawerLayout.openDrawer(GravityCompat.START);
}
}
});
setDrawerClickListener();
}
private void setDrawerClickListener() {
recyclerDrawer.addOnItemTouchListener(new RecyclerItemTouchListener(this, recyclerDrawer, new RecyclerClickListener() {
@Override
public void onClick(View view, int position) {
switch (position) {
case 0:
startActivity(new Intent(HomeActivity.this, AllProductsActivity.class));
drawerLayout.closeDrawer(GravityCompat.START);
break;
case 1:
startActivity(new Intent(HomeActivity.this, CartActivity.class));
drawerLayout.closeDrawer(GravityCompat.START);
break;
case 2:
Intent intent = new Intent(HomeActivity.this, CheckOutActivity.class);
startActivity(intent);
drawerLayout.closeDrawer(GravityCompat.START);
break;
case 3:
startActivity(new Intent(HomeActivity.this, ProfileActivity.class));
drawerLayout.closeDrawer(GravityCompat.START);
break;
case 4:
Log.d(TAG, "Info Clicked");
break;
case 5:
startActivity(new Intent(HomeActivity.this, AboutActivity.class));
drawerLayout.closeDrawer(GravityCompat.START);
break;
}
}
@Override
public void onLongClick(View view, int position) {
}
}));
}
private void init() {
swipeRefresh = findViewById(R.id.swipeRefresh);
toolbar = findViewById(R.id.toolbar);
dashboardRV = findViewById(R.id.dashboardRV);
recyclerDrawer = findViewById(R.id.recyclerDrawer);
drawerLayout = findViewById(R.id.drawerLayout);
}
private void setUpDashboard() {
dashboardRV.setHasFixedSize(true);
layoutManager = new GridLayoutManager(HomeActivity.this, 2, GridLayoutManager.VERTICAL, false);
dashboardRV.setLayoutManager(layoutManager);
getHomeDash();
}
public void getHomeDash() {
AndroidNetworking.get(Constants.CATEGORIES_ENDPOINT)
.setTag("Get Categories Dashboard")
.setPriority(Priority.HIGH)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "Categories Response:\t" + response.toString());
try {
JSONObject jsonObject = new JSONObject(response.toString());
final JSONArray categories = jsonObject.getJSONArray("categories");
for (int i = 0; i < categories.length(); i++) {
JSONObject object = categories.getJSONObject(i);
String id = object.getString("_id");
String title = object.getString("title");
String image = object.getString("image");
slug = object.getString("slug");
HomeDashboard dashboard = new HomeDashboard();
dashboard.setDashIcon(image);
dashboard.setDashTitle(title);
dashboard.setId(id);
dashboardsList.add(dashboard);
}
dashboardAdapter = new DashboardAdapter(getApplicationContext(), dashboardsList);
dashboardRV.setAdapter(dashboardAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
dashboardAdapter.notifyDataSetChanged();
}
@Override
public void onError(ANError anError) {
Log.d(TAG, "Request Failed:\t" + anError.getMessage());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.app_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_view_cart:
startActivity(new Intent(HomeActivity.this, CartActivity.class));
}
return true;
}
@Override
protected void onResume() {
super.onResume();
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
}
}
}
这是我对此活动的示例 json 响应:
D/HomeActivity: Categories Response: {"success":true,"message":"All Categories","categories":[{"_id":"5aac46d8644ac63f14ffe89b","title":"Shrti","slug":"shrti","image":"http:\/\/res.cloudinary.com\/aapni-dukan\/image\/upload\/v1521239768\/categoryImages\/eBeSBYrG7MP7rW2Sa4qqQd06_lwakio.png"},{"_id":"5aac5b01d224ac3b801b9967","title":"Fruits","slug":"fruits","image":"http:\/\/res.cloudinary.com\/aapni-dukan\/image\/upload\/v1521244929\/categoryImages\/uWQT1XEplligclP4PrNC-Zyf_eamj8u.jpg"}]}
我正在 getHomeDash() 中初始化和设置适配器。我也打过电话notifyDataSetChanged()
,但没有显示。
在日志中,要检查列表是否为空,我将大小设为 2 但无法显示任何项目。我也尝试在onCreate()
调用后立即设置适配器,getHomeDash()
但屏幕上仍然没有显示任何内容。
视图持有者类:
public class HomeDashboardHolder extends RecyclerView.ViewHolder {
public ImageView dashIcon;
public TextView dashTitle;
public HomeDashboardHolder(View itemView) {
super(itemView);
dashIcon = itemView.findViewById(R.id.dashIcon);
dashTitle = itemView.findViewById(R.id.dashTitle);
}
}
有人可以帮我解决这个问题吗?谢谢