Java 无法从url获取图像
我需要从url获取图像,并将其设置在listview中 这是我的主要xml文件:Java 无法从url获取图像,java,android,Java,Android,我需要从url获取图像,并将其设置在listview中 这是我的主要xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:bac
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<ScrollView
android:id="@+id/scrollFeed"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="40sp"
android:background="#d2d2d2"
android:clickable="true"
android:gravity="center"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:text="^\nRefresh" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="15000sp" >
</ListView>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="100"
>
<TextView
android:id="@+id/nextPosts"
android:layout_width="match_parent"
android:layout_height="40sp"
android:background="#d2d2d2"
android:clickable="true"
android:gravity="center"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:text="next >>"
android:layout_weight="50" />
<TextView
android:id="@+id/previousPosts"
android:layout_width="match_parent"
android:layout_height="40sp"
android:background="#d2d2d2"
android:clickable="true"
android:gravity="center"
android:layout_weight="50"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:text=" previous" />
</LinearLayout>
</LinearLayout>
</ScrollView>
使用默认适配器无法实现此功能。您需要为listview创建自定义适配器 在适配器的getView方法中,您需要膨胀列表项布局,然后必须将图像绑定到图像视图
例如,您可以参考1。您没有显示初始化或声明“image”元素的位置。2.xml文件中也没有“图像”。它在onCreate方法中作为ImageView图像;image=(ImageView)findViewById(R.id.imageView1);,我还尝试在doInBackground()方法中初始化它,但结果是一样的什么是错误?还要检查您是否已初始化适配器中的图像视图列表项中是否有图像视图。xml@Fahim-我不确定如何在适配器中初始化ImageView。我的适配器是这样的:ListAdapter=newSimpleAdapter(NewsFeed.this,contactList,R.layout.list_项,新字符串[]{TAG_MESSAGE,TAG_CREATETIME},new int[]{R.id.MESSAGE,R.id.time});setListAdapter(适配器);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#abc0e3"
android:gravity="right"
android:text="TextView"
android:textColor="#b1b1b1" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:autoLink="web"
android:linksClickable="true"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:text="TextView" />
if (c.has("picture") == true) {
try {
Log.d("IT HAS PICTURE! TADA!!", "Image Found");
imgUrl = c.getString("picture");
// Thread thread = new Thread(){
// public void run(){
System.out.println("Thread Running");
NewsFeed.this.runOnUiThread(new Runnable(){
@Override
public void run() {
//Your code to run in GUI thread here
URL newUrl = null;
try {
newUrl = new URL(imgUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myBitmap = getBitmapFromUrl(newUrl);
image.setImageBitmap(myBitmap);
}
});
// }
// };
thread.start();
} catch (Exception e) {
Log.d("ERROR LOADING IMAGE","Why you do this, InputStream? Why?");
}
}else{
NewsFeed.this.runOnUiThread(new Runnable(){
@Override
public void run() {
//Your code to run in GUI thread here
image.setImageResource(R.drawable.cover);
}
});
}