LunchList中用到数据库,首先创建一个数据库类,要继承SQLiteOpenHelper,要重载onCreate和onUpgrade的方法:

package com.example.activity_and_service;import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;public class RestaurantHelper extends SQLiteOpenHelper {private static final String DATABASE_NAME = "lunchlist.db";private static final int SCHEMA_VERSION = 1;public RestaurantHelper(Context context){super(context, DATABASE_NAME, null, SCHEMA_VERSION);}public void onCreate(SQLiteDatabase db){db.execSQL("CREATE TABLE restaurants (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, address TEXT, type TEXT, notes TEXT);");}public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){}public Cursor getAll(){return getReadableDatabase().rawQuery("SELECT _id, name, address, type, notes FROM restaurants ORDER BY name", null);}public Cursor getById(String id){String[] args = {id};return getReadableDatabase().rawQuery("SELECT _id, name, address, type, notes FROM  restaurants WHERE _ID =?", args);}public void insert(String name, String address, String type, String notes){ContentValues cv = new ContentValues();cv.put("name", name);cv.put("address", address);cv.put("type", type);cv.put("notes", notes);getWritableDatabase().insert("restaurants", "name", cv);}public void update(String id, String name, String address, String type, String notes){ContentValues cv = new ContentValues();String[] args = {id};cv.put("name", name);cv.put("address", address);cv.put("type", type);cv.put("notes", notes);getWritableDatabase().update("restaurants", cv, "_ID=?", args);}public String getName(Cursor c){return c.getString(1);}public String getAddress(Cursor c){return c.getString(2);}public String getType(Cursor c){return c.getString(3);}public String getNotes(Cursor c){return c.getString(4);}
}

在 LunchList中用到CursorAdapter,自定义个继承CursorAdapter的内部类:

package com.example.activity_and_service;import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.util.Log;public class LunchList extends ListActivity {public final static String ID_EXTRA = "apt.tutorial._ID";Cursor model = null;RestaurantAdapter adapter = null;RestaurantHelper helper = null;public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);helper = new RestaurantHelper(this);//Log.d("onCreate", "" + helper);model = helper.getAll();startManagingCursor(model);adapter = new RestaurantAdapter(model);setListAdapter(adapter);}public void onDestroy(){super.onDestroy();Log.d("test", "" + helper);helper.close();}public void onListItemClick(ListView list, View view, int position, long id){Intent i = new Intent(LunchList.this, MainActivity.class);i.putExtra(ID_EXTRA, String.valueOf(id));startActivity(i);}public boolean onCreateOptionsMenu(Menu menu){new MenuInflater(this).inflate(R.menu.option, menu);return super.onCreateOptionsMenu(menu);}public boolean onOptionsItemSelected(MenuItem item){if (item.getItemId() == R.id.add) {startActivity(new Intent(LunchList.this, MainActivity.class));return true;}return super.onOptionsItemSelected(item);}class RestaurantAdapter extends CursorAdapter{RestaurantAdapter(Cursor c){super(LunchList.this, c);}public void bindView(View row, Context context, Cursor c){RestaurantHolder holder = (RestaurantHolder)row.getTag();holder.populateFrom(c, helper);}public View newView(Context context, Cursor c, ViewGroup parent){LayoutInflater inflater = getLayoutInflater();View row = inflater.inflate(R.layout.row, parent, false);RestaurantHolder holder = new RestaurantHolder(row);row.setTag(holder);return row;}}static class RestaurantHolder{private TextView name = null;private TextView address = null;private ImageView icon = null;RestaurantHolder(View row){name = (TextView)row.findViewById(R.id.title);address = (TextView)row.findViewById(R.id.address);icon = (ImageView)row.findViewById(R.id.icon);}void populateFrom(Cursor c, RestaurantHelper helper){//Log.d("populateFrom", "cursor:" + c);try {name.setText(helper.getName(c));address.setText(helper.getAddress(c));if ("sit_down".equals(helper.getType(c))) {icon.setImageResource(R.drawable.bg_video);} else if ("take_out".equals(helper.getType(c))) {icon.setImageResource(R.drawable.bg_video);}} catch(Exception e) {e.printStackTrace();}}}
}

