1.问题:看到如下代码,在即没有setContenView()方法又没有LayoutInflater的情况下,如何在Activity中实现view的显示?
一下为Activity中关于OnCreate()方法实现的部分代码:
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
browseToRoot();
this.setSelection(0);
}
private void browseToRoot()
{
browseTo(new File("/"));
}
//浏览指定的目录,如果是文件则进行打开操作
private void browseTo(final File file)
{
this.setTitle(file.getAbsolutePath());
if (file.isDirectory())
{
this.currentDirectory = file;
fill(file.listFiles());
}
else
{
fileOptMenu(file);
}
}
//这里可以理解为设置ListActivity的源
private void fill(File[] files)
{
//清空列表
this.directoryEntries.clear();
//添加一个当前目录的选项
this.directoryEntries.add(new IconifiedText(getString(R.string.current_dir), getResources().getDrawable(R.drawable.folder)));
//如果不是根目录则添加上一级目录项
if (this.currentDirectory.getParent() != null)
this.directoryEntries.add(new IconifiedText(getString(R.string.up_one_level), getResources().getDrawable(R.drawable.uponelevel)));
Drawable currentIcon = null;
for (File currentFile : files)
{
//判断是一个文件夹还是一个文件
if (currentFile.isDirectory())
{
currentIcon = getResources().getDrawable(R.drawable.folder);
}
else
{
//取得文件名
String fileName = currentFile.getName();
//根据文件名来判断文件类型,设置不同的图标
if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingImage)))
{
currentIcon = getResources().getDrawable(R.drawable.image);
}
else if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingWebText)))
{
currentIcon = getResources().getDrawable(R.drawable.webtext);
}
else if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingPackage)))
{
currentIcon = getResources().getDrawable(R.drawable.packed);
}
else if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingAudio)))
{
currentIcon = getResources().getDrawable(R.drawable.audio);
}
else if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingVideo)))
{
currentIcon = getResources().getDrawable(R.drawable.video);
}
else
{
currentIcon = getResources().getDrawable(R.drawable.text);
}
}
//确保只显示文件名、不显示路径如:/sdcard/111.txt就只是显示111.txt
int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
this.directoryEntries.add(new IconifiedText(currentFile.getAbsolutePath().substring(currentPathStringLenght), currentIcon));
}
Collections.sort(this.directoryEntries);
IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this);
//将表设置到ListAdapter中
itla.setListItems(this.directoryEntries);
//为ListActivity添加一个ListAdapter
this.setListAdapter(itla);
}
2.答案:原来在自定义的Adapter类中重载了GetView()方法。
//重写getView方法来返回一个IconifiedTextView(我们自定义的文件布局)对象
public View getView(int position, View convertView, ViewGroup parent) {
IconifiedTextView btv;
if (convertView == null)
{
btv = new IconifiedTextView(mContext, mItems.get(position));
}
else
{
btv = (IconifiedTextView) convertView;
btv.setText(mItems.get(position).getText());
btv.setIcon(mItems.get(position).getIcon());
}
return btv;
}
这样,我们就在activity和view之间架起了一架桥梁。
3.原因:
public abstract View getView (int position, View convertView, ViewGroup parent)
Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use inflate(int, android.view.ViewGroup, boolean)
to specify a root view and to prevent attachment to the root.
参数
position | 要从适配器中取得的视图的位置. |
---|---|
convertView | The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. |
parent | The parent that this view will eventually be attached to |
返回值
- A View corresponding to the data at the specified position.