使用Android Studio中的Picasso从列表视图中的URL加载图像

时间:2016-04-09 04:09:37

标签: android image listview picasso

我想将数据库中的图像显示到ListView。 我想用毕加索来加载图像。但我不知道如何将毕加索放在那里。你能告诉我我应该把它放在哪里吗? 谢谢..

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.media.Image;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.squareup.picasso.Picasso;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends ActionBarActivity {
    JSONParser jParser= new JSONParser();
    ArrayList<HashMap<String, String>> nameList;
    JSONArray names=null;
    ListView data;
    private static final String URL_TEST_BARANG= "http://lomapod.azurewebsites.net/readBarang.php?id_penjual=1";
    private static final String TAG_PESAN = "message";
    private static final String TAG_HASIL = "result";
    private static final String TAG_BARANG = "nama_barang";
    private static final String TAG_IMAGE= "nama";
    ImageView imageview;
    int myInt=0;
    public  MainActivity()
    {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        data=(ListView) findViewById(R.id.lvSeller);
        nameList= new ArrayList<HashMap<String, String>>();
        imageview =(ImageView) findViewById(R.id.imageView);
    }


    public class AmbilDataJson extends AsyncTask<String,String,String> {

        int sukses=0;

        public AmbilDataJson() {

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... args) {

            List<NameValuePair> params = new ArrayList<NameValuePair>();

            try
            {
                JSONObject json = jParser.makeHttpRequest(URL_TEST_BARANG, "GET", params);

                if(json != null)
                {
                    sukses = json.getInt(TAG_PESAN);
                    if(sukses == 1)
                    {
                        nameList = new ArrayList<HashMap<String,String>>();
                        Log.d("Semua Nama: ", json.toString());
                        names = json.getJSONArray(TAG_HASIL);

                        for(int i = 0; i < names.length();i++)
                        {
                            JSONObject c = names.getJSONObject(i);
                            String id = c.getString(TAG_BARANG);
                            String seller = "lomapod.esy.es/assets/"+c.getString(TAG_IMAGE);

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

                            map.put(TAG_BARANG,id);
                            map.put(TAG_IMAGE,seller);
                            nameList.add(map);
                        }
                    }
                }
            }catch(JSONException e)
            {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            if(sukses == 1 )
            {
                ListAdapter adapter = new SimpleAdapter(
                        getApplicationContext(),nameList,
                        R.layout.grid_item,new String[]
                        {
                                TAG_BARANG,TAG_IMAGE
                        }
                        ,new int[]
                        {
                                R.id.tvSeller, R.id.imageView
                        }

                );
                data.setAdapter(adapter);
            }
        }
    }

}

这是我想要使用的毕加索代码。 TAG_IMAGE是应加载的图片网址

Picasso.with(this)
    .load(TAG_IMAGE)
    .into(imageView);

0 个答案:

没有答案