1.简单的单行ListView。
代码很如下:
public class ListViewOne extends ListActivity{
String [] str = new String [] {"C","Java","C++","PHP","Python","Perl","Rudy","JavaScript"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,android.R.id.text1,str);
this.setListAdapter(adapter);
}
}
2.复杂的双行ListView
代码如下:
public class ListViewTwo extends ListActivity{
List
3.下面是自定义布局的ListView,并加上了改变了标题栏的样式
代码是这样的:
1.布局文件:
2.ListView的布局:
3.Activity的代码:
public class MainActivity extends Activity {
List> listdata;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.mylistview);
listView.setAdapter(new ImageAdapter(this));
View view = this.findViewById(android.R.id.title);
((TextView)view).setTextColor(Color.YELLOW); //设置标题栏的文字颜色
}
public List> getData(){
List> list = new ArrayList>();
Map map1 = new HashMap();
map1.put("name", "张三同学");
map1.put("address", "学生1");
Map map2 = new HashMap();
map2.put("name", "李四同学");
map2.put("address", "学生2");
Map map3 = new HashMap();
map3.put("name", "王五同学");
map3.put("address", "学生3");
list.add(map1);
list.add(map2);
list.add(map3);
return list;
}
private class ImageAdapter extends BaseAdapter{
Context context;
String [] str = new String[]{"第一张","第二张","第三张","第四张","第五张"};
String [] time = new String[]{"一分钟前","二分钟前","三分钟前","四分钟前","五分钟前"};
String [] content = new String[]{"心情愉快","心情郁闷","嘻嘻","哈哈","嘿嘿,你好啊,好看不啊"};
int [] imageid = new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5};
public ImageAdapter(Context context) {
this.context = context;
}
//返回图片的个数
@Override
public int getCount() {
return str.length;
}
@Override
public Object getItem(int position) {
return str[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
//View的findViewById()方法也是比较耗时的,因此需要考虑中调用一次,之后用
//View的getTag()来获取这个ViewHolder对象
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.mylistview, null);
holder.imageButton = (ImageButton) convertView.findViewById(R.id.myimagebutton);
holder.username = (TextView) convertView.findViewById(R.id.username);
holder.time = (TextView) convertView.findViewById(R.id.time);
holder.content = (TextView) convertView.findViewById(R.id.content);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageButton.setImageResource(imageid[position]);
holder.username.setText(str[position]);
holder.time.setText(time[position]);
holder.content.setText(content[position]);
return convertView;
}
}
//增加这样一个静态类,缓存一下,这样不用每次都重新加载
final class ViewHolder{
public ImageButton imageButton;
public TextView username;
public TextView time;
public TextView content;
}
}
注意改变标题栏的样式的时候,要在你项目的styles.xml,加上:
并在AndroidManifest.xml文件里面加上你改变的标题栏的样式:
android:label="@string/title_activity_main"
ok,就这些了。
android:label="@string/title_activity_main"