使用 for 循环设置 TextView 文本,以便显示多行
Set TextView text with a for loop so multiple lines show
所以我有一个 for 循环,我想在它每次通过时在 textview 中输出一个新行。但是,我不确定这是否可能。我在那里有 TableLayout 但它垂直放置项目而不是水平放置。我想到了 ListView,但不确定如何设置它的文本。
这是我的代码
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();
}
}
这是我的 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()
输出
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,
将android:inputType="textMultiLine"
添加到XML中的TextView
。
并像这样将数据添加到 TextView 中:
driveNumList.setText(driveNumList.getText().toString() + String.valueOf(driveNum) + System.getProperty("line.separator"));
以此类推
编辑: 您可以使用 "\n"
而不是 System.getProperty("line.separator")
来添加换行符。
我会为这种类型的东西推荐一个 ListView,这样它就可以自动滚动,使用 View 回收来保持效率,清理你的代码并将其组织成易于理解的段。
要使用 ListView,您首先需要将其添加到主 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>
<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>
之后,您需要创建 ListView 适配器 class。它负责处理每个 个人 列表项的数据(for 循环中的内容)。
这是您可能会使用的一个,尽管您可能需要进行自己的调整并导入更多您自己的 classes,将您的包名称添加到顶部,等等
因此创建一个名为 DriveListAdapter.java
的新 Java class 并将以下代码放入其中:
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;
}
}
然后,创建一个新的布局文件,这将是单个列表项的样子。为列表中的每个驱动器呈现此布局。您可以调整此布局文件,以便所有内容都正确对齐。我给出了一个如何做到这一点的例子(创建一个名为 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>
最后,您只需使用这个新适配器 class.
填充您在第一步中创建的 ListView
所以使用下面的方法(填充ListView而不是使用for循环):
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);
}
});
}
首先你使用 LinearLayout 而不是 TableLayoyt
<LinearLayout
android:id="@+id/llMainLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
试试这个代码
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);
}
}
所以我有一个 for 循环,我想在它每次通过时在 textview 中输出一个新行。但是,我不确定这是否可能。我在那里有 TableLayout 但它垂直放置项目而不是水平放置。我想到了 ListView,但不确定如何设置它的文本。
这是我的代码
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();
}
}
这是我的 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()
输出
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,
将android:inputType="textMultiLine"
添加到XML中的TextView
。
并像这样将数据添加到 TextView 中:
driveNumList.setText(driveNumList.getText().toString() + String.valueOf(driveNum) + System.getProperty("line.separator"));
以此类推
编辑: 您可以使用 "\n"
而不是 System.getProperty("line.separator")
来添加换行符。
我会为这种类型的东西推荐一个 ListView,这样它就可以自动滚动,使用 View 回收来保持效率,清理你的代码并将其组织成易于理解的段。
要使用 ListView,您首先需要将其添加到主 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>
<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>
之后,您需要创建 ListView 适配器 class。它负责处理每个 个人 列表项的数据(for 循环中的内容)。
这是您可能会使用的一个,尽管您可能需要进行自己的调整并导入更多您自己的 classes,将您的包名称添加到顶部,等等
因此创建一个名为 DriveListAdapter.java
的新 Java class 并将以下代码放入其中:
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;
}
}
然后,创建一个新的布局文件,这将是单个列表项的样子。为列表中的每个驱动器呈现此布局。您可以调整此布局文件,以便所有内容都正确对齐。我给出了一个如何做到这一点的例子(创建一个名为 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>
最后,您只需使用这个新适配器 class.
填充您在第一步中创建的 ListView所以使用下面的方法(填充ListView而不是使用for循环):
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);
}
});
}
首先你使用 LinearLayout 而不是 TableLayoyt
<LinearLayout
android:id="@+id/llMainLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
试试这个代码
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);
}
}