gpt4 book ai didi

android - Recyclerview 不调用 onCreate 或 onBind 或构造函数

转载 作者:行者123 更新时间:2023-11-30 02:19:59 48 4
gpt4 key购买 nike

RecyclerView 没有调用 onCreate 或 onBind 或构造函数。没有断点命中或日志。可能是什么原因?可能是什么原因?任何人如何解决这个问题?这是我的代码。适配器:

public class MainContentRecyclerAdapter extends RecyclerView.Adapter<MainContentRecyclerAdapter.MainContentViewHolder> {

private Context context;
private LayoutInflater inflater;
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
List<Item> items = new ArrayList<Item>();

public MainContentRecyclerAdapter(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
volleySingleton = VolleySingleton.getSingletonInstance();
imageLoader = volleySingleton.getImageLoader();
}

public void setData(List<Item> items) {
this.items = items;
notifyItemRangeChanged(0,items.size());
}

@Override
public MainContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.e("Sony","onCreateViewHolder");
View view = inflater.inflate(R.layout.cart_item, parent, false);
MainContentViewHolder holder = new MainContentViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(final MainContentViewHolder holder, int position) {
Log.e("Sony","onBindViewHolder");
Item currentItem = items.get(position);
if (currentItem.imageURL != null) {
imageLoader.get(currentItem.imageURL, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
Log.e("Sony","onResponseImage");
holder.itemImage.setImageBitmap(response.getBitmap());
}

@Override
public void onErrorResponse(VolleyError error) {
Log.e("Sony","onErrorImage");
holder.itemImage.setImageResource(R.drawable.default_product);
}
});
}

}

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

class MainContentViewHolder extends RecyclerView.ViewHolder {

ImageView itemImage;


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

itemImage = (ImageView) itemView.findViewById(R.id.imgItemPic);
}
}
}

这是我的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardItemHolderRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/imgItemPic"
android:layout_width="@dimen/itemImageDimen"
android:layout_height="@dimen/itemImageDimen"
android:layout_centerVertical="true"
android:contentDescription="@string/productImage"
android:src="@drawable/default_product"
android:elevation="2dp"
android:focusableInTouchMode="false"
android:layout_marginTop="@dimen/loginWidgetTopMargin" />

</RelativeLayout>

创建时的 Activity

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.appBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
//Calls volleyLibrary to fetch images using google image search
getData("Sachin");

mainListView = (RecyclerView) findViewById(R.id.recyclerList);
mainListView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
mainListView.setHasFixedSize(true);
recyclerViewListAdapter = new MainContentRecyclerAdapter(this);
mainListView.setAdapter(recyclerViewListAdapter);
mainListView.setLayoutManager(new LinearLayoutManager(this));
}

最佳答案

我在你的代码中看到的唯一问题

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

items.size();始终返回 0 要在设置适配器后调用 getData 来解决此问题。

   recyclerViewListAdapter = new MainContentRecyclerAdapter(this);     
mainListView.setAdapter(recyclerViewListAdapter);
getData("Sachin");

同时更新你的

设置数据

public void setData(List<Item> items) {
this.items = items;
mRecyclerAdapter.notifyDataSetChanged();
}

public void setData(List<Item> items) {
this.items = items;
}

在您调用 setData 之后从您的主要 Activity 调用

mRecyclerAdapter.notifyDataSetChanged();

关于android - Recyclerview 不调用 onCreate 或 onBind 或构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28718091/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com