Android 如何在没有 ListActivity 的情况下实现 ListView?(仅使用活动)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2242136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:08:03  来源:igfitidea点击:

How can I implement a ListView without ListActivity? (use only Activity)

androidlistview

提问by TIMEX

I'm new to Android, and I really need to do it this way (I've considered doing it in another Activity), but can anyone show me a simple code (just the onCreate()method) that can do Listviewwithout ListActivity?

我是 Android 新手,我真的需要这样做(我已经考虑过在另一个中这样做Activity),但是谁能告诉我一个简单的代码(只是onCreate()方法),可以Listview不用它ListActivity吗?

THanks

谢谢

回答by Patrick Kafka

If you have an xml layout for the activity including a listView like this

如果您有活动的 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"

Then in your onCreate you could have something like this

然后在你的 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();
      }
});

回答by evilspacepirate

Include the following resource in your res/layout/main.xml file:

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

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

your_class.java

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);
  }
}

回答by ccheneson

The following creates a simple ListView programmatically:

下面以编程方式创建一个简单的 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);
}

回答by mobilekid

You could also reference your layout, instantiate a layout object from your code, and then build the ListView in Java. This gives you some flexability in terms of setting dynamic height and width at runtime.

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

回答by Rakesh Rangani

include the following resource file in your res/layout/main.xml

在 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

主活动.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);

}

}