学习(二)ListView的几种实现方式
来源:互联网 发布:淘宝秋季休闲鞋女鞋 编辑:程序博客网 时间:2024/06/06 03:52
ListView是Android软件开发中非常重要组件之一,说道ListView就不得不说Adapter适配器,因为只有通过Adapter才可以把列表中的数据映射到ListView中。
在android的开发中最Adapter 一共可以分为
ArrayAdapter<T>,
BaseAdapter,
CursorAdapter,
HeaderViewListAdapter,
ResourceCursorAdapter,
SimpleAdapter,
SimpleCursorAdapter,
WrapperListAdapter
软件开发中最常用的有ArrayAdapter<T>, BaseAdapter, SimpleAdapter,接下下来讲介绍如何使用ListView控件。
下面是ListView的几种实现
public class StudyActivityextends BaseActivity implements OnClickListener{
private TextViewtitle;
private ImageViewmoreImageView;
private ListViewlistView;
private ArrayList<String>arrayList;
private ArrayList<Map<String, Object>>arrayListSub;
private MyListAdaptermyListAdapter = null;
@SuppressLint("DefaultLocale")
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_study);
title = (TextView) findViewById(R.id.title);
title.setText("学习");
moreImageView = (ImageView)findViewById(R.id.more);
moreImageView.setOnClickListener(this);
arrayList =new ArrayList<String>();
for(int i = 0; i < 100; i++ )
{
String tempStr = String.format("第%1$d章", i+1);
arrayList.add(i, tempStr);
}
arrayListSub =new ArrayList<Map<String,Object>>();
for(int j = 0; j < 100; j++ )
{
Map<String, Object> item = new HashMap<String, Object>();
String tempStr1 = String.format("第%1$d章", j+1);
String tempStr2 = String.format("子标题说明%1$d", j+1);
item.put("image", R.drawable.default_pic);
item.put("title", tempStr1);
item.put("text", tempStr2);
arrayListSub.add(item);
}
listView = (ListView)findViewById(R.id.listView);
//第一种:简单的ListView
// ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_expandable_list_item_1, arrayList);
//第二种:带标题的ListView
// SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayListSub,
// android.R.layout.simple_expandable_list_item_2,
// new String[]{"title","text"},
// new int[]{android.R.id.text1,android.R.id.text2});
//第三种:带图片的ListView
// SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayListSub,
// R.layout.mystudylistview,
// new String[]{"image","title","text"},
// new int[]{R.id.image_study,R.id.title_study,R.id.text_study});
//第四种:自定义布局BaseAdapter
myListAdapter =new MyListAdapter(arrayListSub,this);
listView.setAdapter(myListAdapter);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.more:
// showToast("点击了右侧按钮");
Intent intent_subject = new Intent(this, SubjectAndTextBookActivity.class);
startActivity(intent_subject);
break;
default:
break;
}
}
@Override
public void refresh(Object... param) {
// TODO Auto-generated method stub
super.refresh(param);
}
@Override
protectedvoid onResume() {
// TODO Auto-generated method stub
super.onResume();
SharedPreferences sp = getSharedPreferences("GRADE_SUB_PUB",MODE_PRIVATE);
//读取数据
String grade = sp.getString("GRADE_KEY",null);
String subject = sp.getString("SUBJECT_KEY", null);
String publicatation = sp.getString("PUBLICATION_KEY",null);
showToast(grade);
}
//自定义Adapter
class MyListAdapterextends BaseAdapter{
private LayoutInflaterinflater;
private ArrayList<Map<String, Object>>theList;
public MyListAdapter(ArrayList<Map<String, Object>> list, Context context)
{
super();
inflater = LayoutInflater.from(context);
theList = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return (theList ==null) ? 0 : theList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return theList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if (convertView ==null) {
convertView =inflater.inflate(R.layout.mystudylistview,null);
viewHolder =new ViewHolder();
viewHolder.titleView = (TextView)convertView.findViewById(R.id.title_study);
viewHolder.text = (TextView)convertView.findViewById(R.id.text_study);
viewHolder.image = (ImageView)convertView.findViewById(R.id.image_study);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder)convertView.getTag();
}
Map<String, Object> map = (Map<String, Object>)theList.get(position);
String a = map.get("title") ==null ? "0" : map.get("title").toString();
Log.i("heloo", a);
viewHolder.titleView.setText(a);
String b = map.get("text") ==null ? "0" : map.get("text").toString();
viewHolder.text.setText(b);
viewHolder.image.setImageResource(R.drawable.default_pic);
return convertView;
}
}
class ViewHolder
{
public TextViewtitleView;
public TextViewtext;
public ImageViewimage;
}
}
下面是ListView布局文件
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#666666"
android:orientation="vertical">
<include
layout="@layout/title"/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#666666"
android:scrollbars="none"
android:listSelector="@color/blue"
/>
</LinearLayout>
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_study"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dip"
android:scaleType="fitCenter"
android:src="@drawable/default_pic"/>
<TextView
android:id="@+id/title_study"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image_study"
android:gravity="center_vertical"
android:text="asdfadsf"
android:textColor="#ffffff"
android:textSize="20dip"/>
<TextView
android:id="@+id/text_study"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title_study"
android:layout_toRightOf="@+id/image_study"
android:gravity="center_vertical"
android:singleLine="true"
android:text="adsfkldjskf"
android:textSize="15dip"/>
</RelativeLayout>
最后贴一张运行截图
- 学习(二)ListView的几种实现方式
- 实现ListView的几种方式
- ListView的实现步骤及几种实现方式
- .NET 实现并行的几种方式(二)
- ListView的几种优化方式
- ListView的几种优化方式
- Android 文本滚动效果的几种实现方式(二)
- Unity中UGUI人物血条跟随的几种实现方式(二)
- Android 实现悬浮的几种方式(二)HeaderView显示隐藏
- Spring学习笔记二(Bean注入的几种方式)
- ios学习--单例实现的几种方式
- JDBC 数据库的几种查询方式(二)
- 二、创建线程的几种方式
- Undo/Redo几种实现方式介绍(二)——保存数据方式
- Android中ListView的几种常见优化方式
- Android基础--ListView的显示几种方式
- Action访问Servlet API的几种方式,其中方式三比较常用,下来是方式一,最后是方式二(实现接口的方式)
- 二值图像膨胀腐蚀算法的几种实现方式
- boost.system
- 更换网站IP最新操作技巧
- Android SDK Manager 无法更新SDK
- 学习Opencv 2.4.9 (一)---Opencv + vs2012环境配置
- svn同步的时候报错
- 学习(二)ListView的几种实现方式
- UINavigationController
- mozilla install
- echo/cat
- Linux环境变量的设置和查看方法
- uva 116 - Unidirectional TSP (动态规划)
- hdu1507--二分图最大匹配
- 用SHOPEX增强工具把淘宝数据包批量上传到自己的ShopEX独立网店
- hive不同分区不同存储格式