以下是文件浏览器的代码。它适用于浏览设备上的文件和文件夹。我想在图像名称旁边显示图像缩略图。如何实现?如果有人可以帮助修改此代码以添加图像缩略图。感谢
public class MainActivity extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
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];
if(!file.isHidden() && file.canRead()){
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);
}
//带有ListView的主布局。
<LinearLayout 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:orientation="vertical">
<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"
/>
答案 0 :(得分:0)
试试这个,
为单个列表视图行创建布局,如下所示,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="180dp"
android:layout_height="180dp"/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout >
将所需图标放在drawable文件夹中,并使用BaseAdapter
定义扩展类public class ListAdapter extends BaseAdapter{
private Context context;
private ArrayList<MyModel> data;
public AllListAdapter(Context con, ArrayList<String> dt) {
this.context = con;
this.data = dt;
}
// other overriden methods
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
TextView name;
ImageView image;
if (row == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.layout_for_single_list_row, null);
name = (TextView) row.findViewById(R.id.textview);
image = (ImageView) row.findViewById(R.id.image);
}
else
{
name = (TextView) row.findViewById(R.id.textview);
image = (ImageView) row.findViewById(R.id.image);
}
name.setText(data.get(position).getName());
image.setBackgroundDrawable(this.context.getResources().getDrawable(<your icon>))
return row;
}
}
将数据设置为列表
ListAdapter allListadapter = new ListAdapter(this,item);
your_list.setAdapter(allListadapter);