Java 将图像从JSON URl加载到listview中的ImageView
我试图获取一个返回URL的JSON,然后我想通过Java 将图像从JSON URl加载到listview中的ImageView,java,android,json,lazy-loading,Java,Android,Json,Lazy Loading,我试图获取一个返回URL的JSON,然后我想通过ArrayList将该URL图像加载到ListView中的ImageView。我想通过AsyncTask延迟加载图像,这样它就不会影响我的ListView如果它没有被加载,而且,如果图像不可用,我想从drawable设置一个图像。这是你的电话号码。其中,有一个名为海报的对象,其中有一个名为原始的对象。我希望将此原始海报加载到此布局中名为poster的图像视图中 <?xml version="1.0" encoding="utf-8"?>
ArrayList
将该URL图像加载到ListView
中的ImageView
。我想通过AsyncTask
延迟加载图像,这样它就不会影响我的ListView
如果它没有被加载,而且,如果图像不可用,我想从drawable设置一个图像。这是你的电话号码。其中,有一个名为海报的对象,其中有一个名为原始的对象。我希望将此原始海报加载到此布局中名为poster的图像视图中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left side Thumbnail image -->
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="@+id/list_image"
android:layout_width="60dip"
android:layout_height="60dip"
android:contentDescription="@string/app_name"
android:src="@drawable/poster" />
</LinearLayout>
<!-- Rightend Arrow -->
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:contentDescription="@string/app_name"
android:src="@drawable/arrow" />
<!-- City -->
<TextView
android:id="@+id/namelisttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/list_image"
android:layout_marginLeft="75dip"
android:gravity="top"
android:paddingBottom="10dip"
android:text="Movie Name"
android:textColor="#040404"
android:textSize="25sp"
android:textStyle="bold"
android:typeface="sans" />
<!-- Weather Information -->
<ImageView
android:id="@+id/rtscore"
android:layout_width="18dip"
android:layout_height="18dip"
android:layout_alignLeft="@id/namelisttext"
android:layout_below="@id/namelisttext"
android:contentDescription="@string/app_name"
android:src="@drawable/rtscore" />
<TextView
android:id="@+id/rtscoretext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_toRightOf="@+id/rtscore"
android:layout_alignTop="@id/rtscore"
android:text="Score"
android:textColor="#343434"
android:textSize="15sp" />
<ImageView
android:id="@+id/audscore"
android:layout_width="18dip"
android:layout_height="18dip"
android:layout_below="@id/namelisttext"
android:layout_toRightOf="@id/rtscoretext"
android:layout_marginLeft="7dp"
android:contentDescription="@string/app_name"
android:src="@drawable/audscore" />
<TextView
android:id="@+id/audscoretext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_toRightOf="@+id/audscore"
android:layout_alignTop="@id/audscore"
android:text="Score"
android:textColor="#343434"
android:textSize="15sp" />
<TextView
android:id="@+id/castlisttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/namelisttext"
android:layout_below="@id/rtscore"
android:layout_toLeftOf="@+id/releaseyearlisttext"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:singleLine="true"
android:text="Casts"
android:textColor="#343434"
android:textSize="15sp" />
<TextView
android:id="@+id/releaseyearlisttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/rtscore"
android:layout_marginRight="7dp"
android:paddingTop="5dip"
android:text="Year"
android:textColor="#343434"
android:textSize="15sp" />
</RelativeLayout>
请帮助我如何在
ArrayList
中插入此URL,然后在ListView
中延迟加载图像。您应该在android中使用延迟加载的概念
首先,您需要解析JSON响应,获取原始的键url并设置到arraylist中
查看将从url加载图像并显示到列表中的
按如下方式解析JSON响应:
JSONObject jObj=new JSONObject("<response>");
JSONArray jArray=jObj.getJSONArray("movies");
ArrayList<String> urlList=new ArrayList<String>();
for(int i=0;i<jArray.lenght();i++)
{
JSONObject jo_inside = ja.getJSONObject(i);
String originalUrl=jo_inside.getJSONObject("posters").get("original").toString();
urlList.add(originalUrl);
}
JSONObject jObj=新的JSONObject(“”);
JSONArray jArray=jObj.getJSONArray(“电影”);
ArrayList urlList=新的ArrayList();
对于(inti=0;i来说,真正简化您所寻找内容的一种方法是使用谷歌自己提供的网络库“volley”。
请看下面的视频,其中他们讨论了该库的几个功能
该库为您处理大量样板代码,并以异步方式完成所有工作。您应该能够获取json数据,获取原始海报url,然后将该url设置为图像源。然后,Volley将自动异步加载该图像。访问并使用AsyncTask从中下载数据json和onPostExecute将该数据设置为listadapter。