class MainActivity
extends Activity {
ListView list,lv;
MyOpenHelper dbHelper;
SQLiteDatabase db;
String s;
Button delbtn,addbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=
(ListView)findViewById(R.id.listView1);
addbtn=
(Button)findViewById(R.id.button1);
delbtn=
(Button)findViewById(R.id.button2);
dbHelper=
new MyOpenHelper(MainActivity.
this,"personal_contacts.db",
null,1
);
db=
dbHelper.getReadableDatabase();
///查询数据库
Cursor cursor = db.rawQuery("select * from contacts",
null);
Log.i("sssssssss", "111111"
);
inflateList(cursor);
//长按显示详细信息
lv.setOnItemLongClickListener(
new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view,
int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView textView =
(TextView)findViewById(R.id.textView1);
s=
textView.getText().toString();
Intent intent =
new Intent(MainActivity.
this,DetailActivity.
class);
Bundle bundle=
new Bundle();
bundle.putString("name"
, s);
intent.putExtras(bundle);
startActivity(intent);
finish();
return false;
}
});
delbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
db.delete("contacts", "name=?",
new String[]{s});
Toast.makeText(MainActivity.this, "删除成功"
, Toast.LENGTH_LONG).show();
Cursor cursor=db.rawQuery("select * from contacts",
null);
inflateList(cursor);
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int arg2,
long arg3) {
// TODO Auto-generated method stub
view.setBackgroundColor(Color.BLUE);
TextView textView =
(TextView)findViewById(R.id.textView1);
s=
textView.getText().toString();
}
});
addbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i=
new Intent();
i.setClass(MainActivity.this, DetailActivity.
class);
startActivity(i);
finish();
}
});
}
private void inflateList(Cursor cursor){
Log.i("aaaaa", "111111"
);
int count=
cursor.getCount();
String[] name=
new String[count];
String[] mobile=
new String[count];
int a=0
;
while(cursor.moveToNext()){
name[a]=cursor.getString(cursor.getColumnIndex("name"
));
mobile[a]=cursor.getString(cursor.getColumnIndex("mobile"
));
a++
;
}
ArrayList<HashMap<String, Object>> listItem =
new ArrayList<HashMap<String,Object>>
();
Log.i("sssssssss", "2222222"
);
for(
int i=0;i<count;i++
){
Log.i("qqqqqqqqq", "111111"
);
HashMap<String,Object> map =
new HashMap<String, Object>
();
map.put("ItemText1"
, name[i]);
map.put("ItemText2"
, mobile[i]);
listItem.add(map);
}
SimpleAdapter listItemAdater =
new SimpleAdapter(MainActivity.
this,listItem,R.layout.item,
new String[]{"ItemText1","ItemText2"},
new int[]{R.id.textView1,R.id.textView2});
Log.i("jjjjjjjjjjjjjjjj", "111111"
);
lv.setAdapter(listItemAdater);
Log.i("zzzzzz", "111111"
);
}
@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;
}
}
运行结果:(截图)
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见)
|
这次实验感觉挺难的,虽然一开始就有了大致的逻辑思路;在过程中一运行程序就崩溃,后来检查了很久才发现是因为没实现findViewById这个方法,没有办法编译到相关的代码,解决了这个问题之后,在新建联系人保存的时候程序崩溃了,经检查发现有两个数据库,这是因为MyOpenHelper.java的数据库名称跟MainActivity.java的数据库名称不一致造成的。所以,在实验的时候要仔细。
|
实验评语
|
|
实验成绩
|
|
指导教师签名: 年 月 日
|
实验8 SQLite数据库操作
标签: