Pass JSON data to my ImageAdapter From another class to supply my image paths
Ok so after making lots of progress over the weekend I am getting stuck on the gridView and ImageAdapter. Up to now all I have done is my main activity gets the user selected tags, does a query and returns json_ecoded data. I pass the json data via bundle and intent to my showThumb activity. This activity is supposed to attach an ImageAdapter to my grid view. But I cant figure out how to pass my json data to the ImageAdapter and then use the data from json to supply the paths to my thumbnails.
If there is any advice I can get to help that would be amazing.
JSON Data:
{"id":["1","2","3"],"name":["Dragon","Butterfly","Tattoo"],"thumb":["thm_polaroid.jpg","thm_default.jpg","thm_enhanced-buzz-9667-1270841394-4.jpg"],"path":["polaroid.jpg","default.jpg","enhanced-buzz-9667-1270841394-4.jpg"]}
ShowThumb Class
package com.flash_Images;
import android.os.Bundle;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import java.util.ArrayList;
import java.util.List;
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.OnClickL开发者_StackOverflow中文版istener;
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");
try {
JSONObject jsonObj = new JSONObject(jsonData);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
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 Class:
package com.flash_Images;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private JSONObject jsonObj;
public ImageAdapter(Context c) {
mContext = c;
}
public ImageAdapter(showThumb c) {
// TODO Auto-generated constructor stub
}
public int getCount() {
return mThumbIds.length;
}
public Object 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;
}
JSONArray menuitemArr = popupObject.getJSONArray("thumb");
// references to our images
private Integer[] mThumbIds = {
//I need to loop through my json "thumb:" items below to add the image paths for the grid view
for (int i = 0; i < menuitemArr.length(); i++) {
// printing the values to the logcat
setImageDrawable(Drawable.createFromPath(myFooArray[position].thumb);
}
//This is the example data I need to replace
/** R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7*/
};
}
Use an ArrayAdapter
instead of BaseAdapter
to make things work.
Make a class of menuitemArr
so that they can consist the data returned from json parser.
Define an ArrayAdapter
like this
public class ImageAdapter extends ArrayAdepter<YOUNEWCLASS>
create a constructor where you can pass the list of the parsed data
Take a look at the example of a ArrayAdapter
here.
Now use the adapter like this
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this,listofYourNewCalss));
Hope this can help you.
精彩评论