2013-03-25 72 views

回答

0
如果創建ListView的XML文件

,你可以指定ID屬性,像這樣:android:id="@+id/listView1,每個ListView提供不同的ID。在您的Java代碼中,您需要擴展Activity並創建三個ListView對象,並將它們指向XML文件中的ID。一旦你有了ListView的句柄,你就需要爲每個ListView創建一個數據源並ArrayAdapter<String>。我傾向於使用ArrayList<String>而不是傳統的String[],因爲對我而言,它們更易於使用。下面的工作Java示例將適用於單個ListView。將另外兩個變量和對象重複複製兩次,其他兩個ListViews。希望這有助於:

public class MainListActivityExample extends Activity { 

    ListView listView1; 
    ArrayList<String> lvContents1 = new ArrayList<String>; 
    ArrayAdapter<String> lvAdapter1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Tell the method which layout file to look at 
     setContentView(R.layout.listViewExample_activity); 

     // Point the ListView object to the XML item 
     listView1 = (ListView) findViewById(R.id.listView1); 

     // Create the Adapter with the contents of the ArrayList<String> 
     lvAdapter1 = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, lvContents1); 

     // Attach the Adapter to the ListView 
     listView1.setAdapter(lvAdapter1); 

     // Add a couple of items to the contents 
     lvContents1.add("Foo"); 
     lvContents1.add("Bar"); 

     // Tell the adapter that the contents have changed 
     lvAdapter1.notifyDataSetChanged(); 
} 

爲了添加其他兩個列表視圖,創建兩個ListView對象,兩個ArrayList<String>對象,並且兩個ArrayAdapter<String>對象,每個所以你知道這屬於哪個相應的名稱。然後,您可以按照完全相同的步驟來初始化它們。

相關問題