未将图像从 json 文件加载到我的列表视图

Posted

技术标签:

【中文标题】未将图像从 json 文件加载到我的列表视图【英文标题】:Not loading image from json file to my listview 【发布时间】:2016-01-11 05:37:26 【问题描述】:

我是 android 编程的新手,我正在尝试从 json 文件加载图像和文本,我的文本视图正在加载,但我的图像没有加载,请帮助我。

自定义列表视图适配器

import java.util.List;

import com.squareup.picasso.Picasso;
import com.suni.sun.HomeGv;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListViewAdapter extends ArrayAdapter<HomeGv> 

    private Activity activity;

    public CustomListViewAdapter(Activity activity, int resource,
            List<HomeGv> img) 
        super(activity, resource, img);
        this.activity = activity;
        // TODO Auto-generated constructor stub
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
        // TODO Auto-generated method stub

        ViewHolder holder = null;
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) 
            convertView = inflater.inflate(R.layout.home_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        else 
            holder = (ViewHolder) convertView.getTag();
        

        HomeGv thing = (HomeGv) getItem(position);

        holder.title.setText(thing.getTitle());
        holder.date_taken.setText(thing.getDate());
        Picasso.with(activity).load(thing.getMedia()).into(holder.image);

        return convertView;
    

    private static class ViewHolder 
        private TextView title,date_taken;
        private ImageView image;

        public ViewHolder(View v) 
            title = (TextView) v.findViewById(R.id.grid_item_title);
            date_taken = (TextView) v.findViewById(R.id.grid_item_date);
            image = (ImageView) v.findViewById(R.id.grid_item_image);
        
    


ListViewActivity 类

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.suni.sun.JsonParse;
import com.suni.sun.HomeGv;


public class ListViewActivity extends Activity  

    private ListView lv;
    private ArrayList<HomeGv> img;
    private ArrayAdapter<HomeGv> adap;

    private final static String TAG = ListViewActivity.class.getSimpleName();
    private final static String url = "http://saicloudica.cloudapp.net/json.txt";


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        lv = (ListView) findViewById(R.id.listView);
        setListViewAdapter();
        getDataFromInternet();
    

    private void getDataFromInternet()
        new JsonParse(this, url).execute();
    

    private void setListViewAdapter()
        img = new ArrayList<HomeGv>();
        adap = new CustomListViewAdapter(this, R.layout.home_item, img);
        lv.setAdapter(adap);
    

    public void parseJsonResponse(String result)
        Log.i(TAG, result);
        try
            JSONObject json = new JSONObject(result);
            JSONArray jArray = new JSONArray(json.getString("items"));
            for (int i = 0; i < jArray.length(); i++)
                JSONObject jObj = jArray.getJSONObject(i);

                HomeGv imgs = new HomeGv();
                imgs.setTitle(jObj.getString("title"));
                imgs.setDate(jObj.getString("date_taken"));
                if(json.has("media")) 
                 
                    JSONObject media = jObj.getJSONObject("media"); 
                    String medi = media.getString("m");

                    if(medi != null)
                     
                        imgs.setMedia(media.getString("m"));
                    
                


                img.add(imgs);
            
            adap.notifyDataSetChanged();
         catch (JSONException e)

            e.printStackTrace();
        
    

    @Override
    public void onBackPressed() 
        // TODO Auto-generated method stub

        finish();
    

家庭课堂

public class HomeGv 

    private String title;
    private String media;
    private String date_taken;

    public String getTitle() 
        return title;
    
    public void setTitle(String title) 
        this.title = title;
    
    public String getMedia() 
        return media;
    
    public void setMedia(String media) 
        this.media = media;
    
    public String getDate() 
        return date_taken;
    
    public void setDate(String date_taken) 
        this.date_taken = date_taken;
    

日志:

10-13 12:39:33.835: I/ListViewActivity(5501): "generator":"http:\/\/www.flickr.com\/","title":"Recent Uploads tagged soccer","items":["tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9207","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21782884471\/\" title=\"IMG_9207\"><img src=\"http:\/\/farm1.staticflickr.com\/681\/21782884471_6834f2c9d8_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9207\" \/><\/a><\/p> ","date_taken":"2015-09-27T15:00:50-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21782884471\/","author_id":"124810748@N04","published":"2015-09-28T02:59:30Z","media":"m":"http:\/\/farm1.staticflickr.com\/681\/21782884471_6834f2c9d8_m.jpg","tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9484","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21747301246\/\" title=\"IMG_9484\"><img src=\"http:\/\/farm1.staticflickr.com\/736\/21747301246_f03bc953d6_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9484\" \/><\/a><\/p> ","date_taken":"2015-09-27T15:58:43-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21747301246\/","author_id":"124810748@N04","published":"2015-09-28T02:59:38Z","media":"m":"http:\/\/farm1.staticflickr.com\/736\/21747301246_f03bc953d6_m.jpg","tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9135","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21585600568\/\" title=\"IMG_9135\"><img src=\"http:\/\/farm1.staticflickr.com\/588\/21585600568_bce2bcd47a_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9135\" \/><\/a><\/p> ","date_taken":"2015-09-27T14:53:53-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21585600568\/","author_id":"124810748@N04","published":"2015-09-28T02:59:28Z","media":"m":"http:\/\/farm1.staticflickr.com\/588\/21585600568_bce2bcd47a_m.jpg","tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9062","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21761757502\/\" title=\"IMG_9062\"><img src=\"http:\/\/farm6.staticflickr.com\/5648\/21761757502_e18ee1b76f_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9062\" \/><\/a><\/p> ","date_taken":"2015-09-27T14:48:06-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21761757502\/","author_id":"124810748@N04","published":"2015-09-28T02:59:27Z","media":"m":"http:\/\/farm6.staticflickr.com\/5648\/21761757502_e18ee1b76f_m.jpg","tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9718","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21773428785\/\" title=\"IMG_9718\"><img src=\"http:\/\/farm6.staticflickr.com\/5798\/21773428785_256689c88f_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9718\" \/><\/a><\/p> ","date_taken":"2015-09-27T16:27:04-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21773428785\/","author_id":"124810748@N04","published":"2015-09-28T02:59:43Z","media":"m":"http:\/\/f
    10-13 12:39:34.425: I/JsonParse(5501): "generator":"http:\/\/www.flickr.com\/","title":"Recent Uploads tagged soccer","items":["tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9207","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21782884471\/\" title=\"IMG_9207\"><img src=\"http:\/\/farm1.staticflickr.com\/681\/21782884471_6834f2c9d8_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9207\" \/><\/a><\/p> ","date_taken":"2015-09-27T15:00:50-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21782884471\/","author_id":"124810748@N04","published":"2015-09-28T02:59:30Z","media":"m":"http:\/\/farm1.staticflickr.com\/681\/21782884471_6834f2c9d8_m.jpg","tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9484","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21747301246\/\" title=\"IMG_9484\"><img src=\"http:\/\/farm1.staticflickr.com\/736\/21747301246_f03bc953d6_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9484\" \/><\/a><\/p> ","date_taken":"2015-09-27T15:58:43-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21747301246\/","author_id":"124810748@N04","published":"2015-09-28T02:59:38Z","media":"m":"http:\/\/farm1.staticflickr.com\/736\/21747301246_f03bc953d6_m.jpg","tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9135","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21585600568\/\" title=\"IMG_9135\"><img src=\"http:\/\/farm1.staticflickr.com\/588\/21585600568_bce2bcd47a_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9135\" \/><\/a><\/p> ","date_taken":"2015-09-27T14:53:53-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21585600568\/","author_id":"124810748@N04","published":"2015-09-28T02:59:28Z","media":"m":"http:\/\/farm1.staticflickr.com\/588\/21585600568_bce2bcd47a_m.jpg","tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9062","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21761757502\/\" title=\"IMG_9062\"><img src=\"http:\/\/farm6.staticflickr.com\/5648\/21761757502_e18ee1b76f_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9062\" \/><\/a><\/p> ","date_taken":"2015-09-27T14:48:06-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21761757502\/","author_id":"124810748@N04","published":"2015-09-28T02:59:27Z","media":"m":"http:\/\/farm6.staticflickr.com\/5648\/21761757502_e18ee1b76f_m.jpg","tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9718","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21773428785\/\" title=\"IMG_9718\"><img src=\"http:\/\/farm6.staticflickr.com\/5798\/21773428785_256689c88f_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9718\" \/><\/a><\/p> ","date_taken":"2015-09-27T16:27:04-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21773428785\/","author_id":"124810748@N04","published":"2015-09-28T02:59:43Z","media":"m":"http:\/\/farm6.st
    10-13 12:39:34.425: I/Choreographer(5501): Skipped 77 frames!  The application may be doing too much work on its main thread.
    10-13 12:39:34.495: W/Settings(5501): Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.

【问题讨论】:

您需要使用库在列表中显示图像。像 picasso 或 volley 或通用图像加载器这样的著名库 @Moudiz 请正确查看代码,他已经在使用 Picasso @Heamanth 请在JSONLint上发布您的完整日志并检查您的 JSON 语法 @vipul 在 logcat 中的行仅在加载 json 数据时出现。 去掉这个条件if(json.has("media")) 看看是否有效 【参考方案1】:

您可以使用也用于图像加载的 Aquery,下载查询 jar 并将其导入您的项目并尝试以下代码

     public class CustomAdapterAccept extends BaseAdapter

    private Context context;
    private ArrayList<HashMap<String,String>> listData;
    private AQuery aQuery;

    private static final String TAG_NAME="name";
    private static final String TAG_PROFILE="profile_id";
    private static final String TAG_IMAGE="image";
    private static final String TAG_CAST="cast";
    private static final String TAG_AGE="age";
    private static final String TAG_LOCATION="location";


    public CustomAdapterAccept(Context context,ArrayList<HashMap<String,String>> listData) 
        this.context = context;
        this.listData=listData;
        aQuery = new AQuery(this.context);
    

    @Override
    public int getCount() 
        return listData.size();
    

    @Override
    public Object getItem(int position) 
        return listData.get(position);
    

    @Override
    public long getItemId(int position) 
        return position;
    

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
        ViewHolder holder;
        if (convertView == null) 
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_acceptedlist, null);
            holder.propic = (ImageView) convertView.findViewById(R.id.propicaccept);
            holder.txtproname = (TextView) convertView.findViewById(R.id.txtpronameacptedlist);
            holder.txtproid = (TextView) convertView.findViewById(R.id.txtproidacptedlist);
            holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txtprofilecastacptedlist);
            holder.txtprofileage = (TextView) convertView.findViewById(R.id.txtprofileageacptedlist);
            holder.txtprofileplace = (TextView) convertView.findViewById(R.id.txtprofileplaceacptedlist);

            convertView.setTag(holder);
        else
            holder = (ViewHolder) convertView.getTag();
        

        holder.txtproname.setText(listData.get(position).get(TAG_NAME));
        holder.txtproid.setText(listData.get(position).get(TAG_PROFILE));
        holder.txtprofilecast.setText(listData.get(position).get(TAG_CAST));
        holder.txtprofileage.setText(listData.get(position).get(TAG_AGE));
        holder.txtprofileplace.setText(listData.get(position).get(TAG_LOCATION));

        aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);

        // image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image
        return convertView;
    
    class ViewHolder
        ImageView propic;
        TextView txtproname;
        TextView txtproid;
        TextView txtprofilecast;
        TextView txtprofileage;
        TextView txtprofileplace;
    


【讨论】:

毕加索不可能吗 @HeamanthVarma picasso 比 Aquery 更高效,而 Aquery 已被弃用【参考方案2】:

只需从代码中删除此条件if(json.has("media")) 即可。 当您检查 media 作为主 JSON 中的标签时,它不包含一个标签。

【讨论】:

以上是关于未将图像从 json 文件加载到我的列表视图的主要内容,如果未能解决你的问题,请参考以下文章

将图像从Firebase加载到我的表视图时出错

在 Picasso 图像加载器中加载位图图像会减慢列表视图中的滚动速度

从 JSON 中的 URL 加载的图像视图在滚动时发生变化

从文档文件夹加载集合视图中的图像

如何从 postgresql 视图表中获取要显示的对象列表

GetJSON 从 URL 到 d3.js 图表