当我尝试将Image URL解析到ImageView中时,回收器视图不显示,活动为空。我正在使用Picasso将图像加载到适配器类中的onBinfViewHolder方法中。这是相关代码
代表:
public class Reps {
private String officeName;
private String officialName;
private String party;
private String photoUrl;
//continue to develop this with other values such as phone, photo, address etc.
public Reps(String officeName, String officialName, String party, String photoUrl) {
this.officeName = officeName;
this.officialName = officialName;
this.party = party;
this.photoUrl = photoUrl;
}
public Reps(String officeName, String officialName, String party) {
this.officeName = officeName;
this.officialName = officialName;
this.party = party;
}
public Reps(String officeName) {
this.officeName = officeName;
}
public Reps(String name, String party) {
this.officialName = name;
this.party = party;
}
public String getOfficeName() {
return officeName;
}
public void setOfficeName(String officeName) {
this.officeName = officeName;
}
public String getOfficialName() {
return officialName;
}
public void setOfficialName(String officialName) {
this.officialName = officialName;
}
public String getParty() {
return party;
}
public void setParty(String party) {
this.party = party;
}
public String getPhotoUrl() {
return photoUrl;
}
public void setPhotoUrl(String photoUrl) {
this.photoUrl = photoUrl;
}
}
RepRvAdapter:
public class RepRvAdapter extends
RecyclerView.Adapter<RepRvAdapter.MyViewHolder> {
private Context context;
private List<Reps> repsList;
RequestOptions option;
public RepRvAdapter() {
}
public RepRvAdapter(List<Reps> repList, Context applicationContext) {
this.repsList = repList;
this.context = applicationContext;
}
@NonNull
@Override
public RepRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view;
LayoutInflater mInflater = LayoutInflater.from(context);
view = mInflater.inflate(R.layout.card, viewGroup, false); //Inflate view to card.xml
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RepRvAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.officeName.setText(repsList.get(i).getOfficeName()); //Display these three elements in the card view
myViewHolder.officialName.setText(repsList.get(i).getOfficialName());
myViewHolder.party.setText(repsList.get(i).getParty());
//load image into card view
Picasso.get()
.load(repsList.get(i).getPhotoUrl())
.centerCrop()
.transform(new CircleTransform(50, 0))
.fit()
.into(myViewHolder.photoUrl);
}
@Override
public int getItemCount() {
return repsList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
//Variables
TextView officeName, officialName, party; //Only the Referendum title and type will be in the recycler view
ImageView photoUrl;
public MyViewHolder(View itemView){
super(itemView);
officeName = itemView.findViewById(R.id.office_name);
officialName = itemView.findViewById(R.id.official_name);
party = itemView.findViewById(R.id.party);
photoUrl = itemView.findViewById(R.id.photo_url);
}
}
}
解析JSON数据的方法:
//fetch json data from Civic API
private void getData(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try{
JSONObject jsonObject = new JSONObject(response);
//first loop through offices array. Retrieve officeName value
JSONArray officesArray = jsonObject.getJSONArray("offices"); //One array for offices
JSONArray officialsArray = jsonObject.getJSONArray("officials"); //one array for officials
for (int i = 0; i < officesArray.length(); i++){
JSONObject jsonOffices = officesArray.getJSONObject(i);
JSONObject jsonOfficials = officialsArray.getJSONObject(i);
Reps reps = new Reps (jsonOfficials.getString("name"),
jsonOfficials.getString("party"),
//jsonOfficials.getString("photoUrl")
jsonOffices.getString("name"));
repList.add(reps);
}
adapter = new RepRvAdapter(repList, getApplicationContext());
myrv.setAdapter(adapter);
}catch (JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
现在,我有一行解析图像URL的代码被注释掉了。行取消注释后,活动将不再显示。如何获取要在ImageView中解析和显示的图像url?我认为这可能是JSON数据的问题。在JSON数组中,并非所有photoUrl字段都已设置。API可能不会返回数组中每个代表的图像数据,而party、name和office字段始终具有值。这可能是阻止“回收器”视图显示的原因吗?以下是相关的JSON数据块:
"officials": [
{
"name": "Donald J. Trump",
"address": [
{
"line1": "The White House",
"line2": "1600 Pennsylvania Avenue NW",
"city": "Washington",
"state": "DC",
"zip": "20500"
}
],
"party": "Republican",
"phones": [
"(202) 456-1111"
],
"photoUrl":"https://www.whitehouse.gov/sites/whitehouse.gov/files/images/45/PE%20Color.jpg",
"channels": [
{
"type": "GooglePlus",
"id": "+whitehouse"
},
{
"type": "Facebook",
"id": "whitehouse"
},
{
"type": "Twitter",
"id": "potus"
},
{
"type": "YouTube",
"id": "whitehouse"
}
]
},
{
"name": "Mike Pence",
"address": [
{
"line1": "The White House",
"line2": "1600 Pennsylvania Avenue NW",
"city": "Washington",
"state": "DC",
"zip": "20500"
}
],
"party": "Republican",
"phones": [
"(202) 456-1111"
],
"photoUrl":"https://www.whitehouse.gov/sites/whitehouse.gov/files/images/45/VPE%20Color.jpg",
"channels": [
{
"type": "GooglePlus",
"id": "+whitehouse"
},
{
"type": "Facebook",
"id": "whitehouse"
},
{
"type": "Twitter",
"id": "VP"
}
]
},
{
"name": "Dianne Feinstein",
"address": [
{
"line1": "331 Hart Senate Office Building",
"city": "Washington",
"state": "DC",
"zip": "20510"
}
],
"party": "Democratic",
"phones": [
"(202) 224-3841"
],
"photoUrl": "http://bioguide.congress.gov/bioguide/photo/F/F000062.jpg",
"channels": [
{
"type": "Facebook",
"id": "SenatorFeinstein"
},
{
"type": "Twitter",
"id": "SenFeinstein"
},
{
"type": "YouTube",
"id": "SenatorFeinstein"
}
]
},
{
"name": "Kamala D. Harris",
"address": [
{
"line1": "112 Hart Senate Office Building",
"city": "Washington",
"state": "DC",
"zip": "20510"
}
],
"party": "Democratic",
"phones": [
"(202) 224-3553"
],
"channels": [
{
"type": "Twitter",
"id": "KamalaHarris"
},
{
"type": "YouTube",
"id": "UC0XBsJpPhOLg0k4x9ZwrWzw"
}
]
我愿意尝试不同的图书馆。如果您需要查看更多代码,请告诉我。谢谢你的帮助!
我在jsonParse方法中添加了以下条件,以说明没有photoUrl字段的对象。
try{
JSONObject jsonObject = new JSONObject(response);
//first loop through offices array. Retrieve officeName value
JSONArray officesArray = jsonObject.getJSONArray("offices"); //One array for offices
JSONArray officialsArray = jsonObject.getJSONArray("officials"); //one array for officials
for (int i = 0; i < officesArray.length(); i++){
JSONObject jsonOffices = officesArray.getJSONObject(i);
JSONObject jsonOfficials = officialsArray.getJSONObject(i);
if(jsonOfficials.has("photoUrl")){
Reps reps = new Reps (jsonOfficials.getString("name"),
jsonOfficials.getString("party"),
jsonOfficials.getString("photoUrl"),
jsonOffices.getString("name"));
repList.add(reps);
}else{
Reps reps = new Reps(jsonOfficials.getString("name"),
jsonOfficials.getString("party"),
jsonOffices.getString("name"));
repList.add(reps);
}
}
adapter = new RepRvAdapter(repList, getApplicationContext());
myrv.setAdapter(adapter);
由于最后一位官员没有可用的图像,因此他们的数据显示正确。
JSON响应中存在错误。试试这个
[{
"name": "Donald J. Trump",
"address": [{
"line1": "The White House",
"line2": "1600 Pennsylvania Avenue NW",
"city": "Washington",
"state": "DC",
"zip": "20500"
}],
"party": "Republican",
"phones": [
"(202) 456-1111"
],
"photoUrl": "https://www.whitehouse.gov/sites/whitehouse.gov/files/images/45/PE%20Color.jpg",
"channels": [{
"type": "GooglePlus",
"id": "+whitehouse"
},
{
"type": "Facebook",
"id": "whitehouse"
},
{
"type": "Twitter",
"id": "potus"
},
{
"type": "YouTube",
"id": "whitehouse"
}
]
},
{
"name": "Mike Pence",
"address": [
{
"line1": "The White House",
"line2": "1600 Pennsylvania Avenue NW",
"city": "Washington",
"state": "DC",
"zip": "20500"
}
],
"party": "Republican",
"phones": [
"(202) 456-1111"
],
"photoUrl":"https://www.whitehouse.gov/sites/whitehouse.gov/files/images/45/VPE%20Color.jpg",
"channels": [
{
"type": "GooglePlus",
"id": "+whitehouse"
},
{
"type": "Facebook",
"id": "whitehouse"
},
{
"type": "Twitter",
"id": "VP"
}
]
},
{
"name": "Dianne Feinstein",
"address": [
{
"line1": "331 Hart Senate Office Building",
"city": "Washington",
"state": "DC",
"zip": "20510"
}
],
"party": "Democratic",
"phones": [
"(202) 224-3841"
],
"photoUrl": "http://bioguide.congress.gov/bioguide/photo/F/F000062.jpg",
"channels": [
{
"type": "Facebook",
"id": "SenatorFeinstein"
},
{
"type": "Twitter",
"id": "SenFeinstein"
},
{
"type": "YouTube",
"id": "SenatorFeinstein"
}
]
},
{
"name": "Kamala D. Harris",
"address": [
{
"line1": "112 Hart Senate Office Building",
"city": "Washington",
"state": "DC",
"zip": "20510"
}
],
"party": "Democratic",
"phones": [
"(202) 224-3553"
],
"channels": [
{
"type": "Twitter",
"id": "KamalaHarris"
},
{
"type": "YouTube",
"id": "UC0XBsJpPhOLg0k4x9ZwrWzw"
}
]
}
]
问题#1:当您尝试解析“photoUrl”时发生JSON异常,因为在最后一个对象中不存在名为“photoUrl”的字段。
{
"name": "Kamala D. Harris",
"address": [
{
"line1": "112 Hart Senate Office Building",
"city": "Washington",
"state": "DC",
"zip": "20510"
}
],
"party": "Democratic",
"phones": [
"(202) 224-3553"
],
"channels": [
{
"type": "Twitter",
"id": "KamalaHarris"
},
{
"type": "YouTube",
"id": "UC0XBsJpPhOLg0k4x9ZwrWzw"
}
]
}
解决方案#1:只需将代码放在如下条件中:
String photoUrl = "";
if (jsonOfficials.has("photoUrl")) {
photoUrl = jsonOfficials.getString("photoUrl");
}
问题#2:请检查您的“代表”类第一构造函数。在这里,你得到"photoUrl"feild作为第4个参数,但是当你创建一个"Reps"类的对象时,你传递"photoUrl"feild作为第3个参数。
解决方案#2:
Reps reps = new Reps (jsonOfficials.getString("name"),
jsonOfficials.getString("party"),
jsonOffices.getString("name"),
jsonOfficials.getString("photoUrl"));
我试图显示图像从Firebase数据库中的回收视图在Android Studio,但图像不显示。我不确定我哪里出错了,也不知道问题出在哪里。 主Activity.xml 标志和物品。xml 标志tems.java 主要活动。JAVA LogoItemsAdapter。JAVA
我正在使用volley向tmdb数据库发出RESTAPI请求,以加载有关电影的信息。回收器视图应显示缩略图图像海报。但它没有显示任何东西。我正在使用毕加索图书馆加载图像。图像链接构造得很好,我已经用log语句检查过了 主要活动。xml 列出项目布局。xml 主要活动 电影数据加载器
我刚开始在firebase工作。我设法上传了文本和图像,但是,我无法检索要显示在回收器视图中的图像,只能检索文本。我用的是毕加索依赖。我已经包括了我的主要活动。java类,该类负责显示从问题“我的适配器”中的firebase检索的回收器视图项。java类和模型类。我相信,在我将图像URI上载到firebase存储时,我可能犯了没有存储图像URI的错误,因此适配器无法检索图像位置。我想这可能是因为我
我正在尝试在我的 上实现 ,但我没有得到任何结果。 我遵循了这个教程和这个技巧,但是没有人为我工作。 我已经实现了: 但它不让我编译,因为它们是不同的< code > viewmoder ,因为我创建了两个< code > viewmoder 类,但它们< code >扩展了Recycler。ViewHolder所以我不明白... 我正在尝试这样做,因为我有一个,我希望当列表为空时,它会显示一个,
使用Android Universal Image Loader和RecyclerView异步加载图像,我会遇到与其他人相同的错误,图像会混淆;直到它们都加载了一个缓存。 适配器的代码: 我知道它必须是中的某个东西,因为它需要更新每个视图,但我没有正确地更新。 这与图书馆无关。在不缓存图像的情况下执行延迟加载时,也会发生相同的行为。错误是因为我不知道如何更新中的。 谢谢!
我正在使用毕加索图书馆。我知道,如果我将图片从URL加载到图像视图中,有一种回调方法。但我不想加载到imageview中。相反,我想将其另存为位图。所以我用了下面的代码 我怎样才能得到一个回调方法,知道我的图像是成功下载使用毕加索? 不要说位图对象的空检查。这会导致错误。