- 使用ArrayAdapter
- 与列表项进行交互
- 使用ListActivity
- 使用CheckBoxListView
- 自定义列表
1.使用ArrayAdapter
android.widget
类 ArrayAdapter
java.lang.Object
继承者 android.widget.BaseAdapter
继承者 android.widget.ArrayAdapter
所有已实现的接口:
Adapter, Filterable, ListAdapter, SpinnerAdapter
所使用到的方法
T getItem(int position)
Get the data item associated with the specified position in the data set.
void remove(T object)
Removes the specified object from the array.
在ListView中显示字符串
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context="com.wang.listviewsample.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private ListView ls;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ls = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
adapter.add("C");
adapter.add("java");
adapter.add("C++");
ls.setAdapter(adapter);
}
}
2.与列表项进行交互——点击 显示详细信息,长按删除
package com.wang.listviewsample;
/**
* Created by Administrator on 2016/3/16.
*/
public class Person {
private String sName;
private int iAge;
public Person(String sName,int iAge){
this.sName = sName;
this.iAge = iAge;
}
//重写toString
@Override
public String toString() {
return "[姓名:" + sName +"," + "年龄:" + iAge + "]";
}
}
package com.wang.listviewsample;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private ListView ls;
private ArrayAdapter<Person> adapter;
private Person person;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ls = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1);
adapter.add(new Person("燕青",22));
adapter.add(new Person("晁盖",23));
adapter.add(new Person("宋江",30));
ls.setAdapter(adapter);
//点击某一项查看详细信息
ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//getItem() 获得某一项
new AlertDialog.Builder(MainActivity.this)
.setTitle("详细信息")
.setMessage(adapter.getItem(position).toString())
.setPositiveButton("确定",null)
.show();
}
});
//长按某一项删除
ls.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long
id) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("警告")
.setMessage("你确定要删除该项记录吗?")
.setPositiveButton("确定",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//使用remove移除
adapter.remove(adapter.getItem(position));
}
})
.setNegativeButton("取消",null)
.show();
return true;
}
});
}
}
3.使用ListActivity 使得自己的Activity继承自ListActivity ,去掉 setContentView()
package com.wang.learnlistactivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity {
private ArrayAdapter<String> adapter;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
super.setTitle("我要深入学习");
lv = (ListView) findViewById(R.id.lv);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
adapter.add("JAVA");
adapter.add("ANDROID");
adapter.add("PYTHON");
setListAdapter(adapter);
}
}