我正在使用Gson和Universal Image Loader。 我使用这个项目就像一个模板(你需要更改版本来运行它,它在教程中显示): 教程:https://www.youtube.com/watch?v=X2mY3zfAMfM 链接到此项目:https://github.com/hishamMuneer/JsonParsingDemo
我在JSON的嵌套部分中添加了图像,并在第二个活动中创建了listView以显示它。
{
"movies": [
{
"movie": "Avengers",
"cast": [
{
"name": "Robert Downey Jr.",
"image": "www.mywebsite.com/img2.png"
},
{
"name": "Chris Evans",
"image": "www.mywebsite.com/img3.png"
},
{
"name": "Mark Ruffalo",
"image": "www.mywebsite.com/img4.png"
}
],
"image": "https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/images/avengers.jpg"
},
{
"movie": "Interstellar",
"cast": [
{
"name": "Robert Downey Jr.",
"image": "www.mywebsite.com/img5.png"
},
{
"name": "Chris Evans",
"image": "www.mywebsite.com/img6.png"
},
{
"name": "Mark Ruffalo",
"image": "www.mywebsite.com/img7.png"
}
],
"image": "https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/images/interstellar.jpg"
},...
]
}
第一个listView正常工作。第二,我有错误。 我试过三种方法: 1.使用BaseAdapter: Withouth图像使用textView正确填充第二个listView,并在详细活动中显示每个图像。所以三个活动工作正常(第二个没有图像)。 但是当我尝试使用图像填充ListView时,我有错误: 一个。使用默认方式: 解析时出现问题我在logcat中看到了正确的图像网址,以及在进行调试时的可能性,但在此之后存档错误。
’FATAL EXCEPTION: main
java.lang.NumberFormatException: For input string: ”http://example.com/image14.png”
at java.lang.Integer.parseInt(Integer.java:608)
at java.lang.Integer.parseInt(Integer.java:643)
at ….ListAdapter.getView(ListActivity)
湾使用通用图像加载器。 ,尝试在空对象引用上调用虚方法'void android.widget.ProgressBar.setVisibility(int)' 无法使用null
初始化ImageLoader配置请问你能帮帮我吗?
public class MovieModel {
private String name;
private String imgMovie;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImgMenu() {
return imgMovie;
}
public void setImgMenu(String imgMenu) {
this.imgMovie = imgMenu;
}
@SerializedName("description")
private List<Descriptions> descriptions;
public List<Descriptions> getDescriptionsList() {
return descriptions;
}
public void setDescriptionsList(List<Descriptions> descriptions) {
this.descriptions = descriptions;
}
public static class Descriptions {
private String title;
private String image;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
}
public class ListActivity extends AppCompatActivity {
private ProgressBar progresBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
ListView listView = (ListView) findViewById(R.id.listView);
// getting the model from MainActivity send via extras
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String json = bundle.getString("movieModel");
MovieModel movieModel = new Gson().fromJson(json, MovieModel.class);
final List<MovieModel.Descriptions> result = movieModel.getDescriptionsList();
GridAdapter gridAdapter = new GridAdapter(getApplicationContext(), result);
listView.setAdapter(gridAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MovieModel.Descriptions descriptionsModel = result.get(position);
Intent intent = new Intent(ListActivity.this, DetailActivity.class);
intent.putExtra("descriptionsModel", new Gson().toJson(descriptionsModel));
startActivity(intent);
}
});
}
}
public class GridAdapter extends BaseAdapter {
private LayoutInflater inflater;
private Context context;
private int resource;
private List<MovieModel.Descriptions> items; // You hate to use detailList
public GridAdapter(Context context, List<MovieModel.Descriptions> items) {
this.context = context;
this.items = items;
inflater = (LayoutInflater.from(context));
}
@Override
public int getCount() {
return items.size(); //returns total of items in the list
}
@Override
public Object getItem(int position) {
return items.get(position); //returns list item at the specified position
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.list, null);
// Display text correctly when all below (with image) is comment
TextView listTitle = (TextView) convertView.findViewById(R.id.text);
listTitle.setText(items.get(position).getTitle());
// 1a -Shows error because Parse when all bellow is comment
ImageView listImg = (ImageView) convertView.findViewById(R.id.image);
listImg.setImageResource(Integer.parseInt(items.get(position).getImage()));
// 1b - ImageLoader configuration can not be initialized with null
final ProgressBar progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
ImageLoader.getInstance().displayImage(items.get(position).getImage(), listImg , new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
progressBar.setVisibility(View.GONE);
}
});
return convertView;
}
}
activity_list
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xtech.baselistview.ListActivity">
<GridView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"
/>
</RelativeLayout>
list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center" />
</LinearLayout>
使用ArrayAdapter的第三种方法
public class ListActivity extends AppCompatActivity {
private ProgressBar progresBar;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listView = (ListView) findViewById(R.id.listView);
// getting the model from MainActivity send via extras
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String json = bundle.getString("movieModel");
MovieModel movieModel = new Gson().fromJson(json, MovieModel.class);
List<MovieModel.Descriptions> result = movieModel.getDescriptionsList();
ListAdapter adapter = new ListAdapter(getApplicationContext(), R.layout.list, result);
listView.setAdapter(adapter);
}
}
public class ListAdapter extends ArrayAdapter {
private LayoutInflater inflater;
private Context context;
private int resource;
private List<MovieModel.Descriptions> items;
public ListAdapter(Context context, int resource, List<MovieModel.Descriptions> items) {
super(context, resource, items);
this.context = context;
this.resource = resource;
this.items = items;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items.size(); //returns total of items in the list
}
@Override
public Object getItem(int position) {
return items.get(position); //returns list item at the specified position
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.listImg = (ImageView) convertView.findViewById(R.id.image);
holder.listTitle = (TextView) convertView.findViewById(R.id.text);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ProgressBar progressBar = (ProgressBar)convertView.findViewById(R.id.progressBar);
final ViewHolder finalHolder = holder;
ImageLoader.getInstance().displayImage(items.get(position).getImage(), holder.listImg, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
progresBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progresBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progresBar.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
progresBar.setVisibility(View.GONE);
}
});
holder.listTitle.setText(items.get(position).getTitle()); //
return convertView;
}
}
class ViewHolder {
private ImageView listImg;
private TextView listTitle;
}
}
答案 0 :(得分:0)
我会假设您在DetailActivity中通过意图传递了正确的URL:
String flag;
flag = i.getStringExtra("flag");
flagImg = (ImageView)findViewById(R.id.detailImg);
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(flag, flagImg);