安卓sqlite数据库的使用


创建数据库逻辑代码:

<span style="font-size:18px;">package com.example.day14_work;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class MySqlite extends SQLiteOpenHelper{

	static String NAME="work.db";
	static int VERSION=1;
	//上下文对象,数据库名称,游标工厂,数据库的版本
	public MySqlite(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}
	public MySqlite(Context context)
	{
		super(context,NAME,null,VERSION);
		
	}
	//数据库第一次创建时调用
	@Override
	public void onCreate(SQLiteDatabase db) {
		 String sql="create table myInformation(color varchar(10),contents varchar(100))";
		 db.execSQL(sql);
	}

	//数据库版本发生改变时调用,即数据库版本升级
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	//数据库版本发生改变时调用,即数据库版本降级,只有发生重大错误时才会调用
	@SuppressLint("NewApi") 
	@Override
	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		super.onDowngrade(db, oldVersion, newVersion);
	}
	//每次打开数据库时调用,用于验证数据库打没打开
	@Override
	public void onOpen(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		super.onOpen(db);
		Log.i("===onOpen===", "数据库打开");
	}

}
</span>


第一个Activity的逻辑代码文件:

<span style="font-size:18px;">package com.example.day14_work;

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

import com.example.day14_work.adapter.MyAdapter;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	GridView gv;
	List<Map<String,Object>> list;
	MySqlite helper;
	MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gv=(GridView) findViewById(R.id.gv);
		helper=new MySqlite(getApplicationContext());
        getData();
        adapter=new MyAdapter(list,MainActivity.this);
        //SimpleAdapter adapter=new SimpleAdapter(getApplicationContext(), list, R.layout.style, new String[]{"content"},new int[]{R.id.tv});
        gv.setAdapter(adapter);
    }
    @Override
    protected void onRestart() {
    	super.onRestart();
        getData();
        gv.setAdapter(new MyAdapter(list,MainActivity.this));
    }
    public void click(View v)
    {
    	Intent intent=new Intent(MainActivity.this,Second.class);
    	startActivity(intent);
    }
    public void getData()
    {
    	list=new ArrayList<Map<String,Object>>();
    	SQLiteDatabase db=helper.getReadableDatabase();
		String sql="select * from myInformation";
		Cursor cursor = db.rawQuery(sql, null);
		while(cursor.moveToNext())
		{
			Map<String,Object> map=new HashMap<String, Object>();
			String color=cursor.getString(cursor.getColumnIndex("color"));
			String content=cursor.getString(cursor.getColumnIndex("contents"));
			map.put("color", color);
			map.put("content", content);
			list.add(map);
			//Toast.makeText(getApplicationContext(),color+"\n"+content, 0).show();
		}
		db.close();
    }
    
}
</span>

自定义适配器类逻辑代码:

<span style="font-size:18px;">package com.example.day14_work.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.day14_work.R;

public class MyAdapter extends BaseAdapter
{
	List<Map<String,Object>> list;
	Context context;
	
	public MyAdapter(List<Map<String, Object>> list, Context context) {
		super();
		this.list = list;
		this.context = context;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder = null;
		if(view==null)
		{
			holder=new ViewHolder();
			view=LayoutInflater.from(context).inflate(R.layout.style, null);
			holder.tv=(TextView) view.findViewById(R.id.tv);
			view.setTag(holder);
		}
		else
		{
			holder=(ViewHolder) view.getTag();
		}
		String data = list.get(position).get("content").toString();
		String mycolor = list.get(position).get("color").toString();
		holder.tv.setText(data);
		if("红色".equals(mycolor))
		{
			holder.tv.setTextColor(android.graphics.Color.RED);
		}
		else if("蓝色".equals(mycolor))
		{
			holder.tv.setTextColor(android.graphics.Color.BLUE);
		}
		return view;
	}
	class ViewHolder
	{
		TextView tv;
	}
}
</span>



第二个Activity的逻辑代码文件:

<span style="font-size:18px;">package com.example.day14_work;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class Second extends Activity{

	RadioGroup rg;
	EditText et;
	String content,selected;
	MySqlite helper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		rg=(RadioGroup) findViewById(R.id.rg);
		et=(EditText) findViewById(R.id.et);
		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				RadioButton rb=(RadioButton) findViewById(checkedId);
				selected=rb.getText().toString();
				if("红色".equals(selected))
				{
					et.setTextColor(android.graphics.Color.RED);
				}
				else if("蓝色".equals(selected))
				{
					et.setTextColor(android.graphics.Color.BLUE);
				}
				//Toast.makeText(getApplicationContext(), selected, 0).show();
			}
		});
		helper=new MySqlite(getApplicationContext());
	}
	public void click(View v)
	{
		content=et.getText().toString();
		SQLiteDatabase db=helper.getReadableDatabase();
		String sql="insert into myInformation values('"+selected+"','"+content+"')";
		db.execSQL(sql);
		Intent intent=new Intent();
		finish();
	}
}
</span>



第一个Activity布局文件:

<span style="font-size:18px;"><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" >
	<LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/ll"
        android:gravity="center">
        <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="主页"
        android:textSize="26sp"/>
    <Button 
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加"
        android:onClick="click"
        android:textSize="24sp"
        android:layout_marginLeft="30dp"/>
    </LinearLayout>
    <GridView
        android:id="@+id/gv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        android:layout_below="@+id/ll"
        android:layout_marginTop="20dp"/>
    

</RelativeLayout>
</span>

第二个Activity布局文件

<span style="font-size:18px;"><?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="vertical" >
    <LinearLayout 
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
   		android:orientation="horizontal"
   		android:gravity="center_horizontal" >
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加数据"
        android:textSize="26sp"/>
    <Button 
        android:id="@+id/finish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="完成"
        android:onClick="click"
        android:textSize="24sp"
        android:layout_marginLeft="20dp"/>
    </LinearLayout>
    
    <RadioGroup 
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_marginTop="20dp">
        <RadioButton 
        android:id="@+id/red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="红色"
        android:textSize="24sp"/>
        <RadioButton 
        android:id="@+id/blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="蓝色"
        android:textSize="24sp"/>
    </RadioGroup>
    <EditText 
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00ff00"
        android:hint="请输入内容"
        android:gravity="center"/>

</LinearLayout>
</span>

自定义适配器用的样式布局文件:

<span style="font-size:18px;"><?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="vertical" >
    <TextView 
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
</span>


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值