java - How to put a ListView into a Fragment -


i'm trying put listview in fragment, app crash. followed this guide learn how put listview in activity, want put in fragment changed code bit. fragment:

public class faqfragment extends fragment {

public static fragment newinstance() {     faqfragment fragment = new faqfragment();     bundle args = new bundle();     fragment.setarguments(args);     return fragment; }  private listview listview1;  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {      view v = inflater.inflate(r.layout.fragment_faq, container, false);      element element_data[] = new element[]     {         new element(r.drawable.goccia, "goccia1"),         new element(r.drawable.goccia, "goccia2"),         new element(r.drawable.goccia, "goccia3"),         new element(r.drawable.goccia, "goccia4"),         new element(r.drawable.goccia, "goccia5")     };      elementadapter adapter = new elementadapter(getactivity(), android.r.layout.simple_list_item_1, element_data);       listview1 = (listview)v.findviewbyid(r.id.listview1);      view header = (view)getlayoutinflater(savedinstancestate).inflate(r.layout.element, null);     listview1.addheaderview(header);      listview1.setadapter(adapter);      return v; } 

this arrayadapter:

public class elementadapter extends arrayadapter<element>{  fragmentactivity context;  int layoutresourceid;     element data[] = null;  public elementadapter(fragmentactivity fragmentactivity, int layoutresourceid, element[] data) {     super(fragmentactivity, layoutresourceid, data);     this.layoutresourceid = layoutresourceid;     this.context = fragmentactivity;     this.data = data; }  @override public view getview(int position, view convertview, viewgroup parent) {     view row = convertview;     weatherholder holder = null;      if(row == null)     {         layoutinflater inflater = ((fragmentactivity)context).getlayoutinflater();         row = inflater.inflate(layoutresourceid, parent, false);          holder = new weatherholder();         holder.imgicon = (imageview)row.findviewbyid(r.id.imgicon);         holder.txttitle = (textview)row.findviewbyid(r.id.txttitle);          row.settag(holder);     }     else     {         holder = (weatherholder)row.gettag();     }      element element = data[position];     holder.txttitle.settext(element.title);     holder.imgicon.setimageresource(element.icon);      return row; }  static class weatherholder {     imageview imgicon;     textview txttitle; } 

}

and element:

public class element { public int icon; public string title; public element(){     super(); }  public element(int icon, string title) {     super();     this.icon = icon;     this.title = title; } 

}

the app has 3 tabs, app crash when swipe tab code i've wrote (fragment).

in logcat there error:

03-13 11:25:39.455: e/androidruntime(2840): @ com.example.bluhackathon.elementadapter.getview(elementadapter.java:47)

i tried delete lines 47, 48 , 49 of arrayadapter , app didn't crash (of course there list empty elements.

lines 47, 48, 49:

element element = data[position];     holder.txttitle.settext(element.title);     holder.imgicon.setimageresource(element.icon); 

you can use listfragment implement it, below answer implement "put listview fragment, , data mysql", wrote yesterday , hope can solve problem.

