Android之ListView与SimpleAdapter的使用
2013-10-30 02:05
471 查看
先说明一下,这里的数据使用sqlite里面存进去的,后面会写sqlite的具体使用
直接看效果图
首先在activity_main.xml定义一个ListView的控件
<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:id="@+id/listView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button2" android:layout_below="@+id/button2" > </ListView> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="@string/add" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="27dp" android:text="@string/queryAll" /> <Button android:id="@+id/update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_toRightOf="@+id/button2" android:text="@string/update" /> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/update" android:layout_alignBottom="@+id/update" android:layout_alignRight="@+id/listView1" android:text="@string/delete" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/button2" android:text="@string/interf" /> <Button android:id="@+id/add2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button3" android:layout_alignBottom="@+id/button3" android:layout_alignParentRight="true" android:text="@string/add2" /> </RelativeLayout>
然后为每一行记录定义一个布局my_listvie.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="4dip" android:paddingLeft="12dip" android:paddingRight="12dip" > <ImageView android:id="@+id/ItemImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:contentDescription="@string/hello" android:paddingTop="12dip" /> <TextView android:id="@+id/ItemTitle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/textview01" android:textSize="20sp" /> <TextView android:id="@+id/ItemText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/ItemTitle" android:text="@string/textview02" /> </RelativeLayout>
然后是程序运行的MainActivity类,里面有些接口调用的可以忽略不看,直接看display()方法就可以,
public class MainActivity extends Activity implements OnClickListener { private ListView lv; private Button btn1; private Button btn2; private Button interf; private Button add; private Button delete; private Button update; private DBManager mgr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mgr = new DBManager(this); btn1 = (Button) findViewById(R.id.button1); btn2 = (Button) findViewById(R.id.button2); interf = (Button) findViewById(R.id.button3); add = (Button) findViewById(R.id.add2); delete = (Button) findViewById(R.id.delete); update = (Button) findViewById(R.id.update); btn1.setOnClickListener(this); btn2.setOnClickListener(this); interf.setOnClickListener(this); add.setOnClickListener(this); delete.setOnClickListener(this); update.setOnClickListener(this); } public void add() { for (int i = 1; i <= 5; i++) { Person person = new Person("yang-" + i, 23, "Hello world-" + i); mgr.add(person); } } public void queryAll() { List<Person> persons = mgr.queryAll(); display(persons); } public void interfaceInvocation() { ContentResolver resolver = this.getContentResolver(); Uri uri = Uri.parse("content://com.example.z_sqlite.yang/query"); Cursor cursor = resolver.query(uri, null, null, null, null); List<Person> persons = new ArrayList<Person>(); if (cursor.moveToFirst()) { while (!cursor.isAfterLast()) { Person person = new Person(); person.setId(cursor.getInt(cursor.getColumnIndex("id"))); person.setName(cursor.getString(cursor.getColumnIndex("name"))); person.setAge(cursor.getInt(cursor.getColumnIndex("age"))); person.setInfo(cursor.getString(cursor.getColumnIndex("info"))); persons.add(person); cursor.moveToNext(); } cursor.close(); } display(persons); } public void display(List<Person> persons) { lv = (ListView) findViewById(R.id.listView1); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for (Person person : persons) { Map<String, Object> map = new HashMap<String, Object>(); map.put("ItemImage", R.drawable.favicon); map.put("name", person.getName()); map.put("ageinfo", "id:" + person.getId() + ",今年 " + person.getAge() + " 岁,个人信息:" + person.getInfo()); list.add(map); } // 适配器 SimpleAdapter sa = new SimpleAdapter(this, list, R.layout.my_listvie, new String[] { "ItemImage", "name", "ageinfo" }, new int[] { R.id.ItemImage, R.id.ItemTitle, R.id.ItemText }); // 显示 lv.setAdapter(sa); // 为每个Item添加监听 lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "点击了第" + position + "个Item:", Toast.LENGTH_SHORT).show(); } }); // 添加长按点击 lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("长按菜单-ContextMenu"); menu.add(0, 1, 1, "修改"); menu.add(0, 2, 2, "删除"); menu.add(1, 3, 3, "更多"); } }); } // 长按菜单响应函数 @Override public boolean onContextItemSelected(MenuItem item) { setTitle("点击了长按菜单里面的第" + item.getItemId() + "个项目"); return super.onContextItemSelected(item); } public void add2() { ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.example.z_sqlite.yang/insert"); ContentValues cv = new ContentValues(); cv.put("name", "willwind"); cv.put("age", 120); cv.put("info", "到此一游,好困阿!"); Uri temp = resolver.insert(uri, cv); Log.i("接口调用", "delete ---" + temp); } public void delete() { ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.example.z_sqlite.yang/delete"); int flag = resolver.delete(uri, "id=?", new String[] { "10" }); Log.i("接口调用", "delete ---" + flag); } public void update() { ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.example.z_sqlite.yang/update"); ContentValues cv = new ContentValues(); cv.put("name", "yangxuan"); cv.put("age", 120); int flag = resolver.update(uri, cv, "id=?", new String[] { "11" }); Log.i("接口调用", "update ---" + flag); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { // TODO Auto-generated method stub if (v == btn1) { add(); Toast.makeText(MainActivity.this, "数据添加成功!", Toast.LENGTH_SHORT) .show(); Uri uri = Uri.parse("content://aaa.bbb.ccc"); v.getContext().getContentResolver().notifyChange(uri, null); } else if (v == btn2) { queryAll(); Toast.makeText(MainActivity.this, "查询成功!", Toast.LENGTH_SHORT) .show(); } else if (v == interf) { interfaceInvocation(); Toast.makeText(MainActivity.this, "接口调用查询成功!", Toast.LENGTH_SHORT) .show(); } else if (v == add) { add2(); Toast.makeText(MainActivity.this, "接口调用添加成功!", Toast.LENGTH_SHORT) .show(); } else if (v == delete) { delete(); Toast.makeText(MainActivity.this, "接口调用删除成功!", Toast.LENGTH_SHORT) .show(); } else if (v == update) { update(); Toast.makeText(MainActivity.this, "接口调用修改成功!", Toast.LENGTH_SHORT) .show(); } } @Override protected void onDestroy() { // TODO Auto-generated method stub mgr.closeDB(); super.onDestroy(); } }
相关文章推荐
- android ListView之——SimpleAdapter使用
- Android杂谈--ListView之SimpleAdapter的使用
- Android杂谈--ListView之SimpleAdapter的使用
- Android杂谈--ListView之SimpleAdapter的使用
- Android中ListView之SimpleAdapter的使用
- Android 之 ListView使用SimpleAdapter展示列表
- Android学习之ListView与SimpleAdapter的使用
- Android 之 ListView使用SimpleAdapter展示列表
- Android 中 ListAactivity 和Activity 使用ListView 以及ArrayAdapter和SimpleAdapter参数详解
- Android杂谈--ListView之SimpleAdapter的使用
- android 使用SimpleCursorAdapter将SQLite数据显示到ListView
- Android杂谈--ListView之SimpleAdapter的使用
- Android之ListView使用方法-ArrayAdapter-SimpleAdapter-BaseAdapter
- Android SimpleAdapter应用介绍,代码+解释 ListView使用+滚动监听用scoll
- Android学习---ListView的点击事件,simpleAdapter和arrayadapter,SimpleCursoAdapter的原理和使用
- SimpleAdapter,Android ListView使用BaseAdapter与ListView的优化,SQL命令
- Android中ListView同过自定义布局并使用SimpleAdapter的方式实现数据的绑定
- [Android] ListView 结合SimpleAdapter使用
- Android之ListView使用方法-SimpleAdapter-SimpleCursorAdapter
- Android中动态生成ListView及SimpleAdapter的使用