Salve ragazzi, sto cercando di implementare un database SQLite al cui interno carico tutti i contatti della rubrica, caso vuole che io non ci riesca perciò chiedo aiuto a voi,grazie mille a tutti coloro che risponderanno.
codice DB:
public class DBcont {
private static final String DB_NAME="DBJACK";//nome del db
private static final int DB_VERSION=1;
protected static final String nome="";
protected static final String _id="";
protected static final String numero="";
final String sql="CREATE TABLE CONTATTI ("+"_id PRIMARY KEY AUTOINCREMENT UNIQUE," + "nome text NOT NULL"+"numero text NOT NULL);";
String g;
SQLiteDatabase db;
DBHelper mDbHelper;
Context mContext;
public DBcont(Context ctx){
mContext=ctx;
mDbHelper=new DBHelper(ctx,DB_NAME,null,DB_VERSION);
}
public void open(){
db=mDbHelper.getWritableDatabase();
}
public void close(){
db.close();
}
public void inserisci(String nome1, String numero1,String id){
ContentValues value = null;
value.put(_id, id);
value.put(nome, nome1);
value.put(numero, numero1);
db.insert(sql, null, value);
}
public String leggi(String search){
Ricerca r=new Ricerca();
Cursor cur= db.query( sql, null, nome, null, null, search, "ASC", null);
int i=0;
while(cur.moveToNext()){
i++;
String wanted=cur.getString(cur.getColumnIndex(nome));
if(r.search(search,wanted)==true){
g=r.getMach();
return g;
}
}
return "alcuna corrispondenza trovata";
}
private class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
Codice App:
public class MainActivity extends Activity {
String _id=ContactsContract.CommonDataKinds.Phone._ID;
String Nome=ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
String Numero=ContactsContract.CommonDataKinds.Phone.NUMBER;
Uri uri=ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String sortOrder=ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+"ASC";
String colonna[]={_id,Nome,Numero};
Button bottone;
TextView testo,testo2;
DBcont db=new DBcont(getApplicationContext());
private static final int REQUEST_CODE=1979;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.jack_layout);
bottone=(Button)findViewById(R.id.bottone);
testo=(TextView)findViewById(R.id.text1);
testo2=(TextView)findViewById(R.id.textView1);
}
public void speakButtonClicked(View v){
getContatti();
Ascolto();
}
public void Ascolto(){
Intent StT=new Intent();
StT.setAction(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
StT.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
StT.putExtra(RecognizerIntent.EXTRA_PROMPT,"Acquisizione audio,riconoscimento...");
startActivityForResult(StT,REQUEST_CODE);
}
public void getContatti(){
ContentResolver cr = getContentResolver();
db.open();
ContentResolver pcr = getContentResolver();
Cursor cur=cr.query(uri, null, null, null, sortOrder);
Cursor Pcur;
while(cur.moveToNext()){
String nome=cur.getString(cur.getColumnIndex(Nome));
String id=cur.getString(cur.getColumnIndex(_id));
String Tid[]={id};
Pcur=pcr.query(uri, colonna, id, Tid, null);
if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER)))>0){
while(Pcur.moveToNext()){
String numero=Pcur.getString(Pcur.getColumnIndex(Numero));
db.inserisci(nome, numero, id);
}
Pcur.close();
}
}
Toast t=Toast.makeText(getApplicationContext(), "Database caricato", Toast.LENGTH_LONG);
t.show();;
db.close();
cur.close();
}
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode==REQUEST_CODE && resultCode==RESULT_OK){
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
testo2.setText(matches.get(0));
db.open();
db.leggi(matches.get(0));
}
super.onActivityResult(requestCode, resultCode, data);
}
}
L'errore credo consista in una sbagliata implementazione del Context...perdonate l'ignoranza.