-2

I created a simpleadapter with an HashMap to retrieve json datas. It works but i would create an ArrayAdapter instead. This is the simple adapter with json parsing:

@Override
          protected void onPostExecute(JSONObject json) {
              pDialog.dismiss();
          try {
             // Getting JSON Array from URL
              rating = json.getJSONArray(TAG_ITEM);

             for(int i = 0; i < rating.length(); i++)
             {
                 JSONObject c = rating.getJSONObject(i);
                 // Storing  JSON item in a Variable
                 String name = c.getString(TAG_NOME);
                 String commento = c.getString(TAG_COMMENTO);
                 String valore = c.getString(TAG_VALORE);
                 urlImage = c.getString(TAG_URLIMAGE);
                 String timestamp = c.getString(TAG_TIMESTAMP);        

                 // Adding value HashMap key => value
                 HashMap<String, String> map = new HashMap<String, String>();
                 map.put(TAG_NOME, name);
                 map.put(TAG_COMMENTO, commento);
                 map.put(TAG_URLIMAGE, urlImage);
                 map.put(TAG_VALORE, "Voted: "+valore);
                 map.put(TAG_TIMESTAMP, timestamp);
                 oslist.add(map);

                 ListAdapter adapter = new SimpleAdapter(RatingDetailsActivty.this, oslist, R.layout.list_rating_item,
                     new String[] 
                             { TAG_NOME,
                               TAG_COMMENTO,
                               TAG_VALORE,
                               TAG_TIMESTAMP
                              }, new int[] 
                                      {
                                        R.id.nome,
                                        R.id.commento, 
                                        R.id.valore, 
                                        R.id.timestamp
                                    })
                 {


                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) {
                            View v = super.getView(position, convertView, parent);

                            ImageView profileImage = (ImageView) v.findViewById(R.id.profile_img);
                            try {
                                @SuppressWarnings("unchecked")
                                HashMap<String, String> map = (HashMap<String, String>) getItem(position);
                                URL url = new URL(map.get(TAG_URLIMAGE));
                                imageProfilo = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                                Log.i("Url immagine", url.toString()); // here there is only last one

                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            if (imageProfilo != null) {
                                profileImage.setImageBitmap(imageProfilo);
                            } else {
                                profileImage.setImageDrawable(v.getResources() .getDrawable(R.drawable.welcome));
                            }
                            return v;

                        }

                 };
                 list.setAdapter(adapter);

            }
         } catch (JSONException e) {
           e.printStackTrace();
         }
        }

i tried to transform this simple adapter and create a ArrayAdapter in this way:

 @Override
          protected void onPostExecute(JSONObject json) {
              pDialog.dismiss();
          try {
             // Getting JSON Array from URL
              rating = json.getJSONArray(TAG_ITEM);

             for(int i = 0; i < rating.length(); i++)
             {
                 JSONObject c = rating.getJSONObject(i);
                 // Storing  JSON item in a Variable
                 String name = c.getString(TAG_NOME);
                 String commento = c.getString(TAG_COMMENTO);
                 String valore = c.getString(TAG_VALORE);
                 urlImage = c.getString(TAG_URLIMAGE);
                 String timestamp = c.getString(TAG_TIMESTAMP);


                 nomi.add(name);
                 commenti.add(commento);
                 valori.add(valore);
                 timestamps.add(timestamp);
                 adapter = new DetailsListAdapter(RatingDetailsActivty.this, nomi, valori, commenti, timestamps);
                 list.setAdapter(adapter);
            }
         } catch (JSONException e) {
           e.printStackTrace();
         }
        }

declaring as globals:

ArrayList<String> nomi; 
ArrayList<String> valori; 
ArrayList<String> commenti; 
ArrayList<String> timestamps; 

and this is the adapter:

public class DetailsListAdapter extends ArrayAdapter<String>{

    //riferimenti statici alle risorse e agli id
    private final static int LAYOUT = R.layout.list_rating_item;
    private final static int NOME = R.id.nome;
    private final static int VALORE = R.id.valore;
    private final static int COMMENTO = R.id.commento;
    private final static int TIMESTAMP = R.id.timestamp;

    ArrayList<String> nomi = null; //lista dei nomi
    ArrayList<String> valori; //lista dei valori
    ArrayList<String> commenti; //lista dei commenti
    ArrayList<String> timestamps; //lista dei ts

    ArrayList<String> array;
    Context c; //context
    LayoutInflater inflater; //layout inflater

    public DetailsListAdapter(Context context, ArrayList<String> nomi, ArrayList<String> valori, ArrayList<String> commenti, ArrayList<String> timestamps)
    {
        super(context,NOME);
        this.c = context;
        this.nomi = nomi;
        this.valori = valori;
        this.commenti = commenti;
        this.timestamps = timestamps;

        this.inflater = LayoutInflater.from(c);
        this.array = new ArrayList<String>();
        this.array.addAll(nomi);
    }

    @Override
    public int getCount()
    {
        return nomi.size(); //ritorno lunghezza lista ( = numero dei titoli)
    }



    //quando la lista richiede una view
    @Override
    public View getView(int pos,View view,ViewGroup parent)
    {
        CacheRiga cache; //cache
        if(view==null)//se è la prima volta che viene richiesta la view
        {
            // creo la view ma non l'attacco alla lista in quanto devo ancora modificare
            // i testi delle textview
            String data = nomi.get(pos);
            view = inflater.inflate(LAYOUT, parent,false); 
            cache = new CacheRiga(); //inizializzo la cache
            cache.nome = (TextView) view.findViewById(NOME); //collego titolo
            cache.commento = (TextView) view.findViewById(VALORE);//collego descrizione
            cache.valore = (TextView) view.findViewById(COMMENTO);//collego descrizione
            cache.timestamp = (TextView) view.findViewById(TIMESTAMP);//collego descrizione
            view.setTag(cache);//collego view con cache

        }
        else
        {
            cache = (CacheRiga) view.getTag(); //altrimenti prendo la cache dalla view
        }

        cache.nome.setText(nomi.get(pos)); //imposto il titolo
        cache.commento.setText(commenti.get(pos)); // e la descrizione
        cache.valore.setText(valori.get(pos)); // e la descrizione
        cache.timestamp.setText(timestamps.get(pos)); // e la descrizione

        return view;
    }



    private class CacheRiga { 
        public TextView nome; // cache titolo
        public TextView commento; // cache descrizione
        public TextView valore;
        public TextView timestamp;
    }

}

the app crashes in nomi.add(name); says the value is null... something is wrong?

Atlas91
  • 5,238
  • 14
  • 53
  • 117

1 Answers1

0

Same as you mentioned in first case: Pass the whole list of hashmap to arrayadapter instead of passing different aarraylist.

While extending your adapter to Aarayadapter ,use hashmap as generic instead of string .. ...

logan
  • 72
  • 4