近期的一个实验用到东西挺多,特地总结一下。
要求功能:
1、第一个页面加入歌曲名和歌手,跳到第二个页面列表显示全部记录。使用sqlite数据库
2、对于第二个页面显示的信息能够进行删除和改动操作,并自己主动刷新
最后我做的效果:
长按列表项弹出单个管理的菜单,像微信好友对话管理一样。
删除时能够直接删除这一条并在列表中直接显示。更新时弹出对话框更新内容提交后在列表中又一次显示。
做此例遇到的困难:
1、菜单获取上下文
2、获取对话框中的内容
3、对话框button加入监听事件-----注意包不要导错:import android.content.DialogInterface.OnClickListener;
主要代码例如以下:
第一个页面:
package com.example.musiclist; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { SQLiteDatabase db; String music; String singer; Button add; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/music.db3", null); add = (Button)findViewById(R.id.add); add.setOnClickListener(new OnClickListener(){ Cursor cursor = null; public void onClick(View sourse){ music = ((EditText)findViewById(R.id.music)).getText().toString(); singer = ((EditText)findViewById(R.id.singer)).getText().toString(); try{ insertData(db,music,singer); }catch(SQLiteException se){ //primary key autoincrement db.execSQL("create table musiclist(_id integer primary key autoincrement," +"music varchar(30) ," +"singer varchar(30))"); insertData(db,music,singer); }finally{ //cursor.close(); Intent intentSec = new Intent(MainActivity.this,SecondActivity.class); startActivity(intentSec); } } }); } private void insertData(SQLiteDatabase db,String name,String weibo){ //运行插入语句 db.execSQL("insert into musiclist values(null,?,?)",new String[]{music,singer}); } public void onDestroy(){ super.onDestroy(); if(db!=null&&db.isOpen()){ db.close(); } } @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; } }
<LinearLayout 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:orientation="vertical" > <ListView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
第二个页面:
package com.example.musiclist; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.os.Bundle; import android.support.v4.widget.SimpleCursorAdapter; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnCreateContextMenuListener; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.CursorAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.TableLayout; public class SecondActivity extends Activity { SQLiteDatabase db; ListView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); show = (ListView)findViewById(R.id.show); db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/music.db3", null); //显示列表 showlist(); // 加入长按点击弹出选择菜单 show.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("选择操作"); menu.add(0, 0, 0, "更新该条"); menu.add(0, 1, 0, "删除该条"); } }); } //给菜单项加入事件 @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); //info.id得到listview中选择的条目绑定的id String id = String.valueOf(info.id); switch (item.getItemId()) { case 0: updateDialog(id); return true; case 1: //System.out.println("删除"+info.id); deleteData(db,id); showlist(); return true; default: return super.onContextItemSelected(item); } } //更新对话框 private void updateDialog(final String id){ final TableLayout updatemsg = (TableLayout)getLayoutInflater().inflate(R.layout.updatemsg, null); new AlertDialog.Builder(this) .setTitle("更新该条信息") .setView(updatemsg) .setPositiveButton("更新",new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String musicN = ((EditText)updatemsg.findViewById(R.id.musicN)).getText().toString(); String singerN = ((EditText)updatemsg.findViewById(R.id.singerN)).getText().toString(); updateData(db,id,musicN,singerN); showlist(); } }) .setNegativeButton("取消", new OnClickListener(){ public void onClick(DialogInterface dialog,int which){ } }) .create() .show(); } private void updateData(SQLiteDatabase db,String id,String musicN,String singerN){ db.execSQL("update musiclist set music='"+musicN+"',singer='"+singerN+"' where _id=?",new String[]{id}); } //删除列表项 private void deleteData(SQLiteDatabase db,String id){ db.execSQL("delete from musiclist where _id=?",new String[]{id}); } //显示列表项 private void showlist(){ Cursor cursor = null; try{ cursor = db.rawQuery("select * from musiclist", null); inflateList(cursor); }catch(SQLiteException se){ db.execSQL("create table musiclist(_id integer primary key autoincrement," +"music varchar(30) ," +"singer varchar(30))"); //查询 cursor = db.rawQuery("select * from musiclist", null); inflateList(cursor); }finally{ //cursor.close(); } } private void inflateList(Cursor cursor){ //填充SimpleCursorAdapter SimpleCursorAdapter adapter = new SimpleCursorAdapter(SecondActivity.this,R.layout.line,cursor, new String[]{"_id","music","singer"}, new int[]{R.id.idS,R.id.musicS,R.id.singerS}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); show.setAdapter(adapter); } public void onDestroy(){ super.onDestroy(); if(db!=null&&db.isOpen()){ db.close(); } } @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; } }
<LinearLayout 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:orientation="vertical" > <ListView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
列表 line.xml:
<?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="horizontal"> <TextView android:id="@+id/idS" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " /> <TextView android:id="@+id/musicS" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " /> <TextView android:id="@+id/singerS" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " /> </LinearLayout>
对话框:updatemsg.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TableRow> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:text="音乐名" android:textSize="30px" android:textColor="#ffffff" /> <EditText android:id="@+id/musicN" android:layout_width="100dip" android:layout_height="wrap_content" /> </TableRow> <TableRow> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:text="歌手" android:textSize="30px" android:textColor="#ffffff" /> <EditText android:id="@+id/singerN" android:layout_width="100dip" android:layout_height="wrap_content" /> </TableRow> </TableLayout>