2011-12-12 28 views
-3

如何僅使用數組適配器和getview創建簡單列表視圖。在Android中不使用ListActivity和setAdapter。請詳細說明示例代碼。在任何活動類中動態創建帶有Cutom適配器的ListView

編輯: 我是初學者,很困惑,不熟悉堆棧溢出的問題。 其實我找到解決方案來創建一個適配器並在任何活動類中動態列表視圖。希望downvoted將重新計算!

+0

你知道你在問什麼? – ingsaurabh

+0

我編輯我的問題,後悔我的錯誤,因爲我是Android新手,被阻止在這裏提出進一步的問題......我努力幫助別人,並得到我的previledge ..只有upvote現在可以救我。 –

回答

1

也許你正在尋找?

SampleListActivity.java

package com.academy; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class SampleListActivity extends Activity { 

    private String[] mStrings = { 
       "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi", 
       "Acorn", "Adelost", "Affidelice au Chablis"}; 

    private ListView listView; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     listView = (ListView) findViewById(R.id.listView1); 
     listView.setAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, mStrings)); 

    } 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 
+0

我需要ArrayAdapter和getView也可以對列表項進行一些更進一步的操作,例如區分不同的列表項(文本顏色,圖標和全部)。怎麼做 ?? –

+0

閱讀本文http://www.vogella.de/articles/AndroidListView/article.html 因爲您需要實施自己的適配器來創建自定義列表。 – kelheor

+0

在上面的文章中,所有的例子都與istActivity一起給出,我想要沒有ListActivity的相同功能。可能嗎 ??如果是的話,怎麼樣? –