如何在Android中使用目录和存储卡中的文件填充ListView。 请推荐一些链接或书籍。使用文件填充Android ListView
-1
A
回答
2
试试这个页面:http://android-er.blogspot.hu/2010/01/implement-simple-file-explorer-in.html
复制的代码,如果该链接会死在未来:
Row.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowtext"
android:layout_width="fill_parent"
android:layout_height="25px"
android:textSize="23sp" />
的main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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/path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No Data"
/>
</LinearLayout>
AndroidExplorer.java:
package com.AndroidExplorer;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class AndroidExplorer extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root="/";
private TextView myPath;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myPath = (TextView)findViewById(R.id.path);
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
}
0
从this链接:
使用java.io.File
类从目录中读取文件:
String dirPath = "some directory full path goes here";
File f = new File(dirPath);
File[] files = f.listFiles();
然后你就可以完成,其中列出了ListView
文件相同的链接您的教程。
有关ListView
填充的另一个快速链接,您可以尝试Vogella tutorial。
编辑:
如果你是新来的Android(从您的文章,你可能也可以),尝试一些tutorials从Lars Vogel。他们写得很好,很容易理解。
1
相关问题
- 1. Android:使用CustomAdapter填充ListView
- 2. 填充android listview Android
- 3. android listview不填充
- 4. android - 用数组填充ListView
- 5. 从XML文件填充ListView
- 6. 如何在android中使用“动态”视频文件填充listview?
- 7. Android - 使用光标填充ListView
- 8. Android使用ViewPagerAdapter中的CursorAdapter填充ListView
- 9. Android使用multipe游标填充listview
- 10. 使用NavigationParameters填充ListView?
- 11. 使用SQLite填充ListView
- 12. 使用sqlite db填充listview
- 13. 使用ORMLite填充ListView
- 14. 使用asyncTask填充ListView
- 15. 使用Jsoup填充ListView
- 16. 使用数组填充ListView
- 17. Android listview没有被填充
- 18. 填充的ListView的Android
- 19. 动态填充android listview
- 20. 的Android Baseadapter填充的ListView
- 21. Android ListView填充字符串[]
- 22. Android CursorAdapter ListView未填充
- 23. Listview在Android中填充
- 24. 如何填充Android ListView Dyanamically
- 25. Android的 - 填充的ListView控件
- 26. 用Android中的光标填充ListView
- 27. 用Parse.com中的对象填充ListView Android
- 28. 从XML文件填充datagrid或listview
- 29. ListView将不会填充(来自文件)
- 30. ASP.NET ListView填充
你到目前为止尝试过什么? –
这已经被问及和实施了!倍。 (n接近+ inf) – Nima