How to implement ListView without ListActivity? (use only action)

I'm new to Android and I really need to do this (I covered it in another Activity ), but can anyone show me simple code (only the onCreate() method) that Listview can do without ListActivity ?

thank

+41
android listview
Feb 11 '10 at 3:48
source share
5 answers

If you have an xml layout for 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 might 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(); } }); 
+53
Feb 11 2018-10-11T00
source share

Include the following resource in the 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); } } 
+28
Nov 11 '11 at 18:50
source share

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); } 
+11
Feb 11 '10 at 12:01
source share

You can also reference your layout, create an instance of the layout object from your code, and then create a ListView in Java. This gives you some flexibility in terms of setting dynamic height and width at run time.

+1
Feb 11 '10 at 9:03
source share

include the following resource file in 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); } } 
+1
Jul 10 '14 at 6:46
source share



All Articles