0

In my app,I sucessfully retrieve from a webserver a list of images. They are read as urls ie

http://mw2.google.com/mw-panoramio/photos/medium/131093147.jpg

and so on.

I store the images in the SQLite as string urls. But now I want to display them in a recyclerview.

This is my adapter.

public class MySQLAdapter extends RecyclerView.Adapter<MySQLAdapter.RowHolderClass> {

private List<SQLiteModel> myList;
private Context mContext;
private int focused;


public MySQLAdapter(Activity activity,ArrayList<SQLiteModel> ss) {
    this.myList = ss;
    this.mContext=activity;
}

@Override
public MySQLAdapter.RowHolderClass onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_row, null);

    final RowHolderClass holder = new RowHolderClass(v);
    return holder;
}

@Override
public void onBindViewHolder(RowHolderClass holder, int position) {

    final SQLiteModel listItems = myList.get(position);
    holder.itemView.setSelected(focused==position);

    holder.getLayoutPosition();

    holder.fName.setText(myList.get(position).getSqliteFirstName());
    holder.lName.setText(myList.get(position).getSqliteSecondName());
    holder.jobRole.setText(myList.get(position).getSqliteRole());
    //holder.imageView.setImageResource(Integer.parseInt(myList.get(position).getSqliteImage()));
    holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String first_name = listItems.getSqliteFirstName();
            String last_name = listItems.getSqliteSecondName();
            String job_role = listItems.getSqliteRole();
            //String image_url = listItems.get();
            //String id= String.valueOf(listItems.getSqliteId());
            //Log.v("id",id);
            Intent i = new Intent(mContext, NoInternetDetailedActivity.class);
            i.putExtra("firstName",first_name);
            i.putExtra("lastName",last_name);
            i.putExtra("jobRole",job_role);
            //i.putExtra("imageUrl",image_url);
            //i.putExtra("memberId",id);
            mContext.startActivity(i);
        }
    });

}


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


//ViewHolder pattern used to read the data of the row fast.
class RowHolderClass extends RecyclerView.ViewHolder {

    protected NetworkImageView imageView;
    protected TextView fName, lName, jobRole;
    protected RelativeLayout relativeLayout;

    public RowHolderClass(View itemView) {
        super(itemView);

        this.relativeLayout = (RelativeLayout) itemView.findViewById(R.id.recLayout);
        this.imageView = (NetworkImageView) itemView.findViewById(R.id.imageView);
        this.fName = (TextView) itemView.findViewById(R.id.firstName);
        this.lName = (TextView) itemView.findViewById(R.id.lastName);
        this.jobRole = (TextView) itemView.findViewById(R.id.roleView);

    }
  }
 }

Ι display the rest of the data inside the onBindViewHolder as

holder.fName.setText(myList.get(position).getSqliteFirstName());
holder.lName.setText(myList.get(position).getSqliteSecondName());
holder.jobRole.setText(myList.get(position).getSqliteRole());

I think I need to convert the URL String into a byte array inside that method. I am not sure. Any ideas?

And something else. When I click on recyclerview's row I get the following exception.

 java.lang.NullPointerException: Attempt to invoke virtual method 
'java.lang.String android.content.Context.getPackageName()' on a null object   
 reference

 at android.content.ComponentName.<init>(ComponentName.java:77)

This is how my recyclerview looks like without the image.

myimage

  public void fetchDataFromDB() {

    dba = new DatabaseHandler(getApplicationContext());

    ArrayList<SQLiteModel> membersDB = dba.getMembersDetails();

    for(int i=0; i<membersDB.size();i++){
        int id = membersDB.get(i).getSqliteId();
        String fname = membersDB.get(i).getSqliteFirstName();
        String lname = membersDB.get(i).getSqliteSecondName();
        String role = membersDB.get(i).getSqliteRole();
        String image = membersDB.get(i).getSqliteImage();

        //String desc = moviesFromDB.get(i).getContent();
        SQLiteModel f = new SQLiteModel();
        f.setSqliteId(id);
        f.setSqliteFirstName(fname);
        f.setSqliteSecondName(lname);
        f.setSqliteRole(role);
        f.setSqliteImage(image);
        myarr.add(f);

    }
    dba.close();
}

}

Theo
  • 3,099
  • 12
  • 53
  • 94
  • You have there NetworkImageView, that is currently less and less used. I would suggest you to change this to standard ImageView + using Picasso library, then you can load image by: Picasso.with(getContext()).load(myList.get(position).getUrl()).into(holder.imageView); – Mateusz Pryczkowski Jun 23 '16 at 07:28
  • I get this exception. java.lang.IllegalArgumentException: Context must not be null. – Theo Jun 23 '16 at 07:45
  • 1
    Try: Picasso.with(holder.image‌​View.getContext()).load(myList.get(position).getUrl()).into(holder.image‌​View); – Mateusz Pryczkowski Jun 23 '16 at 07:49
  • yes!!!!!!! Thanks for that!!! – Theo Jun 23 '16 at 07:53
  • 1
    np. Added my comment as answer – Mateusz Pryczkowski Jun 23 '16 at 07:57
  • Cool. And something else. I get an exception,when I click on a row as shown in my question. java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.content.ComponentName.(ComponentName.java:77) – Theo Jun 23 '16 at 08:00

3 Answers3

1

Use Picasso

This is very simple library which allows you to download and cache you images.

So in you case you should write something like:

Picasso.with(mContext)
.load(myList.get(position).getSqliteImage())
.into(holder.imageView);
nutella_eater
  • 3,393
  • 3
  • 27
  • 46
  • Yes I know picasso,I use sqlite to load the data in the recycler view when the internet is off. – Theo Jun 23 '16 at 07:37
  • I think picaso can handle caching. You said that you store your images as urls, not bitmap. Also, consider this http://stackoverflow.com/questions/9357668/how-to-store-image-in-sqlite-database – nutella_eater Jun 23 '16 at 07:41
  • ok. And something else. When I click a row I get an exception described in my question. – Theo Jun 23 '16 at 07:44
  • 1
    try to use debug mode to trace why you context is null. It's not clear for now what is causing the issue. – nutella_eater Jun 23 '16 at 11:28
  • It's ok. Problem fixed:) – Theo Jun 24 '16 at 06:50
0

You have there NetworkImageView, that is currently less and less used. I would suggest you to change this to standard ImageView + using Picasso library, then you can load image by:

Picasso.with(holder.image‌​View.getContext()).load(myList.get(position).getUrl()‌​).into(holder.image‌​View);
Mateusz Pryczkowski
  • 1,874
  • 1
  • 16
  • 20
0

Thanks to Mateusz Pryczkowski

The answer for the images is:

 Picasso.with(holder.imageView.getContext())
            .load(myList.get(position).getSqliteImage()).into(holder.imageView);
Theo
  • 3,099
  • 12
  • 53
  • 94