Android的基础学习:ListView的使用和Adapter
来源:互联网 发布:机器人d-h算法 编辑:程序博客网 时间:2024/05/16 07:55
MVC模式和对应的ListView的应用。
M mode数据模型List<Person>
V view视图listView
C controller控制器Adapter
listView的代码;
package com.exampledatabasesqlite;import java.util.List;import com.example.databasesqlite.db.dao.PersonDao;import com.example.databasesqlite.domain.Person;import android.nfc.Tag;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.ListAdapter;import android.widget.ListView;import android.widget.TextView;import android.app.Activity;import android.content.res.ColorStateList;import android.database.DataSetObserver;import android.graphics.Color;public class MainActivity extends Activity {private ListView lv;private List<Person> persons;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);PersonDao dao=new PersonDao(this);persons=dao.findAll();lv=(ListView) findViewById(R.id.lv);lv.setAdapter(new MyAdapter());}//默认实现类simple,default,baseXXX的名字。private class MyAdapter extends BaseAdapter{private static final String Tag = "MyAdapter";/** * 控制listview里面总共有多少条目 */@Overridepublic int getCount() {// TODO Auto-generated method stubreturn persons.size();//条目个数==集合的size}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub//Log.i(Tag, "返回view对象,位置"+position);//TextView tvTextView=new TextView(getApplicationContext());//tvTextView.setTextSize(20);//tvTextView.setTextColor(Color.BLACK);//Person person=persons.get(position);//tvTextView.setText(person.toString());//return tvTextView;Person person=persons.get(position);View view=View.inflate(MainActivity.this, R.layout.list_item, null);//一定在view对象里面找idTextView tv_id=(TextView) view.findViewById(R.id.tv_id);tv_id.setText("id"+person.getId());TextView tv_name=(TextView) view.findViewById(R.id.tv_name);tv_name.setText("tv_name"+person.getName());TextView tv_number=(TextView) view.findViewById(R.id.tv_number);tv_number.setText("tv_number"+person.getNumber());return view;}}}ArrayAdapter的代码:
package com.example.arrayadapter;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends Activity {private static String[] nameStrings={"功能1","功能2","功能3","功能4","功能5"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView lvListView=(ListView) findViewById(R.id.lv);lvListView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.tv,nameStrings));}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ll_root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".MainActivity" ><ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </LinearLayout>
<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:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv"> </TextView> </LinearLayout>
ArrayAdapter的图片显示只能设置一样的。
simpleAdapter的代码:
package com.example.simpleadapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView lvListView=(ListView) findViewById(R.id.lv);List<Map<String, Object>> data=new ArrayList<Map<String,Object>>();Map<String, Object> map1=new HashMap<String, Object>();map1.put("nameText", "我是第一个功能");map1.put("iconid", R.drawable.btn_check_on_disabled_holo_light);Map<String, Object> map2=new HashMap<String, Object>();map2.put("nameText", "我是第二个功能");map2.put("iconid", R.drawable.btn_check_on_holo);Map<String, Object> map3=new HashMap<String, Object>();map3.put("nameText", "我是第三个功能");map3.put("iconid", R.drawable.btn_check_on_holo_dark);Map<String, Object> map4=new HashMap<String, Object>();map4.put("nameText", "我是第四个功能");map4.put("iconid", R.drawable.btn_check_on_holo_light);Map<String, Object> map5=new HashMap<String, Object>();map5.put("nameText", "我是第五个功能");map5.put("iconid", R.drawable.btn_check_on_pressed_holo_light);data.add(map1);data.add(map2);data.add(map3);data.add(map4);data.add(map5);//data 绑定的数据,list集合//R.layout.list_item 数据显示对应的布局//要让数据跟View对象建立一个对应关系//from[] new集合里面的数据的key//to[] 布局文件里面的idlvListView.setAdapter(new SimpleAdapter(this, data, R.layout.list_item, new String[]{"iconid","nameText"}, new int[]{R.id.iv,R.id.tv}));//绑定的对应的key和对象。}}
<RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/lv"/></RelativeLayout>
<?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="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView> </LinearLayout>
SimpleAdapter的图片显示可以不一样。
0 0
- Android的基础学习:ListView的使用和Adapter
- android listview和适配器adapter的使用
- Android中的ListView和Adapter的使用
- ListView 和 Adapter 的基础
- ListView 和 Adapter 的基础
- ListView 和 Adapter 的基础
- ListView 和 Adapter 的基础
- ListView和Adapter的基础
- ListView 和 Adapter 的基础
- ListView 和 Adapter 的基础
- ListView 和 Adapter 的基础
- ListView 和 Adapter 的基础
- ListView 和 Adapter 的基础
- ListView 和 Adapter 的基础
- ListView和Adapter的学习
- 【Android基础】ListView和不同Adapter的应用
- Android中使用Adapter和ListView的使用
- Android的Listview用法总结和Adapter的使用
- 三次握手
- Java_字符编码(Unicode、UTF-8、UTF-16)
- java设计模式 单例
- TCP和UDP的区别
- 5月13了。革命尚未成功,同志还需加油
- Android的基础学习:ListView的使用和Adapter
- POJ 3009 Curling 2.0
- 好吧 半年了
- PAT 1002. 写出这个数
- 字符,字节和编码
- c++函数的返回
- 设计模式之工厂模式
- 几个问题
- hdu3336解读KMP算法的next数组