Android教程(九)-- ListView控件的使用
今天我们来说说这个ListView控件的使用,ListView其实在任何一款APP(大型)里都会用到的,当然高级的可能是自定义的,但是它们也是继承了ListView这个父类的。
好了我们就直奔主题吧。
第一步,建立一个工程Ep.ListView,其余命名不更改。
第二步,在视图上添加一个ListView:
[java]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1" >
</ListView>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1" >
</ListView>
</RelativeLayout>
第三步,写核心代码:
[java]
package com.example.ep.listview;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
static final String[] COUNTRIES = new String[]{
"Americian","Belize","China","Japan","Korean","Russian"
};
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.易做图_list_item_1
,COUNTRIES));
}
private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
// TODO Auto-generated method stub
lv.setAdapter(arrayAdapter);
}
}
package com.example.ep.listview;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
static final String[] COUNTRIES = new String[]{
"Americian","Belize","China","Japan","Korean","Russian"
};
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.易做图_list_item_1
,COUNTRIES));
}
private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
// TODO Auto-generated method stub
lv.setAdapter(arrayAdapter);
}
}
这里主要是把一个已经定义好的数组的值赋给这个ListView,我们还用到了列表适配器。
最后上一个结果图:
补充:移动开发 , Android ,