我有以下 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@layout/bginset3" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="false"
android:layout_marginTop="0dp"
android:layout_marginLeft="0dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@drawable/forfileicon" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" >
<ImageView
android:id="@+id/imgFileIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:background="@drawable/forfileicon" />
<TextView
android:id="@+id/tvFN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:paddingTop="10dp"
android:textColor="#00A21E"
android:textSize="14dp"
android:paddingLeft="25dp"
android:layout_toRightOf="@+id/imgFileIcon" />
<TextView
android:id="@+id/tvMD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:paddingTop="10dp"
android:textColor="#00A21E"
android:textSize="14dp"
android:paddingLeft="25dp"
android:layout_below="@+id/tvFN"
android:layout_toRightOf="@+id/imgFileIcon" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
以及以下 Java 代码:
package com.test.testing;
import java.io.File;
import java.util.Date;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class PreviousTrip extends Fragment {
int i;
String k = "";
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
final RelativeLayout mFrame3 = (RelativeLayout) inflater.inflate( R.layout.ptrip, container, false );
File folder = new File(Environment.getExternalStorageDirectory() + "/tc/");
File[] filelist = folder.listFiles();
String[] theNamesOfFiles = new String[filelist.length];
if (filelist.length > 0) {
for (i = 0; i < theNamesOfFiles.length; i++) {
long lastTime = filelist[i].lastModified();
String dateString = DateFormat.format("MM/dd/yyyy", new Date(lastTime)).toString();
theNamesOfFiles[i] = filelist[i].getName();
k = k + String.valueOf(theNamesOfFiles[i]) + " " + dateString + "\n";
}
Toast.makeText(getActivity(), k, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getActivity(), "no files", Toast.LENGTH_SHORT).show();
}
return mFrame3;
}
}
我想做的是使用Java,根据文件夹中有多少文件,我想tvFN
用作文件名和tvMD
文件修改日期。我的 Java 代码能够检索文件名和修改日期,但是如何修改我的脚本,以便如果文件夹有三个文件,它会<linearlayout></linearlayout>
根据类似这样的内容自动添加每个:files.length();