任何人都知道如何通过加载具有图像和标签的JSON中的图像和标签来获取GridView。我已经完成了图像加载部分,但每次打开页面时,它都会复制图像并发现设置标签时遇到困难。请让我知道解决这个问题的方法。下面是适配器代码
public class ImageAdapterGrid extends BaseAdapter implements ListAdapter{
private Context mContext;
public ImageAdapterGrid(Context c){
super();
this.mContext = c;
SymGridConfig.addImageUrls();
}
public int getCount() {
return SymGridConfig.getUrl_list().size();
}
public Object getItem(int position) {
return SymGridConfig.getUrl_list().get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ImageView imageView;
if(convertView==null)
{
imageView=new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(180,180));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5,5,5,5);
}else{
imageView=(ImageView)convertView;
}
imageView.setImageDrawable(LoadImageFromURL(SymGridConfig.
getUrl_list().get(position)));
return imageView;
}
private Drawable LoadImageFromURL(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src");
return d;
}catch (Exception e) {
System.out.println(e);
return null;
}
}
}
和类SymGridConfig如下:
public class SymGridConfig {
private static String url = "http://192.168.0.6/tele_medicine/index.php/symptoms/list";
private static final String SYMP_ID = "symp_id";
private static final String SYMP_NAME = "symptom_name";
private static final String SYMP_LOGO_URL = "symptom_logo";
static ArrayList<String> url_list=new ArrayList<String>();
static ArrayList<String> title_list = new ArrayList<String>();
static ArrayList<Item> gridArray = new ArrayList<Item>();
public static ArrayList<Item> getGridArray(){
return gridArray;
}
public static void setGridArray(ArrayList<Item> gridArray)
{
SymGridConfig.gridArray = gridArray;
}
public static ArrayList<String> getUrl_list() {
return url_list;
}
public static void setUrl_list(ArrayList<String> url_list) {
SymGridConfig.url_list = url_list;
}
public static ArrayList<String> getTitles(){
return title_list;
}
public static void setTitle_list(ArrayList<String> url_list) {
SymGridConfig.url_list = url_list;
}
public static void addImageUrls(){
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(img_url, ServiceHandler.GET);
if (jsonStr != null) {
try {
JSONArray jsonArray = new JSONArray(jsonStr);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String diag_id = jsonObject.getString(SYMP_ID);
String diag_name = jsonObject.getString(SYMP_NAME);
String img_url = jsonObject.getString(SYMP_LOGO_URL);
String logo_url="http://m2mapps.co.in/images/symptoms/";
img_url = logo_url.concat(img_url);
title_list.add(diag_name);
url_list.add(img_url);
gridArray.add(new Item(img_url,diag_name));
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
}
}
上述代码会在我重新打开页面时加载图像并重复,并且在设置图像标签时遇到问题。标签应为symptom_name。