2010-09-15 67 views
11

嗨,我是Android新手。誰能告訴我請什麼錯用下面的代碼:以編程方式創建ListView

public class ListApp extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     TextView lText = new TextView(this); 
     lText.setId(0);  

     ListView lView = new ListView(this); 
     String[] lStr = new String[]{"AA","BB", "CC"}; 
     ArrayAdapter lAdap = new ArrayAdapter(this,lText.getId(),lStr); 
     lView.setAdapter(lAdap); 
     lView.setFocusableInTouchMode(true);   

     setContentView(lView); 
    } 
} 
+0

一方面,您錯誤地使用了ArrayAdapter構造函數。你想使用兩個參數版本:(Context context,int textViewResourceId) – moonlightcheese 2011-05-27 19:35:42

+0

像這樣:ArrayAdapter lAdap = new ArrayAdapter (this,lText.getId()); – moonlightcheese 2011-05-27 19:38:47

回答

0
public class ListApp extends ListActivity 
+0

我可以添加ListView表單Activity嗎?我不想從ListActiviy繼承。可能嗎? – ashraf 2010-09-15 14:42:19

+0

我有同樣的問題,從iPhone和Blackberry移植項目,並使用Android的XML是一個重新設計...... argh。如果程序化地創建這些東西很容易,它會讓生活變得更容易,看起來應該是可能的,但嘿,我還是新來的Android :) – sradforth 2011-03-11 14:40:31

0

做一個佈局XML文件的列表視圖,並使用findViewById來設置它的適配器後,第一個內容視圖設置爲您佈局

3

試試這個..

將以下代碼粘貼到list_item.xml中。

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:textSize="16sp" android:textColor="#ffffff" android:textStyle="bold" android:background="@drawable/border_cell"> 
</TextView> 

這裏是活動類....

public class UsersListActivity extends ListActivity{  
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState);   
      String[] statesList = {"listItem 1", "listItem 2", "listItem 3"}; 
      setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, 
        statesList)); 
      ListView lv = getListView(); 

      lv.setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 


        Toast.makeText(getApplicationContext(), 
        "You selected : "+((TextView) view).getText(), Toast.LENGTH_SHORT).show(); 
    } 
      }); 

     } 

} 
1

最佳實踐是分離視圖(XML佈局)和控制器(活性)。

如果你不想這樣,儘量把

setContentView(lView); 

的onCreate

18

開始這裏的,不需要你寫任何XML佈局的解決方案。它在可能的情況下使用標準的android佈局,並且不需要通貨膨脹:

Dialog dialog = new Dialog(this); 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Select Color Mode"); 

ListView modeList = new ListView(this); 
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" }; 
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray); 
modeList.setAdapter(modeAdapter); 

builder.setView(modeList); 
dialog = builder.create(); 
+2

忽略對話框的東西。你也可以調整它以使用ListActivity。 – moonlightcheese 2011-05-27 20:06:01

+0

這應該是標記爲正確的答案 – 2015-07-29 06:06:16