第一种方法:
在xml文件中使用ScrollView将控件包裹起来:
如:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_root"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
在Java文件中
package com.sung.ListviewTest;
import java.util.ArrayList;
import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class LitviewTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//加载ll_root布局,并且实例化
LinearLayout ll_root = (LinearLayout)findViewById(R.id.ll_root);
//设置背景
ll_root.setBackgroundColor(Color.WHITE);
ArrayList<String> al = new ArrayList<String>();
String str = "哈哈";
//定义一random的产生随机数的方法
Random random = new Random();
for(int i=0; i<150; i++){
String addstr =str+random.nextInt(5000)+"\t"+i;
al.add(addstr);
}
for(String s:al){
TextView tv = new TextView(this);
tv.setTextSize(20);
tv.setTextColor(Color.BLACK);
tv.setText(s);
//将TextView添加到布局中
ll_root.addView(tv);
}
}
}
方法二:使用最简单的适配器Arrayadpter(ps:这种适配器只适合显示一种图片的或者不带图片的)
首先要创建两个XMl
第一个xml:
<?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="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv" />
</LinearLayout>
第二个xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Java文件:
package com.sung.ArrayAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ArrayAdapterActivity extends Activity {
private static String[] names ={"功能1","功能2","功能3","功能4","功能5","功能6","功能7","功能8"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView)findViewById(R.id.lv);
//上下文,布局文件ID,资源ID,每个条目所显示的信息四个参数
//三个参数的ArrayAdapter适配器布局文件非常简单,只有TextView
//这种适配器图标一致,只能更改文本
lv.setAdapter(new ArrayAdapter<String>(ArrayAdapterActivity.this, R.layout.list_item, R.id.tv, names));
}
}
方法三:使用Simpadpter适配器
第一个xml文件
<?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="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/LV"/>
</LinearLayout>
第二个xml文件
<?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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
</LinearLayout>
Java文件
package com.sung.SimpleAapterText;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class SimpleAapterTextActivity extends Activity {
private Map<String,Object> map;
private String str[] = {"14届-组织部","好友照片","四零五","我的Windows Phone","第二届通号学院学生会"
,"宣组办","第二期入党积极分子","软件131"};;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv =(ListView)findViewById(R.id.lv);
//创建一个list集合,将所要显示的数据放进集合中
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
int imag [] = new int[]{R.drawable.zzb,R.drawable.hyzhb,R.drawable.slw
,R.drawable.myphone,R.drawable.xsh,R.drawable.zxb,R.drawable.rd,R.drawable.rj};
for(int i =0; i<str.length; i++){
map = new HashMap<String, Object>();
map.put("nameText", str[i]);
map.put("imag", imag[i]);
data.add(map);
//添加适配器 ,上下文,list集合,布局文件,Map集合对应的key,前面对应的资源文件
}
lv.setAdapter(new SimpleAdapter(this, data, R.layout.list_item,
new String[]{"imag","nameText"},new int[]{R.id.iv,R.id.tv}));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//新建一个警告对话框
new AlertDialog.Builder(SimpleAapterTextActivity.this).setMessage("您选中的是:"+str[arg2]).show();
}
});
}
}
第四种方法,也是实现adpter的原理
<?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="wrap_content"
android:id="@+id/ll_root"
android:orientation="vertical" >
<ListView
android:id="@+id/LV"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
第二个xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dip"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:id="@+id/count"
android:textSize="18sp"
android:text="count"
android:textColor="#ff0000"
/>
<TextView
android:id="@+id/random"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dip"
android:text="random"
android:textColor="#88000000"
android:gravity="center"/>
</LinearLayout>
Java文件
package com.sung.ListviewTest;
import java.util.ArrayList;
import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class LitviewTestActivity extends Activity {
private ListView lv;
private ArrayList<String> al;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll_root = (LinearLayout)findViewById(R.id.ll_root);
ll_root.setBackgroundColor(Color.WHITE);
al = new ArrayList<String>();
String str = "哈哈";
for(int i=0; i<150; i++){
String addstr =i+"\t\t"+str;
al.add(addstr);
}
lv = (ListView)findViewById(R.id.LV);
lv.setAdapter(new Myadapter());
}
//当实现一个接口发现里面实现的方法比较多时就得想到继承默认实现类simplexxx,defaultxxx,basexxx数据适配器
private class Myadapter extends BaseAdapter{
/**
*控制listview里面总共有多少个条目 */
@Override
public int getCount() {
// TODO Auto-generated method stub
return al.size();//条目个数 = 集合的size
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
/*
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.v("1", "位置"+position);
/*TextView tv = new TextView(getApplicationContext());
tv.setTextColor(Color.BLACK);
//得到某个位置的对象
//al.get(position);
tv.setText(al.get(position).toString());
return tv;
*/
//填充气 上下文,给定的资源文件,放到某个容器--副窗体根节点
View view = View.inflate(LitviewTestActivity.this,R.layout.list_item , null);
//修改view对象的数据
//在view对象中找到控件,默认是在当前activity中
TextView count= (TextView)view.findViewById(R.id.count);
count.setText(" "+al.get(position));
TextView rand = (TextView)view.findViewById(R.id.random);
Random random = new Random();
rand.setText(" "+random.nextInt(5000));
return view;
}
}
}