(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 要從適配器中取得的視圖的位置.
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.

發佈了49 篇原創文章 · 獲贊 52 · 訪問量 42萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章