简体   繁体   中英

Set TextView text with a for loop so multiple lines show

So I have a for loop that I want to output a new line in a textview every single time it goes through. However, I am not sure if that is possible. I have TableLayout in there but it put the items vertically instead of horizontally. I thought about ListView but not sure how to set the text of it.

Here is my 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 by 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;
                for (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 is my 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 to 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 is 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" to the TextView in the XML.

And add data into TextView like this:

driveNumList.setText(driveNumList.getText().toString() + String.valueOf(driveNum) + System.getProperty("line.separator"));

And so on.

Edit: you can use "\\n" instead of System.getProperty("line.separator") for adding the new line character.

I would recommend a ListView for this type of thing so that it is automatically scrollable, uses View recycling to maintain efficiency, cleans up your code and organizes it into digestible segments.

To use a ListView, you first need to add it to your main XML file. So that would become the following:

<?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 to 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>
    <RelativeLayout
        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">

            <ListView
                android:id="@+id/listView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

    </RelativeLayout>
</RelativeLayout>

Following that, you need to create your ListView adapter class. It is what handles what each individual list item's data is (the thing inside your for-loop).

Here is one that you might use, although you may have to make your own tweaks and import more of your own classes, add your package name to the top , etc.

So create a new Java class called DriveListAdapter.java and place the following code into it:

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

public class DriveListAdapter extends ArrayAdapter<Map.Entry> {

    private final Activity context;
    // you may need to change List to something else (whatever is returned from drives.entrySet())
    private final List<Map.Entry> drives;

    // may also need to change List here (above comment)
    public DriveListAdapter(Activity context, List<Map.Entry> drives) {
        super(context, R.layout.drive_list_item, drives);
        this.context = context;
        this.drives = drives;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.drive_list_item, null, true);

        Map.Entry drive = this.drives.get(position);

        // position is the index of the drives.entrySet() array
        int driveNum = position + 1;

        // need to import your Route class
        Route route = (Route) drive.getValue();

        // need to import your DateTime class
        DateTime startTime = new DateTime(route.getStart_time());
        DateTime endTime = new DateTime(route.getEnd_time());

        TextView driveNumList = (TextView) rowView.findViewById(R.id.Drive_Number_List);
        TextView driveDistList = (TextView) rowView.findViewById(R.id.Drive_Distance_List);
        TextView driveTimeList = (TextView) rowView.findViewById(R.id.Drive_Time_List);

        driveNumList.setText(String.valueOf(driveNum));
        driveDistList.setText(Double.parseDouble(route.getLen()) / 1000 + " km");
        driveTimeList.setText((endTime.getMillis() - startTime.getMillis())/ 1000 + " s");

        return rowView;
    }
}

Then, create a new layout file, which will be what the individual list item looks like. This layout is rendered for each and every drive in the list. You can adjust this layout file so that everything is aligned properly. I gave an example of how to do that (create a layout file called drive_list_item.xml ):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:textSize="20sp"
        android:textColor="#CCCCCC"
        android:text="Medium Text"
        android:id="@+id/Drive_Number_List"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:textSize="20sp"
        android:textColor="#CCCCCC"
        android:text="Medium Text"
        android:id="@+id/Drive_Distance_List"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:textSize="20sp"
        android:textColor="#CCCCCC"
        android:text="Medium Text"
        android:id="@+id/Drive_Time_List"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

And lastly, you just need to populate that ListView you created in the first step with this new adapter class.

So use the following method (populates the ListView instead of using the for loop):

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 Number");
            driveDistance.setText("Drive Distance");
            driveTime.setText("Drive Time");

            // edit: you need to generate your List data from the entrySet
            // the ArrayAdapter cannot take a Set argument - needs to be a List
            List<Map.Entry> list = new ArrayList<Map.Entry>();
            for (Map.Entry drive : drives.entrySet()) {
                list.add(drive);
            }

            // populate the ListView
            // may need to change "getActivity()" to something else
            // this constructor needs the "this" context of the activity 
            DriveListAdapter drivesListAdapter = new DriveListAdapter(getActivity(), list);
            ListView listView = (ListView)findViewById(R.id.listView);
            listView.setAdapter(drivesListAdapter);
        }
    });
}

first of all you use LinearLayout instead of TableLayoyt

<LinearLayout
        android:id="@+id/llMainLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

try this code

LinearLayout llMain=(LinearLayout) myView.findViewById(R.id.llMainLayout);
for (Map.Entry drive : drives.entrySet()) {
            LinearLayout tr = new LinearLayout(getActivity());
                  tr.setOrientation(1); // horizontal
            Route route = (Route) drive.getValue();
            tr.setId(driveNum++);
            // set params 
            DateTime startTime = new DateTime(route.getStart_time());
            DateTime endTime = new DateTime(route.getEnd_time());

            TextView driveNumList = new TextView(); 
            TextView driveDistList = new TextView(); 
            TextView driveTimeList = new TextView(); 

            driveNumList.setText(String.valueOf(driveNum));
            driveDistList.setText(Double.parseDouble(route.getLen()) / 1000 + " km");
            driveTimeList.setText((endTime.getMillis() - startTime.getMillis())/ 1000 + " s");
            tr.addView(driveNumList);
            tr.addView(driveDistList);
            tr.addView(driveTimeList);

          llMain.addView(tr);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM