我正在創建一個圖庫應用程序..它在這裏描述。 http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/如何從服務器的圖像URL列表加載jQuery的gridview中的圖像
我能夠從繪圖資源做到這一點。我需要的是我想從服務器獲取圖像的URL爲JSON並顯示它.. 我的JSON格式是: http://52.42.251.70/testData/imagelist.php
片段代碼:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
GridView gridView = (GridView)getView().findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this.getActivity()));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getActivity(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
Imageadapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
JSONObject jsonobject;
DatabaseUserTable myDatadb;
JSONArray jsonarray;
String imageUrl = "";
String id = "";
ArrayList<String> Urls = new ArrayList<String>();
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.imagewall,R.drawable.imagewall,
R.drawable.imagewall,R.drawable.imagewall,
R.drawable.imagewall,R.drawable.imagewall,
R.drawable.imagewall,R.drawable.imagewall,
R.drawable.imagewall,R.drawable.imagewall,
R.drawable.imagewall,R.drawable.imagewall,
R.drawable.imagewall,R.drawable.imagewall,
R.drawable.imagewall
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
return imageView;
}
}
任何幫助將不勝感激..
你也可以從同一網站查看[this](http://www.androidhive.info/2012/01/android-json-parsing-tutorial/)。 – Nisarg
您的數據不是JSON格式的回覆http://jsonviewer.stack.hu/?utm_content=buffer2b790&utm_medium=social&utm_source=app.net&utm_campaign=buffer#http://52.42.251.70/testData/imagelist.php –
@LiemVo是正確的,你json無效。在每個「id」標籤之前,你需要一個{在第一個之前有一個,但沒有其他的。 –