Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android-将多个图像从url添加到listview_Java_Android_Xml_Json_Listview - Fatal编程技术网

Java android-将多个图像从url添加到listview

Java android-将多个图像从url添加到listview,java,android,xml,json,listview,Java,Android,Xml,Json,Listview,在我的应用程序中,我有一个包含上传视频的列表视图,我是从JSON获得的。 JSON还向我返回视频图像的url。 现在我正试图将该图像设置到xml布局listview的imageview中,但它只为第一个listview项加载了最后一个图像 以下是XML文件: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.a

在我的应用程序中,我有一个包含上传视频的列表视图,我是从JSON获得的。 JSON还向我返回视频图像的url。 现在我正试图将该图像设置到xml布局listview的imageview中,但它只为第一个listview项加载了最后一个图像

以下是XML文件:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="460dp" >

            <ListView
                android:id="@android:id/list"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </ListView>

        </RelativeLayout>

    </LinearLayout>

</FrameLayout> 

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#808080"
    android:choiceMode="singleChoice"
    android:divider="#DCDCDC"
    android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>

以下是每个listview项的XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical" >

    <ImageView
        android:id="@+id/videoImage"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginTop="5dp"
        android:maxHeight="70dp"
        android:maxWidth="70dp" />

    <TextView
        android:id="@+id/videoURL"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textSize="17sp"
        android:textStyle="bold"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/videoTitle"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_toRightOf="@+id/videoImage" />

    <TextView
        android:id="@+id/videoViews"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/videoURL"
        android:layout_marginTop="0dp"
        android:layout_toRightOf="@+id/videoImage" />

    <ImageView
        android:id="@+id/arrow"
        android:layout_width="18dp"
        android:layout_height="22dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:gravity="center_vertical"
        android:src="@drawable/arrow" />

</RelativeLayout>

现在是加载视频的java类和负责视频图像的类:

class LoadVideosFromDB extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(onlineVideos.this);
        pDialog.setMessage(getString(R.string.loadingVideos));
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... args) {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("token", token));

        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(getAllVideosURL, "POST",
                params);

        // Check your log cat for JSON response
        Log.d("All Videos: ", json.toString());

        try { // Trying to get videos

            // Getting Array of videos
            videos = json.getJSONArray("videos");

            // Looping through all videos
            for (int i = 0; i < videos.length(); i++) {

                JSONObject c = videos.getJSONObject(i);

                // Storing each json item in variable
                videoID = c.getString("videoID");
                videoTitle = c.getString("videoTitle");
                videoViews = c.getString("videoViews");
                videoDownloads = c.getString("videoDownloads");
                videoHashTag = c.getString("videoHashTag");
                videoNices = c.getString("videoNices");
                videoNok = c.getString("videoNok");
                videoThumbPath = "https://dev.oncom.ch/upload/thumbs/"
                        + c.getString("videoThumbPath");
                videoPath = "https://dev.oncom.ch/upload/videos/"
                        + c.getString("videoPath");
                videoCategorieID = c.getString("videoCategorieID");
                videoComments = c.getString("videoComments");

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put("videoTitle", videoTitle);
                map.put("videoViews", getString(R.string.viewed) + " " + videoViews + " " + getString(R.string.times));
                map.put("videoUrl", videoPath);

                // adding HashList to ArrayList
                videosList.add(map);

                GetXMLTask task = new GetXMLTask();
                task.execute(new String[] { videoThumbPath });

                Log.d("videos", String.valueOf(i));

            } // End for

        } catch (JSONException e) {

            e.printStackTrace();

        }

        return null;
    }

    protected void onPostExecute(String file_url) {

        // updating UI from Background Thread
        runOnUiThread(new Runnable() {

            public void run() {

                ListAdapter adapter = new SimpleAdapter(

                onlineVideos.this, videosList, R.layout.videos_list_item,
                        new String[] { "videoTitle", "videoViews","videoUrl" }, 
                        new int[] { R.id.videoTitle, R.id.videoViews, R.id.videoURL }
                );

                // updating listview
                setListAdapter(adapter);

                // dismiss the dialog after getting all videos
                pDialog.dismiss();
            }
        });

    } // Close PostExecute

} // Close LoadVideosFromDB

