Note that there are some explanatory texts on larger screens.

plurals
  1. POError while accessing the array
    text
    copied!<p>Noob android developer trying to create a gallery with thumbnails saved on my server:</p> <p>I am trying to use my ArrayList in my ImageAdapter so that I can reference my array values when creating my thumbnail list loop. My eclipse package is available for download <a href="http://www.2020mediaonline.com/Tattoo%20Flash%20App.zip" rel="nofollow">here</a> since I may not explain this correctly. When I do trying and reference my "thumb" values in my array I am getting an undefined error along with a syntax error when trying to create my "mThumbIds"</p> <p>I am not sure why I am having such a hard time understanding this. </p> <p>showThumb.java:</p> <pre><code>package com.flash_tattoo; import android.os.Bundle; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.*; public class showThumb extends Activity{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gridlayout); Bundle bundle = getIntent().getExtras(); String jsonData = bundle.getString("jsonData"); JSONArray jsonArray = null; try { jsonArray = new JSONArray(jsonData); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } ArrayList&lt;image_data&gt; myJSONArray = new ArrayList&lt;image_data&gt;(); for(int i=0;i&lt;jsonArray.length();i++) { JSONObject json_data = null; try { json_data = jsonArray.getJSONObject(i); } catch (JSONException e) { e.printStackTrace(); } try { image_data.id = json_data.getInt("id"); } catch (JSONException e) { e.printStackTrace(); } try { image_data.name = json_data.getString("name"); } catch (JSONException e) { e.printStackTrace(); } try { image_data.thumb = json_data.getString("thumb"); } catch (JSONException e) { e.printStackTrace(); } try { image_data.path = json_data.getString("path"); } catch (JSONException e) { e.printStackTrace(); } myJSONArray.add(new image_data()); } GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this,myJSONArray)); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) { Toast.makeText(showThumb.this, "" + position, Toast.LENGTH_SHORT).show(); } }); } } </code></pre> <p>ImageAdapter:</p> <pre><code>package com.flash_tattoo; import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONObject; import android.content.Context; import android.database.DataSetObserver; import android.graphics.drawable.Drawable; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.ListAdapter; public class ImageAdapter extends ArrayAdapter&lt;image_data&gt; { //This code below was put in when I change from BaseAdapter to ArrayAdapter public ImageAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); // TODO Auto-generated constructor stub } private Context mContext; public int getCount() { return mThumbIds.length; } public image_data getItem(int position) { return null; } public long getItemId(int position) { return 0; } // create a new ImageView for each item referenced by the Adapter public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } imageView.setImageResource(mThumbIds[position]); return imageView; } // references to our images private Integer[] mThumbIds = { for(int i=0;i&lt;myJSONArray.lenght();i++){ Object[] myJSONArray; setImageDrawable(Drawable.createFromPath(myJSONArray[i].thumb) }; }; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload