android - gridView Layout not showning and i got black screen -
i trying make gridlayout
each block movie poster. using themoviedb.org that. think did right there still nothing showing in application can please me code.
i have 3 classes , 1 xml file
1) first class mainactivity:
package com.example.admin.movies; import android.support.v7.app.appcompatactivity; import android.os.bundle; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } }
2) second class mainfragment fragment want work , inner asynctask class show take "poster_path" json query : package com.example.admin.movies;
import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.gridview; import android.widget.toast; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; import java.util.arraylist; import java.util.arrays; /** * created admin on 11/7/2016. */ public class mainfragment extends fragment { private arraylist<string> imagesendpointslist; public mainfragment(){ } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getimagesasynctask sync = new getimagesasynctask(); sync.execute(); sethasoptionsmenu(true); } @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.activity_main,container,false); gridview gridview = (gridview)rootview.findviewbyid(r.id.grid_movies); gridview.setadapter(new imageadapter(getcontext(),imagesendpointslist)); gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { toast.maketext(getcontext(),""+position,toast.length_short).show(); } }); return rootview; } @override public void onactivitycreated(@nullable bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); } public class getimagesasynctask extends asynctask<void,void,string[]> { private string [] imagesendpoints; final private string log_tag = getimagesasynctask.class.getsimplename(); //my api key https://www.themoviedb.org/ string mykey; @override protected void onpreexecute() { super.onpreexecute(); mykey = "29a99f9dc266ab3a2108e9f9511a05ed"; } @override protected string[] doinbackground(void... params) { // these 2 need declared outside try/catch // can closed in block. httpurlconnection urlconnection = null; bufferedreader reader = null; // contain raw json response string. string forecastjsonstr = null; string api_key = "api_key"; try{ final string base_url = "http://api.themoviedb.org/3/movie/popular?"; uri builduri = uri.parse(base_url).buildupon() .appendqueryparameter(api_key,mykey).build(); url url = new url(builduri.tostring()); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("get"); urlconnection.connect(); // read input stream string inputstream inputstream = urlconnection.getinputstream(); stringbuffer buffer = new stringbuffer(); if (inputstream == null) { // nothing do. return null; } reader = new bufferedreader(new inputstreamreader(inputstream)); string line; while ((line = reader.readline()) != null) { // since it's json, adding newline isn't necessary (it won't affect parsing) // make debugging *lot* easier if print out completed // buffer debugging. buffer.append(line + "\n"); } if (buffer.length() == 0) { // stream empty. no point in parsing. return null; } forecastjsonstr = buffer.tostring(); log.v(log_tag,forecastjsonstr); return getmoviedatafromjson(forecastjsonstr); }catch (ioexception e) { log.e(log_tag, "error ", e); // if code didn't weather data, there's no point in attempting // parse it. } catch (jsonexception e) { log.e(log_tag, e.getmessage(), e); e.printstacktrace(); } { if (urlconnection != null) { urlconnection.disconnect(); } if (reader != null) { try { reader.close(); } catch (final ioexception e) { log.e(log_tag, "error closing stream", e); } } } return null; } @override protected void onpostexecute(string[] string) { super.onpostexecute(string); if(string != null) { imagesendpointslist.clear(); (string dayforecaststr : string) { imagesendpointslist.add(dayforecaststr); } } } private string[] getmoviedatafromjson(string jsonstring) throws jsonexception{ // weather information. each day's forecast info element of "list" array. final string owm_result = "results"; final string owm_path = "poster_path"; try{ jsonobject forecastjson = new jsonobject(jsonstring); jsonarray imagesarray = forecastjson.getjsonarray(owm_result); imagesendpoints = new string[imagesarray.length()]; (int = 0 ; i<imagesarray.length(); i++){ jsonobject dayforecast = imagesarray.getjsonobject(i); imagesendpoints[i] = dayforecast.getstring(owm_path); return imagesendpoints; } }catch (jsonexception e) { log.e(log_tag, e.getmessage(), e); e.printstacktrace(); } return null; } } }
3) adapter built:
package com.example.admin.movies; import android.content.context; import android.util.log; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.gridview; import android.widget.imageview; import com.squareup.picasso.picasso; import java.util.arraylist; /** * created admin on 11/7/2016. */ public class imageadapter extends baseadapter { final private string base_url = "http://image.tmdb.org/t/p/w185/"; context mcontext; private arraylist<string> endpoints; public imageadapter(context context, arraylist<string> arraylist){ mcontext = context; endpoints = arraylist; } @override public int getcount() { return endpoints.size(); } @override public object getitem(int position) { return endpoints.get(position); } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { imageview imageview; if(convertview == null){ // if it's not recycled, initialize attributes imageview = new imageview(mcontext); imageview.setlayoutparams(new gridview.layoutparams(85, 85)); imageview.setscaletype(imageview.scaletype.center_crop); imageview.setpadding(8, 8, 8, 8); }else{ imageview = (imageview)convertview; } log.v("baseurl",base_url+endpoints.get(position)); picasso.with(mcontext).load(base_url+endpoints.get(position)).into(imageview); return imageview; } }
4) , xml layout file:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.admin.movies.mainfragment"> <gridview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/grid_movies" android:layout_width="match_parent" android:layout_height="match_parent" android:numcolumns="auto_fit" android:columnwidth="90dp" android:verticalspacing="10dp" android:horizontalspacing="10dp" android:stretchmode="columnwidth" android:gravity="center" /> </framelayout>
i think problem relies on getview() of adapter, create xml imageview , use resource item each row instead of creating imageview programatically.
@override
public view getview(int position, view convertview, viewgroup parent) {
viewholderitem viewholder; if(convertview==null){ // inflate layout layoutinflater inflater = ((activity) mcontext).getlayoutinflater(); convertview = inflater.inflate(layoutresourceid, parent, false); // set viewholder viewholder = new viewholderitem(); viewholder.imageviewitem= (imageview) convertview.findviewbyid(r.id.imageview); // store holder view. convertview.settag(viewholder); }else{ viewholder = (viewholderitem) convertview.gettag(); } // object item based on position string url = base_url+endpoints.get(position); // assign values if object not null if(url!= null) { piccaso. ---.into(imageviewitem); } return convertview;
}
Comments
Post a Comment