java - Custom List View Adapter Syntax for images -
i new android development , in process of creating first app. app takes picture, previews picture, randomly selects picture database upload server , displays json text information in listview.
i trying use custom list view adapter allow json information's corresponding picture in listview , not imageview.
i have thoroughly researched listview adapters understand syntax , place code implement listview picture properly. however, receiving 4 error messages prevent code compiling. realize there other posts similar topics on website, have dug around them , tried implement states solutions no success.
below code:
public class jsonbuilderactivity extends listactivity { private progressdialog pdialog; //url json private static string url = "........."; //json node names private static final string tag_cars = "cars"; //root private static final string tag_carid = "carid"; private static final string tag_carvin = "carvin"; private static final string tag_img = "carmainimage"; customlistviewadapter adapter; jsonarray carid = null; //initializes json array private customlistviewadapter clva = null; listview lv; list<item> item = new arraylist<jsonbuilderactivity.item>(); static string response = null; //hashmap listview arraylist<hashmap<string, object>> caridlist; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); lv = getlistview(); //arraylist<item> item; lv.setadapter(adapter); lv = (listview) findviewbyid(r.id.list_item); adapter = new customlistviewadapter(this,item); lv.setadapter(adapter); //listview on item click listener lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { //gets values selected listitem string cars = ((textview) view.findviewbyid(r.id.cars)).gettext().tostring(); string car_id = ((textview) view.findviewbyid(r.id.car_id)).gettext().tostring(); string car_vin = ((textview) view.findviewbyid(r.id.car_vin)).gettext().tostring(); string model_img = ((imageview) view.findviewbyid(r.id.model_img)).gettag().tostring(); intent in = new intent(jsonbuilderactivity.this, mainactivity.class); //sends data mainactivity in.putextra("tag_cars", cars); in.putextra("tag_carid", car_id); in.putextra("tag_carvin", car_vin); in.putextra("tag_img", model_img); startactivity(in); } }); //calls async task json new getcars().execute(); } public class servicehandler { public final static int = 1; public final static int post = 2; public servicehandler() { } /** * makes service call * * @url - url make request * @method - http request method */ public string makeservicecall(string url, int method) { return this.makeservicecall(url, method, null); } /** * makes service call * * @url - url make request * @method - http request method * @params - http request params */ public string makeservicecall(string url, int method, arraylist<namevaluepair> params) { try { defaulthttpclient httpclient = new defaulthttpclient(); httpentity httpentity = null; httpresponse httpresponse = null; //checks http request method type if (method == post) { httppost httppost = new httppost(url); //adds post params if (params != null) { httppost.setentity(new urlencodedformentity(params)); } httpresponse = httpclient.execute(httppost); } else if (method == get) { //appends params url if (params != null) { string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; } httpget httpget = new httpget(url); httpresponse = httpclient.execute(httpget); } httpentity = httpresponse.getentity(); response = entityutils.tostring(httpentity); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return response; } } public uri getimageuri(context incontext, bitmap inimage) { bytearrayoutputstream bytes = new bytearrayoutputstream(); inimage.compress(bitmap.compressformat.jpeg, 100, bytes); string path = mediastore.images.media.insertimage(incontext.getcontentresolver(), inimage, "carmainimage", null); return uri.parse(path); } public void savebmptofile(file filename, bitmap bmp) { fileoutputstream out = null; try { out = new fileoutputstream(filename); bmp.compress(bitmap.compressformat.png, 100, out); } catch (exception e) { e.printstacktrace(); } { try { if (out != null) { out.close(); } } catch (ioexception e) { e.printstacktrace(); } } } public boolean renamefileextension(string source, string newextension) { string target; string currentextension = getfileextension(source); if (currentextension.equals("")) { target = source + "." + newextension; } else { target = source.replacefirst(pattern.quote("." + currentextension) + "$", matcher.quotereplacement("." + newextension)); } return new file(source).renameto(new file(target)); } public string getfileextension(string f) { string ext = ""; int = f.lastindexof('.'); if (i > 0 && < f.length() - 1) { ext = f.substring(i + 1); } return ext; } /* * async task class json making http call */ private class getcars extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); caridlist = new arraylist<hashmap<string, object>>(); //shows progress dialog pdialog = new progressdialog(jsonbuilderactivity.this); pdialog.setmessage("please wait..."); pdialog.setcancelable(false); pdialog.show(); } @override protected void doinbackground(void... arg0) { //creates service handler class instance servicehandler sh = new servicehandler(); //makes request url , getting response string jsonstr = sh.makeservicecall(url, servicehandler.get); //prints json response in log log.d("getcars response: ", "> " + jsonstr); if (jsonstr != null) { try { log.d("try", "in try"); jsonobject jsonobj = new jsonobject(jsonstr); log.d("jsonobject", "new json object"); //gets json array node carid = jsonobj.getjsonarray(tag_cars); log.d("json array", "user point array"); int len = carid.length(); log.d("len", "get array length"); (int = 0; < carid.length(); i++) { jsonobject c = carid.getjsonobject(i); string car_id = c.getstring(tag_carid); log.d("car_id", car_id); string car_vin = c.getstring(tag_carvin); log.d("car_vin", car_vin); string model_img = c.getstring(tag_img); log.d("model_img", model_img); //customlistviewadapter adapter = new customlistviewadapter(this, r.layout.list_item, item); // string model_img = c.getstring(tag_img); //log.d("model_img", model_img); //hashmap single match hashmap<string, object> matchgetcars = new hashmap<string, object>(); //adds each child node hashmap key => value matchgetcars.put(tag_carid, car_id); matchgetcars.put(tag_carvin, car_vin); matchgetcars.put(tag_img, model_img); caridlist.add(matchgetcars); } } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); //dismisses progress dialog if (pdialog.isshowing()) pdialog.dismiss(); /** * updates parsed json data listview * */ //listadapter adapter = new simpleadapter(jsonbuilderactivity.this, caridlist, r.layout.list_item, // new string[]{tag_carid, tag_carvin, tag_img}, new int[]{r.id.car_id, r.id.car_vin, r.id.model_img}); // setlistadapter(adapter); listview lv = (listview)findviewbyid(r.id.list_item); clva = new customlistviewadapter(); lv.setadapter(clva); } } public class customlistviewadapter extends arrayadapter<item> { private arraylist<item> objitem; activity context; public customlistviewadapter(arraylist<item> item, int resourceid, activity context){ super(context, resourceid, item); this.context = context; this.objitem = item; } private class viewholder { imageview model_img; textview car_id; textview car_vin; } public view getview ( int position, view convertview, viewgroup parent){ viewholder holder = null; final item item = getitem(position); layoutinflater minflater = (layoutinflater) context .getsystemservice(activity.layout_inflater_service); if (convertview == null) { convertview = minflater.inflate(r.layout.list_item, null); holder.car_id = (textview) convertview.findviewbyid(r.id.car_id); holder.car_vin = (textview) convertview.findviewbyid(r.id.car_vin); holder.model_img = (imageview) convertview.findviewbyid(r.id.model_img); convertview.settag(holder); } else holder = (viewholder) convertview.gettag(); holder.car_id.settext(item.getvin()); holder.car_vin.settext(item.getid()); holder.model_img.setimageresource(item.getmodelimg()); return convertview; } } public class item { private int model_img; private string car_id; private string car_vin; public item(int model_img, string car_id, string car_vin) { this.model_img = model_img; this.car_id = car_id; this.car_vin = car_vin; } /* getters */ public int getmodelimg() { return model_img; } public string getvin() { return car_vin; } public string getid() { return car_id; } /* setters */ public void setmodelimg(int model_img) { this.model_img = model_img; } public void setvin(string car_vin) { this.car_vin = car_vin; } public void setid(string car_id) { this.car_id = car_id; } } }
error:
customlistviewadapter() cannot applied customlistviewadapter for: adapter = new customlistviewadapter(this,item); in oncreate
update: adapter = new customlistviewadapter(this, r.id.list_view, item); in oncreate.' still receive same (above) error.
imports:
import android.app.activity; import android.app.listactivity; import android.app.progressdialog; import android.content.context; import android.content.intent; import android.graphics.bitmap; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.provider.mediastore; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.client.utils.urlencodedutils; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.bytearrayoutputstream; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.unsupportedencodingexception; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.regex.matcher; import java.util.regex.pattern;
i having trouble understanding doing incorrect. appreciate input. thank you.
you're trying instantiate adapter has constructor
public customlistviewadapter(arraylist<item> item, int resourceid, activity context)
although you're calling this
adapter = new customlistviewadapter(this,item);
problem: arguments don't match
it should
adapter = new customlistviewadapter(item, r.layout.list_row, this);
note list_row made , should actual layout file in app or in android sdk
Comments
Post a Comment