I'm trying to convert an URL
sent from my SQLite
to my ListFragment
, the thing is when I try to parse it and put it on my ListView
it doesn't works, it worked fine without using the Drawable
item, because I've tried it before without the Drawable
and it worked for me... but at the time to try to put the image from URL
on my ListFragment
, it crashes.
What I'm trying to do is :
I've created a variables to put the data on it :
Drawable FOTO;
String Title, percent, data_f;
Integer Preu;
String URLTest;
Then, I tried to parse all of the info of the query
:
c = db.rawQuery("Select NOM_OFER,PREU_OFERTA,DATA_F,FOTO,PERCENTDESCOMPTE from T_OFERTA", null);
c.moveToFirst();
if (c != null) {
do {
for (int i = 0; i < c.getColumnCount(); i++) {
Title = c.getString((c.getColumnIndex("NOM_OFER")));
Preu = c.getColumnIndex("PREU_OFERTA");
percent = c.getString((c.getColumnIndex("PERCENTDESCOMPTE")));
data_f = c.getString((c.getColumnIndex("DATA_F")));
URLTest = c.getString((c.getColumnIndex("FOTO")));
FOTO = Imagehandler(URLTest);
Log.e("", "" + c.getString(i));
Toast.makeText(getActivity(), "Title" + Title + "Preu" + Preu + "Percent" + percent + "Cheese is " + data_f, Toast.LENGTH_LONG).show();
}
}while (c.moveToNext());
}
c.close();
And then for try a fast test I created my ListFragment
like this :
mItems.add(new ListViewItem(FOTO, Title, Preu.toString(), percent, data_f));
To convert the url to Drawable
I've used this method (from hotveryspicy answer):
protected Drawable Imagehandler(String url) {
try {
url=url.replaceAll(" ", "%20");
InputStream is = (InputStream)this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e)
{
System.out.println(url);
System.out.println("error at URI"+e);
return null;
}
catch (IOException e)
{
System.out.println("io exception: "+e);
System.out.println("Image NOT FOUND");
return null;
}
}
protected Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
And this is the following error on my LogCat
:
Process: info.androidhive.slidingmenu, PID: 2306
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
at java.net.URLConnection.getContent(URLConnection.java:190)
at java.net.URL.getContent(URL.java:455)
at info.androidhive.slidingmenu.MisOfertasFragment.fetch(MisOfertasFragment.java:239)
at info.androidhive.slidingmenu.MisOfertasFragment.Imagehandler(MisOfertasFragment.java:220)
at info.androidhive.slidingmenu.MisOfertasFragment.onCreate(MisOfertasFragment.java:89)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:283)
at android.view.View.dispatchAttachedToWindow(View.java:12620)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2458)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1241)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1024)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5796)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
The LogCat is pointing this line of code :
Object content = url.getContent();
This one :
InputStream is = (InputStream)this.fetch(url);
And this one :
FOTO = Imagehandler(URLTest);
I don't know what I'm doing wrong... Also I've got a question, what is the best way to fetch the data into my ListView
? Shall I put the mItems.add
.... inside of my for loop
?
Aucun commentaire:
Enregistrer un commentaire