当中用到两个xml文件:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@android:id/list"android:layout_width="fill_parent"android:layout_height="fill_parent"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="4dip"><ImageView android:id="@+id/icon"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_alignParentTop="true"android:layout_alignParentBottom="true"android:layout_marginRight="4dip"/><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical">  <TextView android:id="@+id/title"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center_vertical"android:textStyle="bold"android:singleLine="true"android:ellipsize="end"/><TextView android:id="@+id/address"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center_vertical"android:singleLine="true"android:ellipsize="end"/></LinearLayout>
</LinearLayout>

MainActivity类中主要是显示单项信息:

package com.example.activity_and_service;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.database.Cursor;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;public class MainActivity extends Activity  {EditText name = null;EditText address = null;EditText notes = null;RadioGroup types = null;String restaurantId = null;RestaurantHelper helper = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);helper = new RestaurantHelper(this);name = (EditText)findViewById(R.id.name);address = (EditText)findViewById(R.id.addr);notes = (EditText)findViewById(R.id.notes);types = (RadioGroup)findViewById(R.id.types);Button save = (Button)findViewById(R.id.save);save.setOnClickListener(onSave);restaurantId = getIntent().getStringExtra(LunchList.ID_EXTRA);if (restaurantId != null) load();}public void onDestroy(){super.onDestroy();helper.close();}public void load(){Cursor c = helper.getById(restaurantId);c.moveToFirst();name.setText(helper.getName(c));address.setText(helper.getAddress(c));notes.setText(helper.getNotes(c));if ("sit_down".equals(helper.getType(c))) {types.check(R.id.sit_down);} else if ("take_out".equals(helper.getType(c))) {types.check(R.id.take_out);} else {types.check(R.id.delivery);}}private View.OnClickListener onSave = new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString type = null;switch(types.getCheckedRadioButtonId()) {case R.id.sit_down:type = "sit_down";break;case R.id.take_out:type = "take_out";break;case R.id.delivery:type = "delivery";break;}if (restaurantId == null) {helper.insert(name.getText().toString(), address.getText().toString(), type, notes.getText().toString());} else {helper.update(restaurantId, name.getText().toString(), address.getText().toString(), type, notes.getText().toString());}finish();}
};
}

其对应的xml文件:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:stretchColumns="1"><TableRow><TextView android:text="Name:" /><EditText android:id="@+id/name" /></TableRow><TableRow><TextView android:text="Address:" /><EditText android:id="@+id/addr" /></TableRow><TableRow><TextView android:text="Type:" /><RadioGroup android:id="@+id/types"><RadioButton android:id="@+id/take_out"android:text="Take-Out"/><RadioButton android:id="@+id/sit_down"android:text="Sit-Down"/><RadioButton android:id="@+id/delivery"android:text="Delivery"/></RadioGroup></TableRow><TableRow><TextView android:text="Notes:" /><EditText android:id="@+id/notes"android:singleLine="false"android:gravity="top"android:lines="2"android:scrollHorizontally="false"android:maxLines="2"android:maxWidth="200sp"/></TableRow><Button android:id="@+id/save"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Save"/>
</TableLayout>

