0

I would like to use Picasso library to download and show images that are in URL's inside JSON data from my server.

I've lost all hope, I saw something about custom adapter but to no avail. I need help here.

Here's how I do stuff now to show data from json inside onPostExecute:

protected  void onPostExecute(JSONObject objdanejson){
    pDialog.dismiss();

    try {
        android = objdanejson.getJSONArray(TAG_ZAWARTOSC);
        for(int i = 0; i < android.length(); i++){
            JSONObject c = android.getJSONObject(i);

            final String akt_tytul = c.getString(TAG_TYTUL);
            final String akt_skrot = c.getString(TAG_SKROT);
            final String akt_tresc = c.getString(TAG_TRESC);
            final String akt_id = c.getString(TAG_ID);
            final String akt_IMG = c.getString(TAG_IMG);

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

            map.put(TAG_TYTUL, akt_tytul);
            map.put(TAG_SKROT, akt_skrot);
            map.put(TAG_ID, akt_id);
            map.put(TAG_TRESC, akt_tresc);
            map.put("http://www.apirest.poligon.webimpuls.pl/"+TAG_IMG, akt_IMG);

            oslist.add(map);
            lista_aktualnosci = (ListView)findViewById(R.id.lista_aktualnosci);

            final ListAdapter adapter = new SimpleAdapter(aktualnosci.this, oslist,
                R.layout.aktualnosc_item,
                new String[]{TAG_TYTUL, TAG_SKROT, TAG_IMG}, new int[]{R.id.aktTytul, R.id.aktSkrot, R.id.aktIMG});

            lista_aktualnosci.setAdapter(adapter);

            lista_aktualnosci.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(aktualnosci.this, "Kliknąłeś na " + oslist.get(+position).get("akt_tytul"),Toast.LENGTH_SHORT).show();

                    Intent czytaj = new Intent(aktualnosci.this, aktualnosc_czytaj.class);
                    czytaj.putExtra("tytuł",oslist.get(+position).get("akt_tytul"));
                    czytaj.putExtra("tresc",oslist.get(+position).get("akt_tresc"));
                    czytaj.putExtra("id",oslist.get(+position).get("akt_id"));
                    startActivity(czytaj);
                }
            });
        }
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
}

EDIT:

How my custom controler looks:

oslist.add(map);
lista_aktualnosci = (ListView)findViewById(R.id.lista_aktualnosci);

/*   final ListAdapter adapter = new SimpleAdapter(aktualnosci.this, oslist,
    R.layout.aktualnosc_item,
    new String[]{TAG_TYTUL, TAG_SKROT}, new int[]{R.id.aktTytul, R.id.aktSkrot});
*/
class ListAdapter extends ArrayAdapter<ClipData.Item> {

    public ListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public ListAdapter(Context context, int resource, List<ClipData.Item> items) {
        super(context, resource, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.aktualnosc_item, null);
        }

        ClipData.Item p = getItem(position);

        if (p != null) {
            aktTytul = (TextView)findViewById(R.id.aktTytul);
            aktSkrot = (TextView)findViewById(R.id.aktSkrot);
            aktIMG = (ImageView)findViewById(R.id.aktIMG);
        }

        return v;
    }

}

lista_aktualnosci.setAdapter(adapter);
Ahmad Khan
  • 2,655
  • 19
  • 25
Aksebkit
  • 127
  • 1
  • 13
  • you need to implement a custom adapter...show your custom adapter code and then we can check it..also, save your data in a model class and pass that entire object to your custom adapter – Kaveesh Kanwal Sep 21 '16 at 10:24
  • Instead of Async task you can use retrofit..it will automatically set the json result into your model class. – Kaveesh Kanwal Sep 21 '16 at 10:29
  • I have no idea how to start creating custom adapter. With this I need help. – Aksebkit Sep 21 '16 at 10:53

2 Answers2

0

Create a class that extends Simple Adapter or Base Adapter. In getView() the adapter will ask you to create a list item when this is needed. As you create the item you can use Picasso to display the image in whatever image view you've selected for this purpose.

That being said you should ditch ListView and use RecyclerView instead - for which you can find all the information you need here

Other stuff that may improve your code are things like Retrofit and Gson.

Does that help?

Nikhil
  • 3,711
  • 8
  • 32
  • 43
spacitron
  • 2,095
  • 7
  • 30
  • 39
  • So how exacly that class should look like? – Aksebkit Sep 21 '16 at 10:54
  • I did something, but IDK how it relate to mine code. – Aksebkit Sep 21 '16 at 11:39
  • To be hones after looking at what you posted Picasso is the least of your problems. You should really go back to basics and look into how inheritance works and how android handles views and lists of views. The only other way someone could help you right now would be to write your code for you. – spacitron Sep 21 '16 at 13:02
  • I just need a little hint of code. Then I will get it. – Aksebkit Sep 21 '16 at 13:08
  • No, you need to go back and study the basics of object oriented programming. – spacitron Sep 23 '16 at 09:06
0

you can get your image from listview. You can this code your activity after set adapter then you can set your imageview.

for(int i = 0; i < this.listView.getChildCount(); i ++) {
           View view = this.listView.getChildAt(i);
           ImageView img= (ImageView)view.findViewById(R.id.yourimage);
           //do something your image 
}
Volkan Sonmez
  • 745
  • 3
  • 14