我在使用json数组在gridview中使用picasso填充imageview时遇到了一些问题。
我认为我的大部分内容已经失败,但却遇到了一个问题
我的计划是
a)收集json数组 b)转换为hashmap c)添加一个数组列表 d)将arraylist传递给ImageAdaptor e)设置视图并添加正确的数据
我目前因以下错误而陷入困境
Error:(97, 41) error: constructor ImageAdapter in class ImageAdapter cannot be applied to given types;
required: Context,ArrayList<HashMap<String,String>>
found: MainActivity.JSONParse,ArrayList<HashMap<String,String>>
reason: actual argument MainActivity.JSONParse cannot be converted to Context by method invocation conversion
不确定我有什么问题,但它让我感到难过,没有任何谷歌搜索帮助
主要活动
package com.example.alex.jsonparsegridview;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends Activity {
GridView gridView;
static final String[] MOBILE_OS = new String[]{
"Android", "iOS", "Windows", "Blackberry"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private class JSONParse extends AsyncTask<String, String, JSONArray> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONArray doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
String url = "http://www.500kgiveaway.co.uk/phpscript.php";
JSONArray json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONArray json) {
pDialog.dismiss();
try {
// Getting JSON Array from URL
JSONArray android = json;
String TAG_PATH = "path";
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < android.length(); i++) {
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
String path = c.getString(TAG_PATH);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PATH, path);
oslist.add(map);
//copied from onCreate
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, oslist));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(
getApplicationContext(),
((TextView) v.findViewById(R.id.grid_item_label))
.getText(), Toast.LENGTH_SHORT).show();
}
});
//End Copy
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
图像适配器
package com.example.alex.jsonparsegridview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class ImageAdapter extends BaseAdapter {
private Context context;
ArrayList<HashMap<String, String>> dataList;
public ImageAdapter(Context context, ArrayList<HashMap<String, String>> oslist) {
this.context = context;
this.dataList = oslist;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.mobile, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(((Map)getItem(position)).get("path").toString());
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
Picasso.with(context).load(((Map) getItem(position)).get("path").toString()).into(imageView);
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
提前致谢
答案 0 :(得分:1)
问题是当您从此处提供MainActivity.JSONParse
而非MainActivity
引用的引用时:
gridView.setAdapter(new ImageAdapter(this, oslist));
this
实际上会给你MainActivity.JSONParse
,因为你是从MainActivity的内部类调用它的;要获得您的活动参考,您需要致电:MainActivity.this
gridView.setAdapter(new ImageAdapter(MainActivity.this, oslist));
答案 1 :(得分:0)
我希望,你的问题已经解决了。 这不是答案,而是一个建议...... 我建议你使用两个库,一个用于JSON解析,另一个用于图像加载和缓存。
将GSON用于JSON 示例应用程序也随存储库一起提供。
Image Loader用于加载和缓存图片 对于实现它documentation就足够了。