开发者

How to create listview in android? How many methods to be used?

开发者 https://www.devze.com 2023-02-10 06:54 出处:网络
I want to know about basics to create ListView. How many methods to use the create ListView. 开发者_如何转开发Hey i too new to android, there are two different ways to implement listview. 1) we can a

I want to know about basics to create ListView. How many methods to use the create ListView. 开发者_如何转开发


Hey i too new to android, there are two different ways to implement listview. 1) we can assign listview by giving values in main.xml.

 <ListView android:id="@+id/ListView01"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />


public class ListviewExample extends Activity
{
private ListView lv1;
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
}
}

2) In second method, we can assign values in string.xml.

<?xml version="1.0" encoding="utf-8"?>
  <resources>
<string-array name="countries_array">
    <item>Bahrain</item>
    <item>Bangladesh</item>
    <item>Barbados</item>
    <item>Belarus</item>
    <item>Belgium</item>
    <item>Belize</item>
    <item>Benin</item>
   </string-array>
   </resources>

String[] countries = getResources().getStringArray(R.array.countries_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));


This tutorial should guide you

http://developer.android.com/resources/tutorials/views/hello-listview.html


Take a look at the ApiDemos sample code, particularly the group of examples dealing with list views (follow the Views link and scroll down).

0

精彩评论

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