java - Set TextView text with a for loop so multiple lines show -


so have loop want output new line in textview every single time goes through. however, not sure if possible. have tablelayout in there put items vertically instead of horizontally. thought listview not sure how set text of it.

here code

    import android.app.fragment; import android.graphics.color; import android.os.bundle; import android.support.annotation.nullable; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.tablelayout; import android.widget.tablerow; import android.widget.textview;  import com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route;  import org.joda.time.datetime; import org.w3c.dom.text;  import java.io.ioexception; import java.util.map;  import okhttp3.request; import okhttp3.response;   /**  * created james singleton on 8/13/2016.  */  public class weeklydrives extends fragment implements apirequestsutil.apirequestresponselistener {     view myview;     map<string, route> drives;     private textview drivenumber;     private textview drivedistance;     private textview drivetime;     private textview drivenumlist;     private textview drivedistlist;     private textview drivetimelist;      @nullable     @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         myview = inflater.inflate(r.layout.weekly_drives, container, false);         apirequestsutil.setonnetworklistener(this);          return myview;     }       private void populateview() {         this.getactivity().runonuithread(new runnable() {             @override             public void run() {                 drives = apirequestsutil.getroutes();                  drivenumber = (textview) myview.findviewbyid(r.id.drive_number);                 drivedistance = (textview) myview.findviewbyid(r.id.drive_distance);                 drivetime = (textview) myview.findviewbyid(r.id.drive_time);                  drivenumber.settext("drive num.");                 drivedistance.settext("distance");                 drivetime.settext("time");                 int drivenum = 0;                 (map.entry drive : drives.entryset()) {                     tablerow tr = new tablerow(getactivity());                     route route = (route) drive.getvalue();                     tr.setid(drivenum++);                     //tr.setlayoutparams(new viewgroup.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content));                      datetime starttime = new datetime(route.getstart_time());                     datetime endtime = new datetime(route.getend_time());                       drivenumlist = (textview) myview.findviewbyid(r.id.drive_number_list);                     drivedistlist = (textview) myview.findviewbyid(r.id.drive_distance_list);                     drivetimelist = (textview) myview.findviewbyid(r.id.drive_time_list);                      drivenumlist.settext(drivenumlist.gettext().tostring() + string.valueof(drivenum) + system.getproperty("line.separator"));                     drivedistlist.settext(drivedistlist.gettext().tostring() + float.parsefloat(route.getlen()) / 1000 + " km" + system.getproperty("line.separator"));                     drivetimelist.settext(drivetimelist.gettext().tostring() + ((endtime.getmillis() - starttime.getmillis())/ 1000)/60 + " min" + system.getproperty("line.separator"));                 }             }         });      }      @override     public void onfailure(request request, throwable throwable) {      }      @override     public void onresponse(response response) {         populateview();     } } 

here xml:

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent">      <!--header aligned top -->     <relativelayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:id="@+id/header"         android:gravity="center"         android:background="#fc9">          <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/drive_number"             android:layout_margin="4dp"             android:textsize="20sp"             android:textcolor="#000"/>         <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/drive_distance"             android:layout_torightof="@+id/drive_number"             android:layout_margin="4dp"             android:textsize="20sp"             android:textcolor="#000"/>         <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/drive_time"             android:layout_torightof="@+id/drive_distance"             android:layout_margin="4dp"             android:textsize="20sp"             android:textcolor="#000"/>     </relativelayout>     <scrollview         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:scrollbars="vertical"         android:background="#005"         android:layout_below="@+id/header"         android:id="@+id/scrollablecontents">          <!--<tablelayout-->             <!--android:id="@+id/fragment1_tlayout"-->             <!--android:layout_width="wrap_content"-->             <!--android:layout_height="wrap_content"-->             <!--android:stretchcolumns="0,1">-->          <!--</tablelayout>-->         <linearlayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:gravity="center"             android:orientation="horizontal">         <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/drive_number_list"             android:layout_margin="4dp"             android:textsize="20sp"             android:textcolor="#cccccc" />         <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/drive_distance_list"             android:layout_torightof="@+id/drive_number_list"             android:layout_margin="4dp"             android:textsize="20sp"             android:textcolor="#cccccc"/>         <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/drive_time_list"             android:layout_torightof="@+id/drive_distance_list"             android:layout_margin="4dp"             android:textsize="20sp"             android:textcolor="#cccccc"/>         </linearlayout>     </scrollview> </relativelayout> 

drive.entryset() output

what this:[2016-07-11--08-52-18=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@12a862, 2016-07-11--09-37-46=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@50bbdf3, 2016-07-11--18-54-22=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@6d21ab0, 2016-07-12--09-15-59=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@fb72d29, 2016-07-12--09-29-29=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@776e8ae, 2016-07-12--09-33-03=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@d1a464f, 2016-07-12--09-38-56=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@5b631dc, 2016-07-12--09-41-08=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@dec72e5, 2016-07-12--09-42-39=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@acce1ba, 2016-07-12--09-44-33=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@5def86b, 2016-07-12--09-49-31=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@d628fc8, 2016-07-12--09-54-06=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@7765861, 2016-07-12--19-04-34=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@8575f86, 2016-07-12--19-39-20=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@3f2b047, 2016-07-12--19-40-27=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@df4e074, 2016-07-12--19-41-28=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@b5f199d, 2016-07-13--08-45-17=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@db9ee12, 2016-07-13--09-01-32=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@2a009e3, 2016-07-13--15-02-04=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@9b98fe0, 2016-07-14--08-46-22=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@518b299, 2016-07-14--19-22-46=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@d44d95e, 2016-07-14--19-34-02=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@d3f613f, 2016-07-14--20-16-47=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@957ca0c, 2016-07-15--08-36-28=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@b38df55, 2016-07-15--09-52-32=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@c012d6a, 2016-07-15--12-09-57=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@252d25b, 2016-07-15--12-15-07=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@6bd7af8, 2016-07-15--15-36-10=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.route@df51bd1, 

add android:inputtype="textmultiline" textview in xml.

and add data textview this:

drivenumlist.settext(drivenumlist.gettext().tostring() + string.valueof(drivenum) + system.getproperty("line.separator")); 

and on.

edit: can use "\n" instead of system.getproperty("line.separator") adding new line character.


Comments