title: android - populating ListView with data from JSONArray
body: <p>Situation: I'm getting a <code>JSONObject</code> containing user's playlist from the server. I want to display this data in a <code>ListView</code>, there are 288 audio files in my test case. The JSON is parsed correctly, the size equals to 288. I created a help class and parametrized the <code>ArrayList</code> with its Objects. The problem: there're exactly 288 items in my <code>ListView</code> BUT the correct entries Artist - Title go up to index 13 (don't know why) and then they get <em>repeated</em>. So I don't have 288 different entries in the List, but I have only 14 entries which are repeated until the end of the List.</p>
  
  <p><strong>UPDATE:</strong> I removed the if statement and it solved the issue but RAM consumption has increased by 3MB. Any suggestions for optimizing? </p>
  
  <pre><code>   public class AudioList extends ListActivity {
  
  private ListView lv;
  private JSONObject usersPlaylist, singleJSONItem;
  private JSONArray responseJSONArray;
  private SharedPreferences prefs;
  private PlaylistItem audioList;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_audio_list);
      init();
  
      ArrayList&lt;PlaylistItem&gt; playlist = new ArrayList&lt;PlaylistItem&gt;();
      try {
          usersPlaylist = Utils.retrieveJsonObjectFromUrl(new URL(
                  APP_CONSTANTS.REQUEST_AUDIO_LIST(prefs)), this);
          responseJSONArray = usersPlaylist.getJSONArray("response");
  
          for (int i = 0; i &lt; responseJSONArray.length(); i++) {
              singleJSONItem = responseJSONArray.getJSONObject(i);
              audioList = new PlaylistItem(singleJSONItem);
              playlist.add(audioList); 
          }
  
          Toast.makeText(getApplicationContext(),
                  Integer.toString(playlist.size()), Toast.LENGTH_LONG)
                  .show(); //there are 288 entries, JSONArray parsed correctly
  
      } catch (MalformedURLException e) {
  
          e.printStackTrace();
      } catch (JSONException e) {
  
          e.printStackTrace();
      } catch (IOException e) {
  
          e.printStackTrace();
      }
  
      AudioListAdapter adapter = new AudioListAdapter(this,
              R.layout.playlist_item, playlist);
      lv.setAdapter(adapter);
  
  }
  
  private void init() {
      lv = getListView();
      lv.setTranscriptMode(0x00000000);
      lv.setDividerHeight(1);
      lv.setSmoothScrollbarEnabled(true);
      prefs = PreferenceManager.getDefaultSharedPreferences(this);
  
  }
  </code></pre>
  
  <p>The PlayListItem class:</p>
  
  <pre><code>     public class PlaylistItem {
  
  private String artist, title;
  private JSONObject obj;
  
  public PlaylistItem(JSONObject obj) {
      this.obj = obj;
  }
  public PlaylistItem(){
  
  }
  
  public String getArtist() {
      try {
          artist = obj.getString("artist");
      } catch (JSONException e) {
  
          e.printStackTrace();
      }
      return artist;
  
  }
  
  public String getTitle() {
      try {
          title = obj.getString("title");
      } catch (JSONException e) {
  
          e.printStackTrace();
      }
      return title;
  }
      }
  </code></pre>
  
  <p>The adapter:</p>
  
  <pre><code>     public class AudioListAdapter extends ArrayAdapter&lt;PlaylistItem&gt; {
  
  private Context context;
  private int layoutResourceId;
  private PlaylistItem aud;
  private ArrayList&lt;PlaylistItem&gt; data = null;
  
  public AudioListAdapter(Context context, int layoutResourceId,
          ArrayList&lt;PlaylistItem&gt; data) {
      super(context, layoutResourceId, data);
      this.layoutResourceId = layoutResourceId;
      this.context = context;
      this.data = data;
  
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      View row = convertView;
      ViewHolder holder = new ViewHolder();
      aud = data.get(position);
              //it's giving a CORRECT position if I hardcode the index, like data.get(99);
      if (row == null) {
  
          LayoutInflater inflater = ((Activity) context).getLayoutInflater();
          row = inflater.inflate(layoutResourceId, parent, false);
          holder.play = (Button) row.findViewById(R.id.btn_list_play);
          holder.imgSaved = (ImageView) row
                  .findViewById(R.id.img_list_audio_saved);
          holder.tvArtist = (TextView) row
                  .findViewById(R.id.tvListItemArtist);
          holder.tvTitle = (TextView) row.findViewById(R.id.tvListItemSong);
  
          holder.tvArtist.setText(aud.getArtist());
          holder.tvTitle.setText(aud.getTitle());
  
      }
  
      return row;
  }
  
  static class ViewHolder {
      Button play;
      ImageView imgSaved;
      TextView tvArtist, tvTitle;
  
  }
  
      }
  </code></pre>