【问题标题】:How can I implement a ListView without ListActivity? (use only Activity)如何在没有 ListActivity 的情况下实现 ListView? (仅使用活动)
【发布时间】:2010-02-11 03:48:06
【问题描述】:

我是 Android 新手,我真的需要这样做(我考虑过在另一个 Activity 中这样做),但谁能给我看一个简单的代码(只是 onCreate() 方法)可以在没有ListActivity 的情况下做Listview 吗?

谢谢

【问题讨论】:

    标签: android listview


    【解决方案1】:

    如果你有一个活动的 xml 布局,包括这样的 listView

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    
    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="fill_parent"
    

    然后在你的 onCreate 你可以有这样的东西

    setContentView(R.layout.the_view);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, myList);
    ListView lv = (ListView)findViewById(android.R.id.list);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener()
    {
         @Override
         public void onItemClick(AdapterView<?> a, View v,int position, long id) 
         {
              Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_LONG).show();
          }
    });
    

    【讨论】:

    • 如何以及在何处定义 OnItemClick 方法?
    • 这里没有对adapter 有帮助的定义
    • @PascalKlein 来自它是一个覆盖函数
    • @Webnet new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, String items[])
    【解决方案2】:

    在您的 res/layout/main.xml 文件中包含以下资源:

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

    your_class.java

    import android.widget.ListView;
    import android.widget.ArrayAdapter;
    
    public class your_class extends Activity
    {
      private ListView m_listview;
    
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        m_listview = (ListView) findViewById(R.id.id_list_view);
    
        String[] items = new String[] {"Item 1", "Item 2", "Item 3"};
        ArrayAdapter<String> adapter =
          new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
    
        m_listview.setAdapter(adapter);
      }
    }
    

    【讨论】:

      【解决方案3】:

      以下以编程方式创建一个简单的 ListView:

      public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               String[] myList = new String[] {"Hello","World","Foo","Bar"};              
               ListView lv = new ListView(this);
               lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList));
               setContentView(lv);
      }
      

      【讨论】:

      • 嗨 Cchenesson...当我按照此代码操作时,我的 Eclipse 中出现“编辑源代码”(这意味着它不起作用。)这是为什么?
      • 奇怪 - 我创建了一个新的 android 项目,复制/粘贴上面的内容,运行它没有任何问题。您可以发布活动的整个代码吗?您在 Eclipse 中哪里看到“编辑源代码”?
      • 我认为它适用于 ListActivity 而不是 Activity
      【解决方案4】:

      您还可以引用您的布局,从您的代码中实例化一个布局对象,然后在 Java 中构建 ListView。这为您在运行时设置动态高度和宽度方面提供了一些灵活性。

      【讨论】:

        【解决方案5】:

        在 res/layout/main.xml 中包含以下资源文件

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
              <ListView
                 android:id="@+id/listView"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
              </ListView>
        </RelativeLayout>
        

        MainActivity.java

        public class MainActivity extends Activity {
        ListView listView;
        String[] listPlanet={"mercury","Venus","Mars","Saturn","Neptune"};
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        
            listView = (ListView)findViewById(R.id.listView));
        
            ArrayAdapter<String> adapter =
          new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listPlanet);
        
          listview.setAdapter(adapter);
        
        }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2020-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-27
          相关资源
          最近更新 更多