我想将drawable文件夹中的图像添加到我的RecyclerView,但遗憾的是没有显示任何内容。我在LogCat中没有任何错误和警告。我想在flagload void中添加图片。下面是Model,Adapter和Activity的一部分。我的申请的布局很好。 提前致谢
MODEL
private int logo;
public JsonMain(int logo) {
this.logo= logo;
}
public int getLogo() {
return logo;
}
public void setLogo(int logo) {
this.logo = logo;
}
ADAPTER
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
JsonMain pozycja = filtr.get(position);
holder.country_flag.setImageResource(pozycja.getLogo());
holder.country_name.setText(pozycja.getName());
holder.country_subregion.setText(pozycja.getSubregion());
holder.country_nativename.setText(pozycja.getNativeName());
holder.country_capital.setText(pozycja.getCapital());
}
@Override
public int getItemCount() {
return filtr.size();
}...
活性
private RecyclerView recyclerView;
private DataAdapter dataAdapter;
private List<JsonMain> dataArrayList;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading Data... Please wait...");
progressDialog.setIndeterminate(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.show();
}
private void initViews() {
recyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
loadJSON();
}
private void loadJSON() {
dataArrayList = new ArrayList<>();
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://restcountries.eu/").addConverterFactory(GsonConverterFactory.create()).build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
Call<List<JsonMain>> call = requestInterface.getJSON();
call.enqueue(new Callback<List<JsonMain>>() {
@Override
public void onResponse(Call<List<JsonMain>> call, Response<List<JsonMain>> response) {
dataArrayList = response.body();
recyclerView.setHasFixedSize(true);
flagload();
dataAdapter = new DataAdapter(getApplicationContext(), dataArrayList);
recyclerView.setAdapter(dataAdapter);
dataAdapter.notifyDataSetChanged();
dataAdapter.setOnitemClickListener(MainActivity.this);
progressDialog.dismiss();
}
@Override
public void onFailure(Call<List<JsonMain>> call, Throwable t) {
Log.e("Error", t.getMessage());
progressDialog.dismiss();
}
});}
private void flagload(){
JsonMain flag = new JsonMain(R.drawable.asd);
dataArrayList.add(flag);
flag = new JsonMain(R.drawable.abc);
dataArrayList.add(flag);
}...
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="@+id/image_view"
android:layout_width="120dp"
android:layout_height="90dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:padding="4dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/country_name"
android:layout_marginStart="5dp"
android:layout_toRightOf="@+id/image_view"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/country_subregion"
android:layout_below="@id/country_name"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_toRightOf="@+id/image_view"
android:background="@color/colorPrimary"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small.Inverse"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/country_nativename"
android:layout_below="@id/country_subregion"
android:layout_toRightOf="@+id/image_view"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/country_capital"
android:layout_below="@id/country_nativename"
android:layout_toRightOf="@+id/image_view"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="#000000"/>
</RelativeLayout>
</android.support.v7.widget.CardView>