Activity的使用(四):两个activity的交互相关推荐

  1. android activity关联,如何实现两个Activity 之间如何通讯

    <转> 今天主要学习了Activity 组件,在这里作一下总结 1,学习如何创建Activity 创建 Activity 要点: (1) 一个Activity就是一个类,并且这个类要继承A ...

  2. 1.简述一个Activity跳转到另一个Activity时,两个Activity生命周期方法的执行过程。2.编写一个程序,要求在第一个界面中输入两个数字,在第二个界面显示第一个界面两个数字的和。

    1.简述一个Activity跳转到另一个Activity时,两个Activity生命周期方法的执行过程. 首先,我创建了一个MainActivity和SecondActivity两个Activity. ...

  3. 两个Activity左右滑动手势切换

    2019独角兽企业重金招聘Python工程师标准>>> 已经接触android四个月了,之前更多的是android的基础学习,在工作上也没有很多开发的工作,所以这几天有空学一下之前一 ...

  4. 上滑下滑动画切换两个activity

    两个activity互相切换,第一个activity向上滑出页面,第二个从底部滑入.返回时,第二个滑回底部,第一个从页面上部滑入屏幕 自定义了四个动画 in_from_down.xml <?xm ...

  5. Android Activity的launchMode四种启动模式备忘

    Android Activity的launchMode四种启动模式备忘 Android的Activity的启动模式有四种,在AndroidManifest.xml通过配置Activity的androi ...

  6. 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!...

    今天讲下在Surfaceview中如何实现两个或者多个Activity之间的切换与数据交互,为了更形象一些我做了一个游戏登录界面的demo,其中对于输入界面的布局这些我也是随意写了下,主要是实现功能并 ...

  7. Android Bundle类,通过bundle实现在两个activity之间的通讯

    根据google官方的文档(http://developer.android.com/reference/android/os/Bundle.html) Bundle类是一个key-value对,&q ...

  8. java向另一activity输入_Activity经典实例一:两个Activity传递数据和对象

    1.概述: Activity类直接或者间接地继承了Context.ContextWrapper.ContextThemeWrapper等基类,因此Activity可以直接调用它们的方法. 创建一个Ac ...

  9. 【转】 android之如何在两个activity之间传递handler_利用broadcast广播机制

    原文:http://blog.csdn.net/jason0539/article/details/18075293 这算是如何在两个activity之间传递handler的解决方案二了,解决方案一见 ...

最新文章

  1. Redis配置文件中的三个参数
  2. 基于VTKITK的Qt应用程序开发
  3. 【Caffe实践】 添加自己的网络层
  4. 【LeetCode从零单排】No100 Same Tree No101 Symmetric Tree
  5. Dev 等待提示 WaitDialogForm 升级版
  6. HandlerExceptionResolvers
  7. c语言程序设计 doc,《C语言程序设计》.doc
  8. 面向对象语言的技术特点
  9. 如何在data visualization 中update svg_如何操作小程序页面中data数据区中的数据
  10. 再问数据中台 - 数据中台是什么?
  11. 消息人士:欧盟下月将对英伟达收购Arm交易展开正式调查
  12. 基于id3算法根据房价数据进行画图预测python
  13. leetcode(105)从前序遍历和中序遍历构建二叉树
  14. plist中数据存取
  15. 透明贴图原理--浅显易懂
  16. 黑苹果(Hackintosh)简单步骤教程
  17. c语言有理数字符形式,C语言设计实现抽象数据类型有理数基本操作包括有理数的加法,减法,乘法,除法,以及求有理数的分子,分...
  18. 协同工作平台功能说明书
  19. Java自学笔记——Java面向对象——04.抽象类、接口、内部类
  20. vnpy 查询持仓量_持仓回报中的冻结量读取可能有误

热门文章

  1. python怎么读文件夹下的文件夹-python2.7读取文件夹下所有文件名称及内容的方法...
  2. python不能处理excel文件-python处理excel文件(xls和xlsx)
  3. python安装教程mac-Mac 安装python 3.*新版本的详细步骤
  4. python制作工资计算器-Python制作个税计算器
  5. python3.5安装-linux安装python3.5.1
  6. python是用c写的吗-Python是编写人工智能最佳的编程语言吗?
  7. python语言怎么学-怎么学Python语言呢?粤嵌Python学习路线入门
  8. python中的time库安装步骤-Python中time模块的使用
  9. python界面颜色-给Python点颜色——青少年学编程
  10. python九九乘法口诀表-Python 九九乘法表