嗯,我搜索了很多信息,但我仍然没有得到它。如何在Android上使用ListView列出所有ftp文件?
是否有任何示例,或简单的编码来解释我该怎么做。
我知道如何制作样本ListView
public class MainActivity extends ListActivity {
TextView selection;
String[] items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
items=getResources().getStringArray(R.array.planets_array);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items));
selection=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView parent, View v, int position, long id){
selection.setText("Planet - "+items[position]);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我的编码可以在Eslipse上显示服务器中的文件。
public void ftpPrintFilesList(String dir_path)
{
try {
FTPFile[] ftpFiles = mFTPClient.listFiles(dir_path);
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
boolean isFile = ftpFiles[i].isFile();
if (isFile) {
Log.i(TAG, "File : " + name);
}
else {
Log.i(TAG, "Directory : " + name);
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
但我不知道如何将文件信息放入ListView。 我对编码很不好,请帮忙。谢谢。
重建编码后,显示错误,我将编码复制到那里。
MainActivity.java
public class MainActivity extends ListActivity have error implements OnClickListener {
String mString = "/";
private Context cntx = null;
TextView selection;
private ArrayList<String> fileNames;
MyFTPClient ftpclient = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, fileNames));
selection=(TextView)findViewById(R.id.selection);
cntx = this.getBaseContext();
ListView list = (ListView)findViewById(android.R.id.list);
View button5 = findViewById(R.id.button5);
button5.setOnClickListener(this);
ftpclient = new MyFTPClient();}
public void onClick(View v) {
switch(v.getId()) {
case R.id.button5:
setContentView(R.layout.ftpfile);
new Thread(new Runnable() {
public void run(){
ftpclient.ftpPrintFilesList(mString);} }).start();
break;
}
}
这是我在其他java文件中使用的方法:
public ArrayList<String> ftpPrintFilesList(String dir_path)
{
ArrayList<String> validFileNames = new ArrayList<String>();
try {
FTPFile[] ftpFiles = mFTPClient.listFiles(dir_path);
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
boolean isFile = ftpFiles[i].isFile();
if (isFile) {
validFileNames.add(name);
Log.i(TAG, "File : " + name);
}
else {
Log.i(TAG, "Directory : " + name);
}
}
} catch(Exception e) {
e.printStackTrace();
}
return validFileNames;
}
这是我的ListView页面(xml),名为ftpfile.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:textColor="#FFFFFF"
/>
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
和LogCat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ftptest/com.example.ftptest.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
抱歉,如果这太长了,但我真的需要解决问题。请帮忙谢谢。
答案 0 :(得分:1)
您已经完成了大部分编码工作。我已经编辑了一些代码
public class MainActivity extends ListActivity {
private TextView selection;
private ArrayList<String> fileNames;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fileNames = ftpPrintFilesList(pathOfFTPDirectory);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, fileNames));
selection=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView parent, View v, int position, long id){
selection.setText("Planet - "+ fileNames.get(position));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public ArrayList<String> ftpPrintFilesList(String dir_path)
{
ArrayList<String> validFileNames = new ArrayList<String>();
try {
FTPFile[] ftpFiles = mFTPClient.listFiles(dir_path);
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
boolean isFile = ftpFiles[i].isFile();
if (isFile) {
validFileNames.add(name);
Log.i(TAG, "File : " + name);
}
else {
Log.i(TAG, "Directory : " + name);
}
}
} catch(Exception e) {
e.printStackTrace();
}
return validFileNames;
}
}
修改强>
在xml中更改此内容以删除错误
android:id="@android:id/list"
到
android:id="@+id/list"