简体   繁体   English

simplecursoraapter/listview 到 sqlite 数据库

[英]simplecursoradapter/listview to sqlite database

Hi I'm very new to database.嗨,我对数据库很陌生。 I've a code to retrieve SMS Inbox, Sent Box and Draft from phone and show it in a ListView using SimpleCursor Adapter.我有一个代码可以从手机中检索 SMS 收件箱、发送箱和草稿,并使用 SimpleCursor Adapter 在 ListView 中显示它。 I want to save and retrieve ListView items (one at a time) of "Sent Box" to SQLite database.我想将“Sent Box”的 ListView 项目(一次一个)保存和检索到 SQLite 数据库。 Currently I can insert data into database using edittext values.目前我可以使用 edittext 值将数据插入数据库。 So basically what I want is a method to select ListView item one at a time and save it in String[] and then insert String[] value to database.所以基本上我想要的是一种方法,一次选择一个 ListView 项目并将其保存在 String[] 中,然后将 String[] 值插入到数据库中。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks in advance.提前致谢。

 Code to display database items using "ListActivity" SQLiteDatabase db; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { db=openOrCreateDatabase("Student.db", SQLiteDatabase.CREATE_IF_NECESSARY, null); Cursor c=db.rawQuery("select id,name,age from Stud", null); ArrayList<String> list = new ArrayList<String>(); int count=c.getCount(); if(c.getCount()>0) { while(c.moveToNext()) { list.add(c.getString(0)+" , "+c.getString(1)+" , "+c.getString(2)); } c.close(); Toast.makeText(this,"Total Records: "+count, Toast.LENGTH_LONG).show(); ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list); getListView().setAdapter(adapter); } else { Toast.makeText(this, "No Record Found" , Toast.LENGTH_LONG).show(); } } catch(Exception e) { Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show(); } } public void onDestroy() { super.onDestroy(); db.close(); } }
 SQLite database public class MainActivity extends Activity { SQLiteDatabase db; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createDB(); //do insert Button btnInsert=(Button)findViewById(R.id.btnInsert ); btnInsert.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { insert(); } }); Button btnClear=(Button)findViewById(R.id.btnClear ); btnClear.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { clear(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); CreateMenu(menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { return MenuChoice(item); } private void CreateMenu(Menu menu) { MenuItem mnu1 = menu.add(0, 0, 0, "Insert"); { mnu1.setAlphabeticShortcut('i'); mnu1.setIcon(android.R.drawable.ic_input_add); } MenuItem mnu2 = menu.add(0, 1, 1, "Search"); { mnu2.setAlphabeticShortcut('s'); mnu2.setIcon(android.R.drawable.ic_search_category_default); } MenuItem mnu3 = menu.add(0, 2, 2, "Delete"); { mnu3.setAlphabeticShortcut('d'); mnu3.setIcon(android.R.drawable.ic_delete); } MenuItem mnu4 = menu.add(0, 3, 3, "View"); { mnu4.setAlphabeticShortcut('d'); mnu4.setIcon(android.R.drawable.ic_menu_info_details); } } private boolean MenuChoice(MenuItem item) { Intent intent=new Intent(); switch (item.getItemId()) { case 0: insert(); return true; case 1: intent.setClass(MainActivity.this, Search.class); startActivity(intent); return true; case 2: intent.setClass(MainActivity.this, Search.class); startActivity(intent); return true; case 3: intent.setClass(MainActivity.this, ViewRecord.class); startActivity(intent); return true; } return false; } public void createDB() { db=openOrCreateDatabase("Student.db", SQLiteDatabase.CREATE_IF_NECESSARY, null); db.setLocale(Locale.getDefault()); db.setLockingEnabled(true); db.setVersion(1); String sql="create table if not exists Stud(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)"; db.execSQL(sql); } public void insert() { EditText txtName=(EditText)findViewById(R.id.txtName); EditText txtAge=(EditText)findViewById(R.id.txtAge); if(txtName.getText().toString().equals("")) { Toast.makeText(MainActivity.this, "Enter Name.", Toast.LENGTH_SHORT).show(); } else if (txtAge.getText().toString().equals("")) { Toast.makeText(MainActivity.this, "Enter Age.", Toast.LENGTH_SHORT).show(); } else { String sql="insert into Stud(name,age) values('"+ txtName.getText().toString() +"',"+txtAge.getText().toString()+")"; db.execSQL(sql); clear(); Toast.makeText(MainActivity.this, "Record Successfully Inserted.", Toast.LENGTH_SHORT).show(); } } public void clear() { EditText txtName=(EditText)findViewById(R.id.txtName); EditText txtAge=(EditText)findViewById(R.id.txtAge); txtName.setText(""); txtAge.setText(""); txtName.clearFocus(); txtAge.clearFocus(); txtName.requestFocus(); } @Override public void onDestroy() { super.onDestroy(); db.close(); } }
 Code for displaying SMS Inbox, Sent Box and Draft public class MessageBox extends Activity implements OnClickListener { // GUI Widget Button btnSent, btnInbox, btnDraft; TextView lblMsg, lblNo; ListView lvMsg; // Cursor Adapter SimpleCursorAdapter adapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.messagebox); // Init GUI Widget btnInbox = (Button) findViewById(R.id.btnInbox); btnInbox.setOnClickListener(this); btnSent = (Button) findViewById(R.id.btnSentBox); btnSent.setOnClickListener(this); btnDraft = (Button) findViewById(R.id.btnDraft); btnDraft.setOnClickListener(this); lvMsg = (ListView) findViewById(R.id.lvMsg); } @Override public void onClick(View v) { if (v == btnInbox) { // Create Inbox box URI Uri inboxURI = Uri.parse("content://sms/inbox"); // List required columns String[] reqCols = new String[] { "_id", "address", "body" }; // Get Content Resolver object, which will deal with Content // Provider ContentResolver cr = getContentResolver(); // Fetch Inbox SMS Message from Built-in Content Provider Cursor c = cr.query(inboxURI, reqCols, null, null, null); // Attached Cursor with adapter and display in listview adapter = new SimpleCursorAdapter(this, R.layout.row, c, new String[] { "body", "address" }, new int[] { R.id.lblMsg, R.id.lblNumber }); lvMsg.setAdapter(adapter); } if (v == btnSent) { // Create Sent box URI Uri sentURI = Uri.parse("content://sms/sent"); // List required columns String[] reqCols = new String[] { "_id", "address", "body" }; // Get Content Resolver object, which will deal with Content // Provider ContentResolver cr = getContentResolver(); // Fetch Sent SMS Message from Built-in Content Provider Cursor c = cr.query(sentURI, reqCols, null, null, null); // Attached Cursor with adapter and display in listview adapter = new SimpleCursorAdapter(this, R.layout.row, c, new String[] { "body", "address" }, new int[] { R.id.lblMsg, R.id.lblNumber }); lvMsg.setAdapter(adapter); } if (v == btnDraft) { // Create Draft box URI Uri draftURI = Uri.parse("content://sms/draft"); // List required columns String[] reqCols = new String[] { "_id", "address", "body" }; // Get Content Resolver object, which will deal with Content // Provider ContentResolver cr = getContentResolver(); // Fetch Sent SMS Message from Built-in Content Provider Cursor c = cr.query(draftURI, reqCols, null, null, null); // Attached Cursor with adapter and display in listview adapter = new SimpleCursorAdapter(this, R.layout.row, c, new String[] { "body", "address" }, new int[] { R.id.lblMsg, R.id.lblNumber }); lvMsg.setAdapter(adapter); } } }

You are getting the SMS data from the following code您正在从以下代码获取短信数据

String[] reqCols = new String[] { "_id", "address", "body" };

        // Get Content Resolver object, which will deal with Content
        // Provider
        ContentResolver cr = getContentResolver();

        // Fetch Sent SMS Message from Built-in Content Provider
        Cursor c = cr.query(sentURI, reqCols, null, null, null);

Now all you need to do is get the data from cursor and insert it in your database either using raw query as you are doing or by using Content Provider.现在您需要做的就是从游标中获取数据并将其插入到您的数据库中,要么使用原始查询,要么使用内容提供程序。

For more information read Get inbox messages from android device to show in custom listview有关更多信息,请阅读从 android 设备获取收件箱消息以显示在自定义列表视图中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM