我有一个问题,为什么RecyclerView不显示项目我不知道为什么不显示项目。
json网址
[
{
"id": 1,
"title": "English",
"photo": "en.png",
"shortcut": "en"
},
{
"id": 2,
"title": "Spanish",
"photo": "es.png",
"shortcut": "es"
},
{
"id": 3,
"title": "Arabic",
"photo": "ar.png",
"shortcut": "ar"
},
{
"id": 4,
"title": "Russian",
"photo": "ru.png",
"shortcut": "ru"
},
{
"id": 5,
"title": "Portuguese",
"photo": "pt.png",
"shortcut": "pt"
},
{
"id": 6,
"title": "French",
"photo": "fr.png",
"shortcut": "fr"
},
{
"id": 7,
"title": "Turkish",
"photo": "tr.png",
"shortcut": "tr"
}
]
活动
public class CategoriesActivity extends AppCompatActivity {
//mapping
@BindView(R.id.rv_categories)
RecyclerView rv_categories;
CategoriesAdapter categoriesAdapter;
List<CategoriesModel> categoriesModels = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
ButterKnife.bind(CategoriesActivity.this);
//set data
setData();
}
private void setData() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("***Stackoverflow")
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitService retrofitService = retrofit.create(RetrofitService.class);
Call<List<CategoriesModel>> call = retrofitService.get_categories();
call.enqueue(new Callback<List<CategoriesModel>>() {
@Override
public void onResponse(Call<List<CategoriesModel>> call, Response<List<CategoriesModel>> response) {
List<CategoriesModel> list = response.body();
for (CategoriesModel data : list) {
categoriesModels.add(new CategoriesModel(data.id, data.title, data.photo, data.shortcut));
}
rv_categories.setLayoutManager(new GridLayoutManager(CategoriesActivity.this, 2));
categoriesAdapter = new CategoriesAdapter(CategoriesActivity.this, categoriesModels);
rv_categories.setAdapter(categoriesAdapter);
}
@Override
public void onFailure(Call<List<CategoriesModel>> call, Throwable t) {
}
});
}
}
模型
public class CategoriesModel {
@SerializedName("id")
public String id;
@SerializedName("title")
public String title;
@SerializedName("photo")
public String photo;
@SerializedName("shortcut")
public String shortcut;
public CategoriesModel(String id, String title, String photo, String shortcut) {
this.id = id;
this.title = title;
this.photo = photo;
this.shortcut = shortcut;
}
}
接口
public interface RetrofitService {
//Categories
@GET("Categories/get_categories.php")
Call<List<CategoriesModel>> get_categories();
}
适配器
public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.ViewHolder> {
Context context;
List<CategoriesModel> categoriesModels;
public CategoriesAdapter(Context context, List<CategoriesModel> categoriesModels) {
this.context = context;
this.categoriesModels = categoriesModels;
}
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_categories, viewGroup, false);
ViewHolder adapterHolder = new ViewHolder(view);
return adapterHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
CategoriesModel categoriesModel = categoriesModels.get(i);
//set photo
Picasso.get().load(viewHolder.url + categoriesModel.photo).into(viewHolder.img_background);
}
@Override
public int getItemCount() {
return categoriesModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
//mapping
@BindView(R.id.img_background)
ImageView img_background;
@BindString(R.string.categories)
String url;
public ViewHolder(View view) {
super(view);
ButterKnife.bind(context, view);
}
}
}
row_categories
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/img_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
</ImageView>
activity_categories.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CategoriesActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_categories"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
从昨天开始,我一直试图找出问题所在,但没有找到问题,、、、、、、、、、、、、、、、、、、、、、、、、、、 ,,,,,,,,,,,,,,,,,,,,,
最佳答案
您可以避免此代码
for (CategoriesModel data : list) {
categoriesModels.add(new CategoriesModel(data.id, data.title, data.photo, data.shortcut));
}
并执行类似
categoriesModels.addAll(list);
的操作在您的
ViewHolder
中,您不需要此@BindString(R.string.categories)
String url;
由于您的
R.layout.row_categories
仅包含一个ImageView
。尝试将您的
row_categories.xml
更改为此:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="50dp"
android:layout_height="50dp">
<ImageView
android:id="@+id/img_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
</ImageView>
</LinearLayout>
另外,您的
RecyclerView
尝试将高度更改为wrap_content
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
如果不起作用,请尝试将您的
onCreate()
更改为此:@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
ButterKnife.bind(CategoriesActivity.this);
rv_categories.setHasFixedSize(true);
rv_categories.setLayoutManager(new GridLayoutManager(CategoriesActivity.this, 2));
setData();
}
编辑
我认为问题出在这里
public class CategoriesModel {
@SerializedName("id")
public String id;
@SerializedName("title")
public String title;
@SerializedName("photo")
public String photo;
@SerializedName("shortcut")
public String shortcut;
public CategoriesModel(String id, String title, String photo, String shortcut) {
this.id = id;
this.title = title;
this.photo = photo;
this.shortcut = shortcut;
}
}
问题就在这里
Picasso.get().load(viewHolder.url + categoriesModel.photo).into(viewHolder.img_background);
您必须将其更改为
Picasso.get().load(URL_BASE+"Forvo/Categories/Images/" + categoriesModel.getPhoto()).into(viewHolder.img_background);
您必须为所有这些属性添加
Setter
和Getter
。我建议您在要解析POJO类时都使用JsonSchema2Pojo。
关于android - RecyclerView不显示项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52613966/