//GetXMLTask - Used to get image from url
private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... urls) {

        Bitmap map = null;
        for (String url : urls) {
            map = downloadImage(url);
        }
        return map;
    }

    // Sets the Bitmap returned by doInBackground
    @Override
    protected void onPostExecute(Bitmap result) {

        ImageView videoImage = (ImageView)findViewById(R.id.videoImage);
        videoImage.setImageBitmap(result);

        int i = 0;
        Log.d("thumbs", String.valueOf(i));
        i++;
    }

    // Creates Bitmap from InputStream and returns it
    private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.
                    decodeStream(stream, null, bmOptions);
            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    // Makes HttpURLConnection and returns InputStream
    private InputStream getHttpConnection(String urlString) throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }
} //END OF GetXMLTask
class LoadVideosFromDB扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(onlineVideos.this);
setMessage(getString(R.string.loadingVideos));
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
添加(新的BasicNameValuePair(“令牌”,令牌));
//从URL获取JSON字符串
JSONObject json=jParser.makeHttpRequest(getAllVideosURL,“POST”,
参数);
//检查日志cat中的JSON响应
Log.d(“所有视频:,json.toString());
尝试{//尝试获取视频
//获取一系列视频
videos=json.getJSONArray(“视频”);
//循环浏览所有视频
对于(int i=0;ivalue
地图放置(“视频标题”,视频标题);
map.put(“videoViews”,getString(R.string.viewed)+“+videoViews+”+getString(R.string.times));
map.put(“videoUrl”,videoPath);
//将哈希列表添加到ArrayList
添加(地图);
GetXMLTask task=新建GetXMLTask();
执行(新字符串[]{videoThumbPath});
Log.d(“视频”,String.valueOf(i));
}//结束
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
//从后台线程更新UI
runOnUiThread(新的Runnable(){
公开募捐{
ListAdapter=新的SimpleAdapter(
onlineVideos.this,videosList,R.layout.videos\u list\u项,
新字符串[]{“videoTitle”、“VideoView”、“videoUrl”},
新int[]{R.id.videoTitle,R.id.videoViews,R.id.videoURL}
);
//更新列表视图
setListAdapter(适配器);
//获取所有视频后关闭对话框
pDialog.disclose();
}
});
}//关闭后执行
}//关闭LoadVideosFromDB
//GetXMLTask-用于从url获取图像
私有类GetXMLTask扩展了AsyncTask{
@凌驾
受保护位图doInBackground(字符串…URL){
位图映射=空;
for(字符串url:url){
map=下载图像(url);
}
返回图;
}
//设置doInBackground返回的位图
@凌驾
受保护的void onPostExecute(位图结果){
ImageView videoImage=(ImageView)findViewById(R.id.videoImage);
videoImage.setImageBitmap(结果);
int i=0;
Log.d(“拇指”,String.valueOf(i));
i++;
}
//从InputStream创建位图并返回它
私有位图下载图像(字符串url){
位图=空;
InputStream=null;
BitmapFactory.Options bmOptions=新的BitmapFactory.Options();
bmOptions.inSampleSize=1;
试一试{
流=getHttpConnection(url);
位图=位图工厂。
解码流(流,空,bmOptions);
stream.close();
}捕获(IOE1异常){
e1.printStackTrace();
}
返回位图;
}
//建立HttpURLConnection并返回InputStream
私有InputStream getHttpConnection(字符串urlString)引发IOException{
InputStream=null;
URL=新URL(URL字符串);
URLConnection=url.openConnection();
试一试{
HttpURLConnection httpConnection=(HttpURLConnection)连接;
setRequestMethod(“GET”);
httpConnection.connect();
if(httpConnection.getResponseCode()==HttpURLConnection.HTTP\u确定){
stream=httpConnection.getInputStream();
}
}捕获(例外情况除外){
例如printStackTrace();
}
回流;
}
}//GetXMLTask结束
请帮忙。
谢谢你的关注

用这个,我用这个,这个效果很好