Noob android developer trying to create a gallery with thumbnails saved on my server:
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 here 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"
I am not sure why I am having such a hard time understanding this.
showThumb.java:
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<image_data> myJSONArray = new ArrayList<image_data>();
for(int i=0;i<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 (JSONE开发者_C百科xception 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<?> parent, View v, int position, long id) {
Toast.makeText(showThumb.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
ImageAdapter:
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<image_data>
{
//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<myJSONArray.lenght();i++){
Object[] myJSONArray;
setImageDrawable(Drawable.createFromPath(myJSONArray[i].thumb)
};
};
}
You have there a private field mThumbIds of type Integer[] and you're trying to initialize it inline, but you fail to do so because you have an static block {} with statements.
I would say you need to just declare:
private Integer[] mThumbIds;
then, in the constructor you can populate the array.
精彩评论