第一次实验 Android界面设计
一. 实验目的及实验环境 1. 实验目的
1)掌握SQLiteOpenHelper类结构
2)掌握基于SQLite数据库的应用开发过程 3)掌握Content Provider发布数据的方法 4)掌握Content Resolver获取数据的方法 2.实验环境
系统开发平 Android Studio 3.0 系统开发平台:Android 7.1 运行平台:Windows10 x64
运行环境:Microsoft.NET Framework SDK 2.0 二. 实验教材、组织方式、实验内容 1.实验教材: Andorid开发与应用 2.组织方式:个人独立完成 2.实验内容:
实现基于SQLite数据库的通信录应用,通过单击增加图标打开添加通信录
界面,通过单击通信录中的各条信息可删除选中项。 三.方案设计
Android系统中集成了SQLite数据库,并且为数据库的操作提供了相关的类和方法,便于没有数据库开发经验的开发者编写程序。另外,Android平台中利用Content Provider机制来实现跨应用程序数据共享。一个应用程序可以通过Content Provider来发布自己的数据,其他的应用程序可以通过Content Resolver来获取共享数据。
四.运行结果
五.总结
通过这次实验掌握了SQLite OpenHelper类结构,掌握了基于SQLite数据库的应用开发过程以及Content Provider发布数据的方法和掌握Content Resolver获取数据的方法。 六.附录:源代码
主布局文件activity_main.xml: android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:text=\"通信录\" /> android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" >
添加联系人布局文件addrelation.xml:
android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" /> android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" /> android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:entries=\"@array/test\" >
主Activity文件MainActivity.java:
import android.support.v7.app.ActionBarActivity; import android.app.AlertDialog; import android.app.Application;
import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View;
import android.widget.AdapterView; import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends ActionBarActivity { private ListView listView;
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView); // getRelationFromDB(); }
private void getRelationFromDB() {
final DatabaseHelper dbHelper = new DatabaseHelper(this); Cursor cursor = dbHelper.query();
String[] from = { \"_id\", \"name\", \"tel\", \"groupName\" }; int[] to = { R.id._id, R.id.name, R.id.tel, R.id.group };
SimpleCursorAdapter scadapter = new SimpleCursorAdapter(this, R.layout.relationlist, cursor, from, to); listView.setAdapter(scadapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView> adapter, View view, int position, long id) { final long temp = id;
AlertDialog.Builder adBuilder = new AlertDialog.Builder( MainActivity.this); adBuilder
.setMessage(\"确认要删除记录吗?\") .setPositiveButton(\"确认\",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub dbHelper.del((int) temp);
Cursor cursor = dbHelper.query();
String[] from = { \"_id\", \"name\", \"tel\", \"groupName\" };
int[] to = { R.id._id, R.id.name, R.id.tel, R.id.group };
SimpleCursorAdapter scadapter = new SimpleCursorAdapter (
getApplicationContext(),
R.layout.relationlist, cursor, from, to);
MainActivity.this.listView
.setAdapter(scadapter); } })
.setNegativeButton(\"取消\",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) { } });
AlertDialog aleraDialog = adBuilder.create(); aleraDialog.show(); } });
dbHelper.close(); }
public void add(View view) {
Intent intent = new Intent(MainActivity.this, AddrelationActivity.class);
startActivityForResult(intent, 0x111); }
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0x111 && resultCode == 0x111) { getRelationFromDB(); } }
@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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();
if (id == R.id.action_settings) { return true; }
return super.onOptionsItemSelected(item); } }
添加联系人AddrelationActivity.java import android.app.Activity; import android.app.AlertDialog;
import android.content.ContentValues; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View;
import android.widget.EditText; import android.widget.Spinner;
public class AddrelationActivity extends Activity { private EditText addName, addTel; private Spinner addGroup;
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.addrelation);
addName = (EditText) findViewById(R.id.addName); addTel = (EditText) findViewById(R.id.addTel); addGroup = (Spinner) findViewById(R.id.addGroup); }
public void save(View view) {
final ContentValues values = new ContentValues(); values.put(\"name\ values.put(\"tel\
values.put(\"groupName\ final DatabaseHelper dbHelper = new DatabaseHelper( getApplicationContext());
final AlertDialog.Builder adBuilder = new AlertDialog.Builder(this); adBuilder.setMessage(\"确认保存记录吗?\") .setPositiveButton(\"确认\DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dbHelper.insert(values); Intent intent = getIntent(); setResult(0x111, intent);
AddrelationActivity.this.finish(); } })
.setNegativeButton(\"取消\DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { } });
AlertDialog aleraDialog = adBuilder.create(); aleraDialog.show(); } }
因篇幅问题不能全部显示,请点此查看更多更全内容