(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)
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 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.