我们新建一个Person类,定义用户名和电话,
public class Person {
private String name;
private String phone;
}
然后我们在其中使用shift+alt+s快速生成这两个变量的构造函数,并对其进行实例化,
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (phone == null) {
if (other.phone != null)
return false;
} else if (!phone.equals(other.phone))
return false;
return true;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(String name, String phone) {
super();
this.name = name;
this.phone = phone;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "person [name=" + name + ", phone=" + phone + "]";
}
这里我们需要试用下hashcode来确定姓名和电话为同一个对象
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((phone == null) ? 0 : phone.hashCode());
return result;
然后我们新建一个管理电话号码姓名的集合的类personManage,在里面自定义一个静态方法块并且new一个集合persons
public class personManage {
private static List<Person> persons;
static {
persons = new ArrayList<Person>();
persons.add(new Person("zhang" , "4444444444"));
persons.add(new Person("zhan" , "4"));
persons.add(new Person("zha" , "444"));
persons.add(new Person("zh" , "44"));
persons.add(new Person("z" , "4444"));
persons.add(new Person("hang" , "44444"));
persons.add(new Person("ang" , "444444"));
persons.add(new Person("ng" , "44444444"));
persons.add(new Person("g" , "4444444"));
persons.add(new Person("a" , "444444444"));
}
这里我们集合的属性使用的是private私有的,否则每次调用personManage都会创建一个新的集合,私有的话一个类方法对应一个集合,不会产生这种情况。
接下来我们构建两个方法,查找联系人和删除,
public List<Person> findAllPersons(){
return persons;
}
public void deletePerson(String name ){
Iterator<Person> ps = persons.iterator();
if (ps.hasNext()) {
if (ps.next().getName().equals("name")) {
ps.remove();
}
}
这里我们用if (ps.next().getName().equals("name")) {
ps.remove();来判断是否删除对象,我们没设置id所以我们用name作为关键字来判断
注意:遍历过程中不能删除,只能使用内部方法删除,
接下来我们修改布局,使每一行峻峰成四等份,否则用户名长度不同,会影响控件的层次和美观
<TextView
android:id="@+id/name"
android:layout_weight="0"
android:layout_height="wrap_content"
android:text="mjl"
然后我们在主函
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView != null) {
convertView= View.inflate(MainActivity.this, R.layout.activity_main, null);
}
// TODO Auto-generated method stub
// ImageView image = new ImageView (MainActivity.this);
// image .setImageResource(R.drawable.ic_launcher);
// System.out.println("getView"+position);
// System.out.println("getView" + position);
// View view = View.inflate(MainActivity.this, R.layout.activity_main, null);
// return image;
TextView name = (TextView) convertView.findViewById(R.id.name);
name.setText(persons.get(position).getName());
if (position % 2 == 0) {
linearlayout = convertView.findViewById(R.id.linear);
linearlayout.setBackgroundColor(color.holo_blue_light);
}
return convertView;
}
数中newpersonManage或者一个集合
private personManage manage = new personManage();
然后修改适配器,在getCount中获取行数
@Override
public int getCount() {
// TODO Auto-generated method stub
return manage.findAllPersons().size();
}
然后修改getView,这里是重点,当我们滑动列表,隐藏的进入缓存,用getView显示出来新的list,当返回显示以前的list时我们直接从缓存调用,
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView != null) {
convertView= View.inflate(MainActivity.this, R.layout.activity_main, null);
}
TextView name = (TextView) convertView.findViewById(R.id.name);
name.setText(persons.get(position).getName());
if (position % 2 == 0) {
linearlayout = convertView.findViewById(R.id.linear);
linearlayout.setBackgroundColor(color.holo_blue_light);
}
return convertView;
}
下面是全部代码MainActivity
package com.example.listview;
import java.util.List;
import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity {
// String[] strs = {"aa","xx","ss","dd","ff","gg","hh","jj"};
// private ListView listview;
private personManage manage = new personManage();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// listview= (ListView) findViewById(R.id.list_view);
// listview.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, strs));
// listview.setOnItemClickListener(listener);
// listview.setAdapter(new MyAdapter());
}
public class MyAdapter extends BaseAdapter {
private View linearlayout;
private List<Person> persons = manage.findAllPersons();
@Override
public int getCount() {
// TODO Auto-generated method stub
return persons.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView != null) {
convertView= View.inflate(MainActivity.this, R.layout.activity_main, null);
}
// TODO Auto-generated method stub
// ImageView image = new ImageView (MainActivity.this);
// image .setImageResource(R.drawable.ic_launcher);
// System.out.println("getView"+position);
// System.out.println("getView" + position);
// View view = View.inflate(MainActivity.this, R.layout.activity_main, null);
// return image;
TextView name = (TextView) convertView.findViewById(R.id.name);
name.setText(persons.get(position).getName());
if (position % 2 == 0) {
linearlayout = convertView.findViewById(R.id.linear);
linearlayout.setBackgroundColor(color.holo_blue_light);
}
return convertView;
}
}
}
person
package com.example.listview;
public class Person {
private String name;
private String phone;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((phone == null) ? 0 : phone.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (phone == null) {
if (other.phone != null)
return false;
} else if (!phone.equals(other.phone))
return false;
return true;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(String name, String phone) {
super();
this.name = name;
this.phone = phone;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "person [name=" + name + ", phone=" + phone + "]";
}
}
personManage
package com.example.listview;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class personManage {
private static List<Person> persons;
static {
persons = new ArrayList<Person>();
persons.add(new Person("zhang" , "4444444444"));
persons.add(new Person("zhan" , "4"));
persons.add(new Person("zha" , "444"));
persons.add(new Person("zh" , "44"));
persons.add(new Person("z" , "4444"));
persons.add(new Person("hang" , "44444"));
persons.add(new Person("ang" , "444444"));
persons.add(new Person("ng" , "44444444"));
persons.add(new Person("g" , "4444444"));
persons.add(new Person("a" , "444444444"));
}
public List<Person> findAllPersons(){
return persons;
}
public void deletePerson(String name ){
Iterator<Person> ps = persons.iterator();
if (ps.hasNext()) {
if (ps.next().getName().equals("name")) {
ps.remove();
}
}
}
}
activity_main.xml
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_weight="0"
android:layout_height="wrap_content"
android:text="mjl"
/>
<TextView
android:layout_weight="0"
android:layout_height="wrap_content"
android:id="@+id/phone"
/>
<!-- android:src="@drawable/ic_launcher"-->
<CheckBox
android:id="@+id/check"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>