没有将图像从json文件加载到我的listview

时间:2015-10-13 12:49:31

标签: android json android-listview

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

Customlistviewadapter

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 Class

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.

2 个答案:

答案 0 :(得分:0)

你可以使用Aquery,它也用于图像加载,下载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;
    }

}

答案 1 :(得分:0)

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