我想用于自定义适配器的 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="@drawable/bginset3" >
<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>
</RelativeLayout>
我的列表视图 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="@drawable/bginset3" >
<ListView
android:id="@+id/lvFiles"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
我的Java代码:
package com.test.testing;
import java.io.File;
import java.util.ArrayList;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
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/");
ListView lv;
ArrayList<String> FilesInFolder = GetFiles(folder.getAbsolutePath());
lv = (ListView) mFrame3.findViewById(R.id.lvFiles);
lv.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, FilesInFolder));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Clicking on items
Toast.makeText(getActivity(), "Item " + (position + 1) + ": ID" + v, Toast.LENGTH_SHORT).show();
}
});
return mFrame3;
}
public ArrayList<String> GetFiles(String DirectoryPath) {
ArrayList<String> MyFiles = new ArrayList<String>();
File f = new File(DirectoryPath);
//f.mkdirs();
File[] files = f.listFiles();
if (files.length == 0)
return null;
else {
for (int i=0; i<files.length; i++) {
if (files[i].getName().endsWith(".tol")) {
long lastTime = files[i].lastModified();
String dateString = DateFormat.format("MM/dd/yyyy", new Date(lastTime)).toString();
String sizeString = getReadableSize(files[i].length());
String fileName = files[i].getName().substring(0, files[i].getName().lastIndexOf("."));
MyFiles.add("Trip Name: " + fileName + "\nTrip Taken On: " + dateString + "\n" + sizeString);
}
}
}
return MyFiles;
}
public String getReadableSize(long size) {
String returnSize = "";
if ((double) size / (1024 * 1024 * 1024) > 1.0) {
double i = ((double) size / (1024 * 1024 * 1024)) * 100.0;
long j = (long) i;
double k = (double) j / 100.0;
returnSize = k + " GB";
} else if ((double) size / (1024 * 1024) > 1.0) {
double i = ((double) size / (1024 * 1024)) * 100.0;
long j = (long) i;
double k = (double) j / 100.0;
returnSize = k + " MB";
} else if ((double) size / (1024) > 1.0) {
double i = ((double) size / (1024)) * 100.0;
long j = (long) i;
double k = (double) j / 100.0;
returnSize = k + " KB";
} else {
returnSize = size + " bytes";
}
return returnSize;
}
}
上面的代码产生了一个非常简单的格式:
我需要一些帮助来实现 CustomAdapter 以使用这两个 XML 文件,这样我就可以使它如下所示: