(Android)Adapter类的GetView()方法的奇妙之处

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)

引入自: API 级别1

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要从适配器中取得的视图的位置.
convertViewThe 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.
parentThe parent that this view will eventually be attached to

返回值

  • A View corresponding to the data at the specified position.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值