How do I configure Image In Adapter when we have the file name and it is in the resource folder?

advertisements

I have image file name, and wanted to set in my layout which is in adapter, As i am using Recycle view.

{
  "formules": [
    {
      "title": "Freecharge",
      "imageName": "freerecharge.jpg",
      "description": "Get 3% Cashback on Bill Payments of `50 or more + Extra 1.2% Cashback from us",
      "expiry_date": "Valid till 12 Jul 2016"
    },
    {
      "title": "Airtel",
      "imageName": "airtel.jpg",
      "description": "Flat 5% OFF on Airtel postpaid bill payments & Airtel prepaid recharges made through Airtel Money + Upto 3.85% Cashback from",
      "expiry_date": "Valid till 31 Jul 2016"
    },
    {
      "title": "Cleartrip",
      "imageName": "ctrip.jpg",
      "description": "Flat 25% Cashback on Domestic Hotels + Upto `340 Cashback from us",
      "expiry_date": "Valid till 15 Jul 2016"
    },
    {
      "title": "FirstCry",
      "imageName": "fcry.jpg",
      "description": "Flat `400 OFF on order of `1100 from The Premium Store (For First Purchase) + Upto `40 Cashback from us",
      "expiry_date": "Valid till 31 Jul 2016"
    },
    {
      "title": "Dominos Pizza",
      "imageName": "dominoz.jpg",
      "description": "Buy 1 & Get 50% OFF on 2nd Pizza + Extra `29 Cashback from us",
      "expiry_date": "Valid till 31 Jul 2016"
    },
    {
      "title": "Lenskart",
      "imageName": "lenkart.jpg",
      "description": "Get your First Frame FREE + Upto `360 Cashback from us",
      "expiry_date": "Valid till 31 Jul 2016"
    },
    {
      "title": "Infibeam",
      "imageName": "infibeam.jpg",
      "description": "Upto 60% OFF plus Extra 10% OFF on Best-Selling Books worth `200 + Upto 7% Cashback from us",
      "expiry_date": "Valid till 20 Jul 2016"
    },
    {
      "title": "Indiatimes Shopping",
      "imageName": "itimeshoping.jpg",
      "description": "Shop for Multimedia Phones at `499 + Upto `250 Cashback from us",
      "expiry_date": "Valid till 31 Jul 2016"
    },
    {
      "title": "ExcitingLives",
      "imageName": "exiting.jpg",
      "description": "Sound And Light Camera Keychain at `399 + Extra `105 Cashback from us",
      "expiry_date": "Valid till 31 Jul 2016"
    },
    {
      "title": "Babyoye",
      "imageName": "baboye.jpg",
      "description": "End Of Season Sale - Buy 5 & Get 50% OFF on Maternity wear + Upto `410 Cashback from us",
      "expiry_date": "Valid till 31 Jul 2016"
    },
    {
      "title": "Ferns N Petals",
      "imageName": "frennPetals.jpg",
      "description": "Flat 18% OFF Sitewide + Upto 13% Cashback from us",
      "expiry_date": "Valid till 30 Sep 2016"
    }
  ]
}

Main Class where is doing action

public class CashBackFragments extends Fragment {
    private RecyclerView recyclerView;
    private MyRecyclerAdapter mAdapter;
    RecyclerView.LayoutManager mLayoutManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.cash_back_screen, container, false);
        ((HomeActivity) getActivity()).setTitle("Cash Back Offers");
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        ArrayList<HashMap<String, String>> rows = new CommonFunction(getActivity()).getData("cashBackList.json", "title", "imageName", "description", "expiry_date");
        ArrayList<FeedItem> rowItems = new ArrayList<>();
        for (int i = 0; rows.size() > i; i++) {
            FeedItem item = new FeedItem(rows.get(i).get("title"),
                    rows.get(i).get("imageName"),
                    rows.get(i).get("description"),
                    rows.get(i).get("expiry_date"));
            rowItems.add(item);
        }
        mAdapter = new MyRecyclerAdapter(rowItems,getActivity());
        recyclerView.setAdapter(mAdapter);
        RecyclerView.ItemDecoration itemDecoration =
                new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL);
        recyclerView.addItemDecoration(itemDecoration);
        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        ((HomeActivity) getActivity()).setTitle("Dashboard");
    }
}

My Adapter class is as :

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {

    private List<FeedItem> dataList;
    Context context;

    public MyRecyclerAdapter(ArrayList<FeedItem> rowItems, FragmentActivity activity) {
        this.dataList = rowItems;
        context = activity;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title;
        public TextView description;
        public TextView expiry_date;
        public ImageView img_cashback;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            description = (TextView) view.findViewById(R.id.txt_description);
            expiry_date = (TextView) view.findViewById(R.id.expiry_date);
            img_cashback = (ImageView) view.findViewById(R.id.img_cashback);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cash_back_list, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
    FeedItem data = dataList.get(position);
    holder.title.setText(data.getTitle());
    holder.description.setText(data.getDescription());
    holder.expiry_date.setText(data.getExpiryDate());
    int id = context.getResources().getIdentifier("com.rayz.digitalpostbox:drawable/" + data.getImageName(), null, null);
    holder.img_cashback.setImageResource(id);
}

and FeedItem class is as:

public class FeedItem {
    private String title, imageName, description, expiryDate;

    public FeedItem(String title, String imageName, String description, String expiryDate) {
        this.title = title;
        this.imageName = imageName;
        this.description = description;
        this.expiryDate = expiryDate;
    }

    public String getExpiryDate() {
        return expiryDate;
    }

    public void setExpiryDate(String expiryDate) {
        this.expiryDate = expiryDate;
    }

    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;
    }

    public String getTitle() {

        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

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

Problem is i am unable to set image in Adpater Image View. My layout screen is as:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:paddingBottom="10dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="10dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="@string/app_name"
        android:textSize="18sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/img_cashback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:contentDescription="@string/app_name"
        android:src="@drawable/freerecharge" />

    <TextView
        android:id="@+id/txt_description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_cash_back"
        android:layout_alignStart="@+id/btn_cash_back"
        android:layout_below="@id/img_cashback"
        android:layout_marginBottom="20dp"
        android:text="@string/txt_name_demo"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/expiry_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_cash_back"
        android:layout_alignStart="@+id/btn_cash_back"
        android:layout_below="@id/txt_description"
        android:layout_marginBottom="20dp"
        android:text="@string/txt_date_demo"
        android:textSize="16sp" />

    <Button
        android:id="@id/btn_cash_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/expiry_date"
        android:layout_centerInParent="true"
        android:background="@color/colorLine"
        android:paddingEnd="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingStart="20dp"
        android:text="@string/txt_cash_back"
        android:textColor="@color/colorWhite"
        android:textSize="18sp" />

</RelativeLayout>


why dont you use Picasso to load image. willl make your work easy.

load Asset folder images like this

Picasso.with(context).load("file:///android_asset/"+data.getImageName()).into(holder.img_cashback);

put that in onBindViewHolder of your recycleview adapter.

source from here