Android04
学生管理系统界面的显示
- findAll()查到所有学生数据;
cursor.getString(cursor.getColumnIndex(columnName));
2.显示是一个LinearLayout
数据太多了,用滚动布局 (只能有一个子控件)
竖向滚动布局<ScrollView ></ScrollView>
横向滚动布局<HorizontalScrollView ></HorizontalScrollView>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/ll_result"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
3.显示数据逻辑
>
1. 创建文本TextView
2. 内容是查出的学生信息
3. 得到要显示的LinearLayout,添加进文本
4.清除原来数据
LinearLayout.removeAllView();
listview引入
是系统给我们提供的一个可以显示很多个item的控件
这个控件合理的控制了界面的显示,即使有1000000万个item要显示他也能扛的住
MVC
M:model 数据模型
V:view 界面展现
C:controller 控制器
listview的使用
适配器的操作使用步骤
- 在布局xml文件声明listview控件
- 在java代码找到listview控件,设置数据适配器setAapter();
- 可以设置ListView条目的点击事件,listview.setOnItemClickListener();
- 写一个类,继承BaseAdapter
重写四个方法
getCount()确定listview里面有多少个条目getView(int positon)返回某个位置要显示的view对象
positon是当前的位置,位置从0开始
converView表示的是复用的视图,用于优化ListView处理
学生管理系统的升级
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
/**
* 获取数据库的全部记录,刷新显示数据
*/
private void refreshData() {
final List<Student> students = dao.findAll();
// ll_result.removeAllViews();// 把原来的数据给清除
// for (Student student : students) {
// TextView tv = new TextView(this);
// tv.setText(student.toString());
// ll_result.addView(tv);
// }
lv.setAdapter(new BaseAdapter() {
@Override
public int getCount() {//获取一共有多少个条目
return students.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setGravity(Gravity.CENTER_VERTICAL);
ImageView iv = new ImageView(MainActivity.this);
String sex = students.get(position).getSex();
if("male".equals(sex)){
iv.setImageResource(R.drawable.nan);
}else {
iv.setImageResource(R.drawable.nv);
}
TextView tv = new TextView(MainActivity.this);
tv.setText( students.get(position).getName());
ll.addView(iv, 30, 30);
ll.addView(tv);
return ll;
}
listview的优化的原理
就是讲变成gc垃圾的Item,在放到最后使用
public View getView(int position, View convertView, ViewGroup parent)
listview的优化
view view = null;
if(convertView == null){
view = new TextView(MainActivity.this);
}else{
view = convertView;
}
采用打气筒创建view对象
View view = null;
if (convertView == null) {
// 把一个布局xml文件转化成view对象
view = View.inflate(MainActivity.this, R.layout.item, null);
} else {
view = convertView;
}
通知数据适配器刷新数据
/**
* 获取数据库的全部记录,刷新显示数据
*/
private void refreshData() {
students = dao.findAll();
if (adapter == null) {
adapter = new MyAdapter();
lv.setAdapter(adapter);
}else{
//通知数据适配器更新数据,而不是new出来新的数据适配器
adapter.notifyDataSetChanged();
}
}
常见对话框
确定取消对话框
public void click01(View view) {
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("警告:");
builder.setMessage("若练此功,必先自宫,是否继续?");
builder.setPositiveButton("确定自宫", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "啊....", 0).show();
}
});
builder.setNegativeButton("想想再说", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "如不自宫,一定不成功", 0).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
单选对话框
public void click02(View view) {
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("请选择您的性别:");
final String[] items = { "男", "女", "中性" };
builder.setSingleChoiceItems(items, -1, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您的性别:" + items[which], 0)
.show();
}
});
builder.setNegativeButton("取消选择", null);
builder.show();
}
多选对花框
public void click03(View view) {
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("请选择您爱吃的水果");
final String[] items = new String[] { "黄瓜", "苹果", "香蕉", "菠萝菠萝蜜" };
final boolean[] checkedItems = new boolean[] { true, true, false, false };
builder.setMultiChoiceItems(items, checkedItems,
new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(MainActivity.this,
items[which] + isChecked, 0).show();
checkedItems[which] = isChecked;
}
});
builder.setNegativeButton("取消选择", null);
builder.show();
}
进度对话框
public void click04(View view) {
final ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("提醒");
pd.setMessage("正在加载数据...请稍后");
pd.show();
new Thread() {
public void run() {
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
pd.dismiss();
};
}.start();
}
进度条对话框
public void click05(View view) {
final ProgressDialog pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMax(100);
pd.setTitle("提醒");
pd.setMessage("正在加载数据...请稍后");
pd.show();
new Thread() {
public void run() {
for (int i = 0; i <= 100; i++) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
pd.setProgress(i);
}
pd.dismiss();
};
}.start();
}
}
删除学生信息的对话框
view.findViewById(R.id.iv_delete).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("提醒");
builder.setMessage("是否删除这条学生信息?");
builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Student student = students.get(position);
String name = student.getName();
// 从数据库删除数据.
dao.delete(name);
Toast.makeText(MainActivity.this, "数据被删除了", 0)
.show();
// 更新ui界面.
refreshData();
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
});
return view;
}
快速拖动
android:fastScrollEnabled="true"
数据库的另外一种增删改查的方法
用API进行增删改查
1.增
ContentValues values = new ContentValues();
values.put(columnName, value);
db.insert(tableName,null,values);
2.删
db.delete(tableName, "name=?", new String[]{value});
3.改
ContentValues values = new ContentValues();
values.put(columnName, value);
db.update(tableName, values, "name=?", new String[] { value });
4.查
Cursor cursor = db.query(tableName, 需要查询的列, "name=?", new String[]{value},null,null, 排序);
数据库的事物
db.beginTransaction(); // 开启事务
try {
// 模拟转账的操作
db.execSQL("update account set money=money-100 where name='zhangsan'");
s.endsWith("haha");
db.execSQL("update account set money=money+100 where name='lisi'");
db.setTransactionSuccessful();// 设置事务执行成功
} finally {
db.endTransaction();
}
常见数据适配器-simple和arrayAdapter
simpleAdapter
<!--TextView不是LinearLayout-->
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:textSize="20sp"
android:textColor="#66ff0000"
android:layout_height="wrap_content" >
</TextView>
public class MainActivity extends Activity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
String[] objects = new String[]{"Animation","App","content","Media","NFC","OS"};
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item, objects));
}
}
arrayAdapter
public class MainActivity extends Activity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (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("icon", R.drawable.ic_menu_preferences);
map1.put("name", "功能设置");
data.add(map1);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("icon", R.drawable.ic_menu_recent_history);
map2.put("name", "时钟设置");
data.add(map2);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("icon", R.drawable.ic_menu_refresh);
map3.put("name", "同步设置");
data.add(map3);
Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("icon", R.drawable.ic_menu_report_image);
map4.put("name", "图片设置");
data.add(map4);
lv.setAdapter(new SimpleAdapter(this, data, R.layout.item, new String[]{"icon","name"}, new int[]{R.id.iv,R.id.tv}));
}
Android下的图形动画
log.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/logo1"
android:duration="850">
</item>
<item
android:drawable="@drawable/logo2"
android:duration="850">
</item>
<item
android:drawable="@drawable/logo3"
android:duration="850">
</item>
</animation-list>
activity_main.xml
<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" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv" />
</RelativeLayout>
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setBackgroundResource(R.drawable.logo);
AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
anim.start();//开始播放动画
}
应用程序的国际化
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">13_i18n</string>
<string name="action_settings">Settings</string>
<string name="hello_world">hello world!</string>
</resources>
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/flag" />
</RelativeLayout>
样式和主题
<resources>
<style name="text_content_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#0000ff</item>
<item name="android:textSize">20sp</item>
</style>
<style name="text_title_style" parent="@style/text_content_style">
<item name="android:textSize">25sp</item>
</style>
<style name="text_content_style.sub">
<item name="android:textSize">28sp</item>
</style>
</resources>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
style="@style/text_title_style"
android:text="@string/hello_world" />
<TextView
style="@style/text_content_style"
android:text="@string/hello_world" />
<TextView
style="@style/text_content_style.sub"
android:text="@string/hello_world" />
<TextView
style="@style/text_content_style"
android:text="@string/hello_world" />
<TextView
style="@style/text_content_style"
android:text="@string/hello_world" />
<TextView
style="@style/text_content_style"
android:text="@string/hello_world" />
</LinearLayout>