RecyclerView不加载图像

时间:2017-09-28 18:42:19

标签: java android

我正在使用recyclerView作为具有标题,描述和图像的菜单。标题&显示说明但图像不显示。不会抛出任何错误或其他任何错误。

在我的代码中,imageName是我res中图像的文件名> drawable目录,即revise_notepad.jpg我缺少什么?

代码 - XML

activity_main

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.mark.spanishapp.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/esp_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>

menu_layout

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#FFFFFF"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/menuImg"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

代码 - Java

MainActivity

  package com.example.mark.spanishapp;

    import android.database.sqlite.SQLiteException;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.GridLayoutManager;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;

    import java.io.IOException;
    import java.sql.SQLException;
    import java.util.ArrayList;

    public class MainActivity extends AppCompatActivity {

        private final static String TAG = "MainActivity";
        DBHandler dbHandler = null;

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

        dbHandler = new DBHandler(this);
        try {
            dbHandler.createDataBase();

        }catch (IOException ioe){
            throw new Error("unable to create database");
        }

        try{
            dbHandler.openDataBase();
        }catch (SQLException sqle)
        {
            Log.e(TAG, sqle.getMessage());
        }

        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.esp_menu);
        recyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 2);
        recyclerView.setLayoutManager(layoutManager);
        ArrayList<MenuEsp> menuList = dbHandler.Get_MenuList();

        MyAdapter adapter = new MyAdapter(getApplicationContext(), menuList);
        recyclerView.setAdapter(adapter);

    }
}

MenuEsp

  package com.example.mark.spanishapp;

    public class MenuEsp {

    public String getMenu() {
        return menu;
    }

    public void setMenu(String menu) {
        this.menu = menu;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImageName() {
        return imageName;
    }

    public void setImageName(String imageName) {
        this.imageName = imageName;
    }

    private String menu;
    private String description;
    private String imageName;
}

MyAdapter

 package com.example.mark.spanishapp;

    import android.content.Context;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;

    import java.util.ArrayList;


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

    private ArrayList<MenuEsp> menuList;
    private Context context;

    public  MyAdapter(Context context, ArrayList<MenuEsp> menuList){
        this.context = context;
        this.menuList = menuList;
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
        holder.title.setText(menuList.get(position).getMenu());
        holder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);

        int id = this.context.getResources().getIdentifier(menuList.get(position).getImageName(), "drawable", this.context.getPackageName());
        holder.img.setImageResource(id);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView title;
        private TextView descirption;
        private ImageView img;

        public ViewHolder(View view) {
            super(view);
            descirption = (TextView)view.findViewById(R.id.description);
            title = (TextView)view.findViewById(R.id.title);
            img = (ImageView) view.findViewById(R.id.menuImg);
        }
      }
    }

0 个答案:

没有答案