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

时间:2010-02-11 03:48:07

标签: android listview

我是Android的新手,我真的需要这样做(我考虑过在另一个Activity中执行此操作),但任何人都可以向我展示一个简单的代码(只有{{1}没有onCreate()可以Listview的情况?

感谢

5 个答案:

答案 0 :(得分:55)

如果你有活动的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();
      }
});

答案 1 :(得分:28)

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

答案 2 :(得分:11)

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

答案 3 :(得分:1)

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

答案 4 :(得分:1)

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

}

}