我试图在ImageAdapter(图片)中设置一个数组,该数组等于名为mComments的arrayList中的JSON内容。我似乎无法通过在mComments上使用toArray()方法来实现这一点,并且类型转换也不起作用。我试图使用JSON内容在GridView中设置图像而不是硬编码资源。请参阅下面的ImageAdapter类以及相关的调用类。可根据要求提供完整的课程,如果有帮助,可以使用JSON URL:https://shipstudent.com/complaint_desk/androidImageFetch.php。如果您需要更多信息,请与我们联系。
ImageAdapter:
public class ImageAdapter extends BaseAdapter
{
private Context context;
public ImageAdapter(Context c)
{
context=c;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return pics.length;
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv;
if (convertView == null) { // if it's not recycled, initialize some attributes
iv = new ImageView(context);
iv.setLayoutParams(new GridView.LayoutParams(350,350));
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setPadding(8, 8, 8, 8);
} else {
iv = (ImageView) convertView;
}
iv.setImageResource(pics[position]);
return iv;
}
public Integer[] pics={
R.drawable.menu ,R.drawable.dog,
R.drawable.favorites,R.drawable.pmlimage,
R.drawable.progress,R.drawable.hearsay,
// R.drawable.sample_6,R.drawable.sample_7
};
}
致电班级:
private JSONArray mComments = null;
public void updateJSONdata() {
// Instantiate the arraylist to contain all the JSON data.
// we are going to use a bunch of key-value pairs, referring
// to the json element name, and the content, for example,
// message it the tag, and "I'm awesome" as the content..
categoryList = new ArrayList<HashMap<String, String>>();
// Bro, it's time to power up the J parser
JSONParser jParser = new JSONParser();
// Feed the beast our comments url, and it spits us
// back a JSON object. Boo-yeah Jerome.
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
// when parsing JSON stuff, we should probably
// try to catch any exceptions:
try {
// I know I said we would check if "Posts were Avail." (success==1)
// before we tried to read the individual posts, but I lied...
// mComments will tell us how many "posts" or comments are
// available
mComments = json.getJSONArray(TAG_POSTS);
// looping through all posts according to the json object returned
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
// gets the content of each tag
String comment = c.getString(TAG_COMMENT);
String filename = c.getString(TAG_FILENAME);
String IDUser = c.getString(TAG_IDUser);
System.out.print(comment);
System.out.print(filename);
System.out.print(IDUser);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_COMMENT, comment);
map.put(TAG_FILENAME, filename);
map.put(TAG_IDUser, IDUser);
// adding HashList to ArrayList
categoryList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* Inserts the parsed data into the listview.
*/
private void updateList() {
// For a ListActivity we need to set the List Adapter, and in order to do
//that, we need to create a ListAdapter. This SimpleAdapter,
//will utilize our updated Hashmapped ArrayList,
//use our single_post xml template for each item in our list,
//and place the appropriate info from the list to the
//correct GUI id. Order is important here.
// ListAdapter adapter = new SimpleAdapter(this.getActivity(), categoryList,
// R.layout.categories_list, new String[] { TAG_CATEGORY_NAME }, new int[] { R.id.title });
//
// // I shouldn't have to comment on this one:
// setListAdapter(adapter);
//imageAdapter.pics = (Integer[]) categoryList.clone();
System.out.print(categoryList.toString());
}