在我的应用中,我成功地从网络服务器中检索了一系列图像。它们被读作网址即
http://mw2.google.com/mw-panoramio/photos/medium/131093147.jpg
等等。
我将图像存储在SQLite中作为字符串URL。但现在我想在Recyclerview中显示它们。
这是我的适配器。
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);
}
}
}
Ι将onBindViewHolder中的其余数据显示为
holder.fName.setText(myList.get(position).getSqliteFirstName());
holder.lName.setText(myList.get(position).getSqliteSecondName());
holder.jobRole.setText(myList.get(position).getSqliteRole());
我想我需要将URL String转换为该方法中的字节数组。我不确定。有什么想法吗?
还有别的。当我点击recyclerview的行时,我得到以下异常。
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)
这就是我的recyclerview没有图像的样子。
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();
}
}
答案 0 :(得分:1)
使用 Picasso
这是一个非常简单的库,允许您下载和缓存图像。
所以在你的情况下你应该写一些类似的东西:
Picasso.with(mContext)
.load(myList.get(position).getSqliteImage())
.into(holder.imageView);
答案 1 :(得分:0)
你有NetworkImageView,目前使用的越来越少。我建议您使用Picasso库将其更改为标准ImageView +,然后您可以通过以下方式加载图像:
Picasso.with(holder.imageView.getContext()).load(myList.get(position).getUrl()).into(holder.imageView);
答案 2 :(得分:0)
感谢 Mateusz Pryczkowski
图片的答案是:
Picasso.with(holder.imageView.getContext())
.load(myList.get(position).getSqliteImage()).into(holder.imageView);