SQlite Android 数据库应用程序系统

前端之家收集整理的这篇文章主要介绍了SQlite Android 数据库应用程序系统前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

package com.example.ex_example_00;

import java.util.ArrayList;

import android.os.Bundle;

import android.preference.PreferenceManager.OnActivityResultListener;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.app.Dialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.database.Cursor;

import android.database.sqlite.sqliteDatabase;

import android.util.Log;

import android.view.ContextMenu;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ContextMenu.ContextMenuInfo;

import android.view.View.OnClickListener;

import android.view.View.OnCreateContextMenuListener;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.AdapterView.OnItemLongClickListener;

import android.widget.BaseAdapter;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

EditText et1;

EditText et2;

ListView listview;

sqliteDatabase mdb;

ArrayList<Student> stulist = new ArrayList<Student>();

int pos;

private MyAdaper myAdaper;

private Builder builder;

private AlertDialog dialog;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findViewById(R.id.button1).setOnClickListener(this);

et1 = (EditText) findViewById(R.id.editText1);

et2 = (EditText) findViewById(R.id.editText2);

listview = (ListView) findViewById(R.id.listView1);

MysqLite MysqLite = new MysqLite(this);

mdb = MysqLite.getReadableDatabase();

ChaXhao();

listview.setOnItemLongClickListener(new OnItemLongClickListener() {


@Override

public boolean onItemLongClick(AdapterView<?> parent,View view,

int position,long id) {

pos = position;

listview.showContextMenu();

return true;

}

});

listview.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {


@Override

public void onCreateContextMenu(ContextMenu menu,View v,

ContextMenuInfo menuInfo) {

menu.setHeaderTitle("选项");

menu.add(0,1,"修改");

menu.add(0,2,"删除");

menu.add(0,3,"取消");

}

});

}


@Override

public boolean onContextItemSelected(MenuItem item) {

switch (item.getItemId()) {

case 1:

Intent intent = new Intent(this,SencondActivity.class);

intent.putExtra("name",stulist.get(pos).name);

intent.putExtra("phone",stulist.get(pos).phone);

intent.putExtra("id",stulist.get(pos).id);

startActivityForResult(intent,0);

break;

case 2:

showDialog(1);

break;

case 3:


break;


default:

break;

}

return super.onContextItemSelected(item);

}


@Override

@Deprecated

protected Dialog onCreateDialog(int id) {

switch (id) {

case 1:

builder = new AlertDialog.Builder(this);

builder.setTitle("对话框");

builder.setMessage("是否删除");

builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,int which) {

String str = "DELETE FROM student WHERE id = ?";

mdb.execsql(str,new String[] { stulist.get(pos).id });

ChaXhao();

}

}

);


builder.setNegativeButton("取消",int which) {


Toast.makeText(MainActivity.this,"已经取消",Toast.LENGTH_LONG).show();

}

});

dialog = builder.create();

break;


default:

break;

}

return dialog;

}

@Override

protected void onActivityResult(int requestCode,int resultCode,Intent data) {

switch (resultCode) {

case 1:

String name = data.getStringExtra("isname");

String phone = data.getStringExtra("isphone");

String id = data.getStringExtra("isid");

String sql = "UPDATE student SET name = ?,phone = ? WHERE id = ? ";

mdb.execsql(sql,new String[] { name,phone,id });

ChaXhao();

break;

case 2:

break;


default:

break;

}

super.onActivityResult(requestCode,resultCode,data);

}


class MyAdaper extends BaseAdapter {


@Override

public int getCount() {

// TODO Auto-generated method stub

return stulist.size();

}


@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return null;

}


@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return 0;

}


@Override

public View getView(int position,View convertView,ViewGroup parent) {

Log.e("=================================","" + position);

Student student = stulist.get(position);

LayoutInflater layoutInflater = getLayoutInflater();

View inflate = layoutInflater.inflate(R.layout.itemp,null);

TextView textview = (TextView) inflate.findViewById(R.id.textView1);

textview.setText(student.name + "," + student.phone);

return inflate;

}


}


@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) {

switch (v.getId()) {

case R.id.button1:

btn_01();

break;

default:

break;

}

}


void ChaXhao() {

stulist.clear();

String str = "SELECT * FROM student";

Cursor cursor = mdb.rawQuery(str,null);

boolean toFirst = cursor.moveToFirst();

while (toFirst) {

int name_index = cursor.getColumnIndex("name");

int phone_index = cursor.getColumnIndex("phone");

int id_index = cursor.getColumnIndex("id");

String name = cursor.getString(name_index);

String phone = cursor.getString(phone_index);

String id = cursor.getString(id_index);

stulist.add(new Student(name,id));

toFirst = cursor.moveToNext();

}

myAdaper = new MyAdaper();

listview.setAdapter(myAdaper);

}


private void btn_01() {

String str = "insert into student(name,phone)values(?,?);";

String name = et1.getText().toString();

String phone = et2.getText().toString();

mdb.execsql(str,phone });

// stulist.add(new Student(name,phone));

// myAdaper.notifyDataSetChanged();

ChaXhao();

}


class Student {

String name;

String phone;

String id;


// public Student(String name,String phone) {

// super();

// this.name = name;

// this.phone = phone;

// }


public Student(String name,String phone,String id) {

this.name = name;

this.phone = phone;

this.id = id;

}


@Override

public String toString() {

return "Student [name=" + name + ",phone=" + phone + "]";

}


}

}

wKiom1Oo7VOSMCghAAFM_LU9Vk0534.jpg

wKioL1Oo7SaynRTsAAFwZhtTBbQ886.jpg

wKiom1Oo7VSzjwO3AAG4DADeGJ8903.jpg

wKioL1Oo7SaBN-mXAACq2BbjgB4749.jpg

原文链接:https://www.f2er.com/sqlite/200438.html

猜你在找的Sqlite相关文章