-1

嗨,我很新的數據庫。我有一個代碼來檢索手機短信收件箱,發件箱和草稿,並使用SimpleCursor適配器在ListView中顯示它。我想保存並檢索「發送框」的ListView項目(一次一個)到SQLite數據庫。目前我可以使用edittext值將數據插入到數據庫中。所以基本上我想要的是一次選擇ListView項目的方法,並將其保存在String []中,然後將String []值插入到數據庫中。任何幫助,將不勝感激。提前致謝。simplecursoradapter/listview到sqlite數據庫

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 { 
 

 
\t // GUI Widget 
 
\t Button btnSent, btnInbox, btnDraft; 
 
\t TextView lblMsg, lblNo; 
 
\t ListView lvMsg; 
 

 
\t // Cursor Adapter 
 
\t SimpleCursorAdapter adapter; 
 

 
\t /** Called when the activity is first created. */ 
 
\t @Override 
 
\t public void onCreate(Bundle savedInstanceState) { 
 
\t \t super.onCreate(savedInstanceState); 
 
\t \t setContentView(R.layout.messagebox); 
 

 
\t \t // Init GUI Widget 
 
\t \t btnInbox = (Button) findViewById(R.id.btnInbox); 
 
\t \t btnInbox.setOnClickListener(this); 
 

 
\t \t btnSent = (Button) findViewById(R.id.btnSentBox); 
 
\t \t btnSent.setOnClickListener(this); 
 

 
\t \t btnDraft = (Button) findViewById(R.id.btnDraft); 
 
\t \t btnDraft.setOnClickListener(this); 
 

 
\t \t lvMsg = (ListView) findViewById(R.id.lvMsg); 
 

 
\t } 
 

 
\t @Override 
 
\t public void onClick(View v) { 
 

 
\t \t if (v == btnInbox) { 
 

 
\t \t \t // Create Inbox box URI 
 
\t \t \t Uri inboxURI = Uri.parse("content://sms/inbox"); 
 

 
\t \t \t // List required columns 
 
\t \t \t String[] reqCols = new String[] { "_id", "address", "body" }; 
 

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

 
\t \t \t // Fetch Inbox SMS Message from Built-in Content Provider 
 
\t \t \t Cursor c = cr.query(inboxURI, reqCols, null, null, null); 
 

 
\t \t \t // Attached Cursor with adapter and display in listview 
 
\t \t \t adapter = new SimpleCursorAdapter(this, R.layout.row, c, 
 
\t \t \t \t \t new String[] { "body", "address" }, new int[] { 
 
\t \t \t \t \t \t \t R.id.lblMsg, R.id.lblNumber }); 
 
\t \t \t lvMsg.setAdapter(adapter); 
 

 
\t \t } 
 

 
\t \t if (v == btnSent) { 
 

 
\t \t \t // Create Sent box URI 
 
\t \t \t Uri sentURI = Uri.parse("content://sms/sent"); 
 

 
\t \t \t // List required columns 
 
\t \t \t String[] reqCols = new String[] { "_id", "address", "body" }; 
 

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

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

 
\t \t \t // Attached Cursor with adapter and display in listview 
 
\t \t \t adapter = new SimpleCursorAdapter(this, R.layout.row, c, 
 
\t \t \t \t \t new String[] { "body", "address" }, new int[] { 
 
\t \t \t \t \t \t \t R.id.lblMsg, R.id.lblNumber }); 
 
\t \t \t lvMsg.setAdapter(adapter); 
 

 
\t \t } 
 

 
\t \t if (v == btnDraft) { 
 
\t \t \t // Create Draft box URI 
 
\t \t \t Uri draftURI = Uri.parse("content://sms/draft"); 
 

 
\t \t \t // List required columns 
 
\t \t \t String[] reqCols = new String[] { "_id", "address", "body" }; 
 

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

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

 
\t \t \t // Attached Cursor with adapter and display in listview 
 
\t \t \t adapter = new SimpleCursorAdapter(this, R.layout.row, c, 
 
\t \t \t \t \t new String[] { "body", "address" }, new int[] { 
 
\t \t \t \t \t \t \t R.id.lblMsg, R.id.lblNumber }); 
 
\t \t \t lvMsg.setAdapter(adapter); 
 

 
\t \t } 
 

 
\t } 
 
}

+0

沒有人有任何想法? –

回答

0

你從下面的代碼

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); 

獲得SMS數據現在,所有你需要做的是從光標獲取數據,並或者通過將其插入數據庫原始查詢,或者使用Content Provider。

欲瞭解更多信息,請閱讀Get inbox messages from android device to show in custom listview