Reputation: 1418
Im having some problems with using a json array to populate an imageview using picasso within a gridview.
I think i have most of it down but have however run into a but of a problem
my plan is to
a) collect the json array b) convert to a hashmap c) add it an array list d) pass arraylist to ImageAdaptor e) set the view and add correct data
i am currently stuck on d with the following error
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
not sure what i have got wrong but its got me stumped, and no amount of googling is helping
Main Activity
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();
}
}
}
}
Image Adaptor
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;
}
}
thanks in advance
Upvotes: 2
Views: 1454
Reputation: 2417
I hope, your problem was solved. It's not answer but a suggestion... I suggest you two libraries, one for JSON parsing and another for image loading and caching.
Use GSON for JSON The example app is also given with repository.
And Image Loader for load and cache image For implementation it's documentation is enough.
Upvotes: 0
Reputation: 26198
The problem is when you give the reference to the MainActivity.JSONParse
instead of the MainActivity
reference from here:
gridView.setAdapter(new ImageAdapter(this, oslist));
the this
will actually give you MainActivity.JSONParse
because you are calling it from an inner class of your MainActivity; To get the reference of you activity you need to call: MainActivity.this
gridView.setAdapter(new ImageAdapter(MainActivity.this, oslist));
Upvotes: 1