以下为使用BaseAdapter作扩展,自定义Adapter来使用ListView控件:
需要注意以下的几点:
1、自定义Adapter时,需要特别注意Adapter类中getView()方法覆盖,注意加载布局文件和加载控件的区别;
2、自定义好Adapter后,在Activity中实现ListView的过程:
(1)生成Adapter对象(注意传入一个list表),并将Adapter对象作为ListView的适配器
ListViewAdapter adapter = new ListViewAdapter(getApplicationContext(),list); listView.setAdapter(adapter);
(2)当list发生改动的时候,需要使用adapter.notifyDataSetChanged();
3、在Adapter的getView()时,使用LayoutInflater.Inflate()方法加载布局,findViewByID()加载控件;
项目包含的文件如下:
结果如下:
1、输入数字时候为第一种布局
2、输入文本的时候为第二种布局
完整代码如下
Adapter类:
package com.example.testlistview;
import java.util.List;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
private Context context;
private LayoutInflater listContainer;
private List<String> listItems;
private TextView textView1;
private TextView textView2;
private Button button1;
private Button button2;
public ListViewAdapter(Context context,List<String> list) {
// TODO Auto-generated constructor stub
this.context = context;
this.listItems = list;
this.listContainer = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listItems.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String str = listItems.get(position).toString();
if(isNum(str)==false){
convertView = listContainer.inflate(R.layout.list_item_layout2, null);
textView1 = (TextView)convertView.findViewById(R.id.textView1);
textView2 = (TextView)convertView.findViewById(R.id.textView2);
button1 = (Button)convertView.findViewById(R.id.button1);
button2 = (Button)convertView.findViewById(R.id.button2);
textView1.setText(listItems.get(position));
textView1.setTextColor(Color.BLACK);
textView2.setText(listItems.get(position));
textView2.setTextColor(Color.BLACK);
button1.setText(listItems.get(position));
button2.setText(listItems.get(position));
return convertView;
}else {
convertView = listContainer.inflate(R.layout.list_item_layout1, null);
textView1 = (TextView)convertView.findViewById(R.id.textView1);
textView2 = (TextView)convertView.findViewById(R.id.textView2);
textView1.setText(listItems.get(position));
textView1.setTextColor(Color.BLACK);
textView2.setText(listItems.get(position));
textView2.setTextColor(Color.BLACK);
return convertView;
}
}
public static boolean isNum(String str){
return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
}
}
//Activity类
package com.example.testlistview;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class ListViewActivity extends ActionBarActivity {
private ListView listView;
private Button btn;
private EditText edt;
private ListViewAdapter adapter;
private ArrayList<String> list = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
listView = (ListView)findViewById(R.id.listView1);
btn = (Button)findViewById(R.id.button1);
edt = (EditText)findViewById(R.id.editText1);
adapter = new ListViewAdapter(getApplicationContext(),list);
listView.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list.add(edt.getText().toString());
adapter.notifyDataSetChanged();
}
});
}
}
主布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="312dp" >
</ListView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="提交" />
</LinearLayout>
</LinearLayout>
layout1布局:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:text="TextView" />
</LinearLayout>
layout2布局:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:gravity="center"
android:text="detail" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="layout1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="layout2" />
</LinearLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:gravity="center"
android:text="state" />
</LinearLayout>