android - implementing a list within my fragment -


so started hands dirty android development , after wrestling , failing working thought id run guys , gals. hope of has solved similar problem

i have fragment layout display textview , list of items follow:

historical data item1    20 item2    30 item3    40 

this includes button on action bar implemented allow user add more items , number. having trouble achieving above behavior. able implement adapter ended displaying follow:

historical data     item1    20 historical data     item2    30 historical data     item3    40 

any ideas how above?

myfragment.xml

 <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="vertical" >      <textview         style="?android:listseparatortextviewstyle"         android:textsize="18sp"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="@string/weight_history_label" />      <relativelayout   android:layout_width="match_parent"   android:layout_height="wrap_content"  >           <textview android:id="@+id/item"             android:layout_width="match_parent"             android:layout_height="wrap_content"              android:textstyle="bold"             android:paddingleft="4dp"             android:paddingright="4dp"             android:text="weigh in date" />          <textview             android:id="@+id/amount"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_below="@id/item"              android:inputtype="numberdecimal"             android:paddingleft="4dp"             android:paddingright="4dp"             android:paddingtop="4dp"             android:text="weight amount " />    </relativelayout> </linearlayout> 

i have class of items:

items.java public class newentry {     private string mname;     private int mnumber;      public newentry(){         ....      } 

getters/setters }

then in fragment.java

myfragment.java   public class myfragment extends listfragment {      private arraylist<items> mentries;     menuitem test;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         //get entries , store them on mentries         mentries = entrieslab.get(getactivity()).getweighinentries();         sethasoptionsmenu(true);          itemadapter adapter = new itemadapter(mentries);         setlistadapter(adapter);     }      /interface talk activity     public interface onbuttonclicked {         public void onweighinbuttonclicked(view v);     }      private class itemadapter extends arrayadapter<items> {          public itemadapter(arraylist<items> items) {             super(getactivity(), 0, items);         }         @override         public view getview(int position, view convertview, viewgroup parent) {             // if weren't given view, inflate 1             if (convertview == null) {                 convertview = getactivity().getlayoutinflater()                     .inflate(r.layout.myfragment, null);             }             items c = getitem(position);             textview titledateview =                     (textview)convertview.findviewbyid(r.id.name);             titledateview.settext(c.getdate().tostring());           //  log.i("weighinfragment: ", "###current weight " + c.getdate().tostring());             textview titleweightview =                     (textview)convertview.findviewbyid(r.id.witem);              titleweightview.settext(""+c.getweight());              return convertview;          }     } } 

so implement label once , display list of items! no matter try list of : label item value label item value label item value ...

the problem inflating xml label each of list items. that's why header each time. xml inflated inside of getview supposed individual items, not label above list.

try overriding oncreateview instead of oncreate in fragment. in function can inflate xml file has label , listview. have separate xml items, has relativelayout , 2 textview children.

edit:

sorry, let me more complete. in oncreateview, inflate full fragment's xml, should include listview. since using listactivity, sure listview's id android:id/list. should like

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="vertical" >      <textview         style="?android:listseparatortextviewstyle"         android:textsize="18sp"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="@string/weight_history_label" />      <listview android:id="@id/android:list"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:background="#00ff00"         android:layout_weight="1"         android:drawselectorontop="false"/> </linearlayout> 

now create separate list_item.xml contains relativelayout:

<?xml version="1.0" encoding="utf-8"?> <relativelayout     android:layout_width="match_parent"     android:layout_height="wrap_content">      <textview android:id="@+id/item"         android:layout_width="match_parent"         android:layout_height="wrap_content"          android:textstyle="bold"         android:paddingleft="4dp"         android:paddingright="4dp"         android:text="weigh in date" />      <textview         android:id="@+id/amount"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_below="@id/item"          android:inputtype="numberdecimal"         android:paddingleft="4dp"         android:paddingright="4dp"         android:paddingtop="4dp"         android:text="weight amount " />  </relativelayout> 

finally, oncreateview place attach adapter listview:

public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     //get entries , store them on mentries     mentries = entrieslab.get(getactivity()).getweighinentries();     sethasoptionsmenu(true);      view rootview = inflater.inflate(r.layout.my_fragment, container, true);     setlistadapter(new itemadapter(mentries));     return rootview; } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -