这是我的代码:
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);
lv = (ListView) mFrame3.findViewById(R.id.lvFiles);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.ptrip, FilesInFolder));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Clicking on items
}
});
/*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;
}
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++)
MyFiles.add(files[i].getName());
}
return MyFiles;
}
}
我的ptrip 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>
我收到以下错误,我会帮忙解决:
ArrayList<String> FilesInFolder = GetFiles(folder);
错误:The method GetFiles(String) in the type PreviousTrip is not applicable for the arguments (File)
已修复:ArrayList<String> FilesInFolder = GetFiles(folder.toString());
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.ptrip, FilesInFolder));
错误:ptrip cannot be resolved or is not a field
除此之外,我正在尝试在卡片视图中显示每个项目,就像Play商店中的“我的应用”一样:
答案 0 :(得分:1)
ArrayList<String> FilesInFolder = GetFiles(folder);
Error: The method GetFiles(String) in the type PreviousTrip is not applicable for the arguments (File)
Fixed: ArrayList<String> FilesInFolder = GetFiles(folder.toString());
我建议你使用folder.getName()
而不是folder.toString()
。
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.ptrip, FilesInFolder));
Error: ptrip cannot be resolved or is not a field
首先,只有android.R.layout.some_layout
是android框架的一部分才能使用some_layout
。
如果R.layout.ptrip
是您的布局之一,那么您必须在此布局中向构造函数提供id
TextView
。例如,您的初始化应如下所示:
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.ptrip,
R.id.some_textview_inside_layout_ptrip, FilesInFolder));
如果布局文件ptrip
中只有TextView,则以下内容不会导致错误:
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.ptrip, FilesInFolder));
答案 1 :(得分:1)
有很多错误。
您对列表视图项使用了错误的布局ptrip
。项目布局必须有TextView
android:id = "@android:id/text1"
才能自动使用ArrayAdapter
。
这里有一些改进的代码:
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 );
final ListView lv = (ListView) mFrame3.findViewById(R.id.lvFiles);
final File directory = new File(Environment.getExternalStorageDirectory(), "tc");
final ArrayList<String> files = GetFiles(directory);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,files);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Clicking on items
}
});
return mFrame3;
}
public ArrayList<String> GetFiles(File directory) {
ArrayList<String> files = new ArrayList<String>();
directory.mkdirs();
if(!directory.exists()){return files;}
File[] fileArray = directory.listFiles();
for (int i=0; i<fileArray.length; i++){
files.add(fileArray[i].getName());
}
return files;
}
}