Theo Theo - 5 months ago 20
Android Question

Display URL image in sqlite

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


}

Answer

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);
Comments