    package com.ecnu.vendingmachine;  import java.util.arraylist; import java.util.hashmap; import java.util.list;  import org.apache.http.namevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject;  import android.app.listfragment; import android.app.progressdialog; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.listview; import android.widget.simpleadapter;  import com.ecnu.vendingmachine.widgets.jsonparser;  public class vendingfragment extends listfragment {      private string tag = "vendingfragment";     private static final string tag_success = "success";      private listview listview;      // progress dialog     private progressdialog pdialog;     // creating json parser object     jsonparser jsonparser = new jsonparser();     jsonarray vendingmachine = null;      arraylist<hashmap<string, string>> vendinglist;      // url products list     mainactivity main = new mainactivity();     private string url_all_vendingmachine = main.getip()             + "vendingmachine/get_all_vendingmachine.php";      // products jsonarray      @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {          return inflater.inflate(r.layout.vending_main, container, false);     }       @override       public void onviewcreated (view view, bundle savedinstancestate) {          vendinglist = new arraylist<hashmap<string, string>>();         //listview = (listview) view.findviewbyid(r.id.list);          listview = getlistview();         new get_all_vendingmachine().execute();     }         class get_all_vendingmachine extends asynctask<string, string, string> {          /**          * before starting background thread show progress dialog          * */         @override         protected void onpreexecute() {             super.onpreexecute();             pdialog = new progressdialog(getactivity());             pdialog.setmessage("creating product..");             pdialog.setindeterminate(false);             pdialog.setcancelable(true);             pdialog.show();             log.i(tag, "pdialog");         }          protected string doinbackground(string... args) {              // building parameters             list<namevaluepair> params = new arraylist<namevaluepair>();              log.i(tag, url_all_vendingmachine);             // getting json object             // note create product url accepts post method              jsonobject json = jsonparser.makehttprequest(                     url_all_vendingmachine, "get", params);              // check log cat response             log.i(tag, json.tostring());              // check success tag             try {                 int success = json.getint(tag_success);                  if (success == 1) {                     // ada record data (success = 1)                     // getting array of vendingmachine                     vendingmachine = json.getjsonarray("vendings");                      // looping through vendingmachine                     (int = 0; < vendingmachine.length(); i++) {                         jsonobject c = vendingmachine.getjsonobject(i);                          // storing each json item in variable                         string id = c.getstring("vmid");                         string name = c.getstring("name");                         string address = c.getstring("address");                         log.i(tag, id);                         log.i(tag, name);                         log.i(tag, address);                         // creating new hashmap                         hashmap<string, string> map = new hashmap<string, string>();                         // adding each child node hashmap key => value                         map.put("vmid", id);                         map.put("name", name);                         map.put("address", address);                          // adding hashlist arraylist                         vendinglist.add(map);                     }                  } else {                     // failed create product                     getactivity().finish();                 }             } catch (jsonexception e) {                 e.printstacktrace();             }              return null;         }          /**          * after completing background task dismiss progress dialog          * **/         protected void onpostexecute(string file_url) {             // dismiss dialog once done             pdialog.dismiss();             getactivity().runonuithread(new runnable() {                 @override                 public void run() {                     // updating listview                     // hashmap<string, string>中的key                     string[] = { "name", "address", "vmid" };                     // list_item.xml中对应的控件id                     int[] = { r.id.vending_name, r.id.vending_address,                             r.id.vending_id };                     log.i(tag, from[0]);                     simpleadapter adapter = new simpleadapter(getactivity(),                             vendinglist, r.layout.vending_list, from, to);                     listview.setadapter(adapter);                 }             });         }      }  } 

the vending_main_xml:

    <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:background="@color/whitecolor" >      <include         android:id="@+id/layout_buy_title_actionbar"         layout="@layout/headbar_title" />      <listview         android:id="@id/android:list"         android:layout_below="@id/layout_buy_title_actionbar"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:cachecolorhint="@android:color/transparent"         android:divider="@drawable/reader_item_divider"         android:listselector="@android:color/transparent" >     </listview>  </relativelayout> 

the vending_list.xml:

    <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="@dimen/selectvmlistcell_height"     android:orientation="vertical" >      <relativelayout         android:layout_width="fill_parent"         android:layout_height="@dimen/selectvmlistcell_height"         android:layout_gravity="center_vertical"         android:background="@drawable/bg_listview"         android:gravity="center_vertical"         android:paddingleft="@dimen/layout_margin_10" >          <linearlayout             android:id="@+id/imagetypelayout"             android:layout_width="@dimen/smallimage_hw_45"             android:layout_height="fill_parent"             android:layout_alignparentleft="true"             android:layout_centervertical="true"             android:gravity="center_vertical"             android:orientation="vertical" >              <imageview                 android:id="@+id/vendingimage"                 style="@style/smallimagestyle"                 android:src="@drawable/buy_main_not_shop" />              <textview                 android:id="@+id/distancetext"                 style="@style/smalltxtstyle"                 android:layout_gravity="center_horizontal"                 android:gravity="center"                 android:singleline="true"                 android:textcolor="@color/lightorangecolor" />         </linearlayout>          <relativelayout             android:id="@+id/vendingsave"             android:layout_width="47.0dip"             android:layout_height="fill_parent"             android:layout_alignparentright="true"             android:layout_centervertical="true" >              <imageview                 android:id="@+id/vendingsaveimage"                 android:layout_width="27.0dip"                 android:layout_height="27.0dip"                 android:layout_centerinparent="true"                 android:layout_centervertical="true"                 android:background="@drawable/bg_favor_btn" />         </relativelayout>          <linearlayout             android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:layout_centervertical="true"             android:layout_marginleft="@dimen/layout_margin_10"             android:layout_toleftof="@id/vendingsave"             android:layout_torightof="@id/imagetypelayout"             android:gravity="center_vertical"             android:orientation="vertical" >              <textview                 android:id="@+id/vending_name"                 style="@style/middarkgraytxtstyle"                 android:layout_width="fill_parent" />              <textview                 android:id="@+id/vending_address"                 style="@style/smalllightgraytxtstyle"                 android:layout_width="fill_parent"                 android:maxlines="2" />              <linearlayout                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:orientation="horizontal" >                  <textview                     style="@style/smalllightgraytxtstyle"                     android:text="number:" />                  <textview                     android:id="@+id/vending_id"                     style="@style/smalllightgraytxtstyle" />             </linearlayout>         </linearlayout>     </relativelayout>  </linearlayout> 

hope can find how put listview fragment. element in listview complex, if want single row, it's more simple code.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -