gpt4 book ai didi

android - 未显示所有数据,但带有 RecyclerView 的 AdMobs

转载 作者:行者123 更新时间:2023-12-04 15:28:55 25 4
gpt4 key购买 nike

我正在尝试使用 RecyclerView 显示数据列表,并在显示 3 个数据后添加 AdMob。我关注了this stackoverflow Q&A因为它与我的代码几乎相同,但我使用的是 Retrofit。

但我的结果只显示测试 AdMob 行...它不显示我数据的 name 字符串。

这是我的主要 Activity :

public class Nilai extends AppCompatActivity implements NilaiView {

RecyclerView recyclerView;
SwipeRefreshLayout swipeRefresh;

NilaiPresenter presenter;
RecyclerViewAdopter adapter;
List<modelNilai> score;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nilai);

MobileAds.initialize(this, "ID_APP");

recyclerView = findViewById(R.id.recyclerView);
swipeRefresh = findViewById(R.id.swipe_refresh);

recyclerView.setLayoutManager(new LinearLayoutManager(this));

presenter = new NilaiPresenter(this);
presenter.getNilai();

swipeRefresh.setOnRefreshListener(
() -> presenter.getNilai()
);

}

@Override
public void showLoading() {
swipeRefresh.setRefreshing(true);
}

@Override
public void hideLoading() {
swipeRefresh.setRefreshing(false);
}

@Override
public void onGetNilai(List<modelNilai> scores) {
adapter = new RecyclerViewAdopter(Nilai.this, scores);
adapter.notifyDataSetChanged();
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);

score = scores;

for (int i = 0; i <= scores.size(); i += 3)
{
modelNilai myString2 = new modelNilai();
myString.setId(i);
scores.add(i,myString);
}

}

}

我的适配器:

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

private Context mContext;
private List<modelNilai> mList;

public RecyclerViewAdopter(Context mContext, List<modelNilai> mList) {
this.mList = mList;
this.mContext = mContext;
}

public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public MyViewHolder(View view) {
super(view);
name = view.findViewById(R.id.namaSoal);
}
}

public static class ViewHolderAdMob extends RecyclerView.ViewHolder {
public AdView mAdView;
public ViewHolderAdMob(View view) {
super(view);
mAdView = view.findViewById(R.id.adView);

AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("CEE26DC1A5BBF0E603B08A5460483046")
.build();
mAdView.loadAd(adRequest);
}
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch(viewType){
case 1:
{
View v = inflater.inflate(R.layout.list_nilai_ad, parent, false);
viewHolder = new MyViewHolder(v);
break;
}
case 2:
default:
{
View v = inflater.inflate(R.layout.banner_ad_row, parent, false);
viewHolder = new ViewHolderAdMob(v);
break;
}
}
return viewHolder;
}

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

modelNilai model = mList.get(holder.getAdapterPosition());

switch(holder.getItemViewType()){
case 1:{
MyViewHolder viewHolder = (MyViewHolder) holder;
viewHolder.name.setText(model.getName());
break;
}
case 2:{
break;
}
}
}

@Override
public int getItemViewType(int position)
{
return mList.get(position).getId();
}

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

我的主持人:

public class NilaiPresenter {
private NilaiView view;

public NilaiPresenter(NilaiView view) {
this.view = view;
}

void getNilai() {
view.showLoading();

ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<List<modelNilai>> call = apiInterface.getNilai();
call.enqueue(new Callback<List<modelNilai>>() {
@Override
public void onResponse(@NonNull Call<List<modelNilai>> call, @NonNull Response<List<modelNilai>> response) {
view.hideLoading();
if (response.isSuccessful() && response.body() != null) {
view.onGetNilai(response.body());
}

}

@Override
public void onFailure(@NonNull Call<List<modelNilai>> call, @NonNull Throwable t) {
view.hideLoading();
view.onRequestError(t.getLocalizedMessage());

}
});
}
}

我的模型类:

public class modelNilai {
@Expose
@SerializedName("id") private int id_nilai;
@Expose
@SerializedName("name") private String name;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

还有我的 View 界面:

public interface NilaiView {
void showLoading();
void hideLoading();
void onGetNilai(List<modelNilai> scores);
}

手机上的结果:

screenshot

它应该显示 3 行名称和 1 行 Admob,并每 4 行重复一次。

最佳答案

也许,getItemViewType() 方法应该是这样的:

@Override
public int getItemViewType(int position)
{
return (position + 1) % 4 == 0 ? 2 : 1;
}

您还必须修改 getItemCount() 方法。因为它应该返回所有 mList 行的计数和 Admob 行的 PLUS

@Override
public int getItemCount() {
return mList.size() + mList.size / 4;
}

除此之外,onBindViewHolder 方法受到影响:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
modelNilai model = mList.get(holder.getAdapterPosition() - holder.getAdapterPosition() / 4);

if ((position + 1) % 4 != 0) {
MyViewHolder viewHolder = (MyViewHolder) holder;
viewHolder.name.setText(model.getName());
}
}

关于android - 未显示所有数据,但带有 RecyclerView 的 AdMobs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61692401/

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