开发者

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

开发者 https://www.devze.com 2022-12-20 00:44 出处:网络
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 L

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 Listview without ListActivity?

THanks


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

<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

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


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

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


The following creates a simple ListView programmatically:

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


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.


include the following resource file in your 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);

}

}
0

精彩评论

暂无评论...
验证码 换一张
取 消