栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

关于安卓的数据库交互问题以及跳转传值问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于安卓的数据库交互问题以及跳转传值问题

此篇文章只写java文件不写Android控件了

首先创建一个专门写数据库的文件,使其继承SQLiteOpenHelper类

这里书写建库建表语句

package com.example.day02;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class MyDbHelper extends SQLiteOpenHelper {

	public MyDbHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, "RoleDB.db", null, 2);
		// 创建数据库
	}
}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}
}

重写onCreate类(建表语句)

@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		// 建表
		db.execSQL("create table RoleDB(RoleID integer primary key autoincrement,RoleName varchar(64)"
				+ ",RoleLevel int,RoleArea varchar(64))");
		// 插入数据
		ContentValues cv1 = new ContentValues();
		cv1.put("RoleName", "农夫三拳有点疼");
		cv1.put("RoleLevel", "75");
		cv1.put("RoleArea", "华北1");
		db.insert("RoleDB", null, cv1);
		
		ContentValues cv2 = new ContentValues();
		cv2.put("RoleName", "落地骑猪去刚枪");
		cv2.put("RoleLevel", "75");
		cv2.put("RoleArea", "华中2区");
		db.insert("RoleDB", null, cv2);
		
		ContentValues cv3 = new ContentValues();
		cv3.put("RoleName", "不要干就是怂");
		cv3.put("RoleLevel", "75");
		cv3.put("RoleArea", "华南3区");
		db.insert("RoleDB", null, cv3);
		
		ContentValues cv4 = new ContentValues();
		cv4.put("RoleName", "sh");
		cv4.put("RoleLevel", "75");
		cv4.put("RoleArea", "华南3区");
		db.insert("RoleDB", null, cv4);

在这里完成数据库的创建

就进行第一个页面的输出

第一页面就是注意空间的绑定

运用Intent方法进行跳转

运用getText()方法获取内容

运用toString()方法进行转化

package com.example.day02;

import com.example.day02.MainActivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {
	EditText etUser, etPsd;
	Button btnYes, btnInsert, btnSkilly;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActionBar().hide();
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);
		init();
		btnYes.setOnClickListener(this);
	}

	void init() {
		etUser = (EditText) findViewById(R.id.et_user);
		etPsd = (EditText) findViewById(R.id.et_psd);
		btnYes = (Button) findViewById(R.id.btn_yes);
		btnInsert = (Button) findViewById(R.id.btn_insert);
		btnSkilly = (Button) findViewById(R.id.btn_skilly);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		String users = etUser.getText().toString();
		Intent it = new Intent(getApplicationContext(), InfoActivity.class);
		it.putExtra("etUser", users);
		startActivity(it);
	}

}

第二个页面较为复杂

得到传值以及绑定控件进行输出

package com.example.day02;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.day02.MyDbHelper;
import com.example.day02.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class InfoActivity extends Activity implements OnClickListener {
	TextView tvShow;
	Button btnSelect;
	ListView lvShow;
	EditText etName;
	List> listItem;
	MyDbHelper myDbhelper;
	SQLiteDatabase db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActionBar().hide();
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_info);
		init();

		Intent it = getIntent();
		String users = it.getStringExtra("etUser");
		tvShow.setText(users);

		// 从数据库中读取数据
		listItem = new ArrayList>();
		// 创建数据库
		myDbhelper = new MyDbHelper(getApplicationContext(), "RoleName.db",
				null, 2);
		db = myDbhelper.getWritableDatabase();
		
		Cursor cursor = db.query("RoleDB", null, null, null, null, null, null);
		// 从数据库查询数据
		while (cursor.moveToNext()) {
			String name = cursor.getString(1);
			String level = cursor.getString(2);
			String area = cursor.getString(3);
			Map map = new HashMap();
			map.put("RoleName", name);
			map.put("RoleLevel", level);
			map.put("RoleArea", area);
			listItem.add(map);
		}
		// 适配器创建
		SimpleAdapter adapte = new SimpleAdapter(getApplicationContext(),
				listItem, R.layout.item_list, new String[] { "RoleName",
						"RoleLevel", "RoleArea" }, new int[] {
						R.id.tv_top_text, R.id.tv_buttom_text,
						R.id.tv_buttom_texttwo });
		// 绑定
		lvShow.setAdapter(adapte);
	}

	void init() {
		btnSelect = (Button) findViewById(R.id.btn_select);
		tvShow = (TextView) findViewById(R.id.tv_show);
		lvShow = (ListView) findViewById(R.id.lv_show);
		etName = (EditText) findViewById(R.id.et_name);
		btnSelect.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		

}

数据库书写中主要注意适配器以及控件绑定的正确性,否则运行可能会直接闪退

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/930431.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号