2017-08-27 2 views
-4

J'ai créé un adaptateur personnalisé pour mon ListView suivant this tutorial.
Mais lorsque j'exécute mon application sur mon appareil, une erreur se produit au démarrage.
L'erreur s'affiche lorsque la méthode onCreate() de MainActivity tente d'appeler la méthode getData() de la classe NotesDbHelper.SQLiteException: base de données inconnue

Pouvez-vous m'aider?

MainActivity.java

public class MainActivity extends Activity 
{ 

private EditText mEditText; 
private Button mButton; 

NotesCustomAdapter notesCustomAdapter = null; 
ListView listView = null; 
NotesDbHelper database = null; 
ArrayList<Notes> notes = null; 

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

    mButton = (Button) findViewById(R.id.button); 
    mEditText = (EditText) findViewById(R.id.editText); 

    database = new NotesDbHelper(this); 
    notes = database.getData(); 
    notesCustomAdapter= new NotesCustomAdapter(this,R.layout.notes_details,notes); 

    listView = (ListView) findViewById(R.id.simpleListView); 
    listView.setAdapter(notesCustomAdapter); 

    mButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String input = mEditText.getText().toString(); 

      if (input.length() > 0) { 
      database.insertNote(input); 
      } 
     } 
    }); 

    listView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> a, View v, final int position, long id) { 
    AlertDialog.Builder adb=new AlertDialog.Builder(MainActivity.this); 
    adb.setTitle("Delete?"); 
    adb.setMessage("Are you sure you want to delete this note?"); 
    final int positionToRemove = position; 
    adb.setNegativeButton("Cancel", null); 
    adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      database.deleteNote(which); 
      notes.remove(positionToRemove); 
      notesCustomAdapter.remove(String.valueOf(positionToRemove)); 
      notesCustomAdapter.notifyDataSetChanged(); 
     }}); 
    adb.show(); 
    } 
}); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
    } 
} 

NotesDbHelper.java

public class NotesDbHelper extends SQLiteOpenHelper { 

    public static final String DATABASE_NAME = "Notes.db"; 
    public static final String NOTES_TABLE_NAME = "Notes.user"; 
    public static final String NOTES_COLUMN_ID = "id"; 
    public static final String NOTES_COLUMN_NAME = "n_text"; 

    public NotesDbHelper(Context context) { 
     super(context, DATABASE_NAME, null, 1); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("create table " + NOTES_TABLE_NAME + 
        "(_id integer primary key AUTOINCREMENT NOT NULL," + NOTES_COLUMN_NAME + 
        ")" 
     ); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int i, int i1) { 
     db.execSQL("DROP TABLE IF EXISTS "+ DATABASE_NAME); 
     onCreate(db); 
    } 

    public boolean insertNote(String text) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put("n_text", text); 

     db.insert(NOTES_TABLE_NAME, null, contentValues); 
     return true; 
    } 

    public ArrayList<Notes> getData() { 
     SQLiteDatabase db = this.getReadableDatabase(); 
     ArrayList<Notes> notes = new ArrayList<Notes>(); 
     Cursor result = db.rawQuery("select * from "+ NOTES_TABLE_NAME , null); 
     while(result.moveToNext()){ 
      notes.add(new Notes(result.getString(result.getColumnIndex(NOTES_COLUMN_NAME)))); 

     } 
     return notes; 
    } 

    public boolean updateNotes(int id, int text) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues contentValues = new ContentValues(); 
     contentValues.put("n_text", text); 

     db.update(NOTES_TABLE_NAME, contentValues, "id = ? ", new String[]{Integer.toString(id)}); 
     return true; 
    } 

    public Integer deleteNote(Integer id) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     return db.delete(NOTES_TABLE_NAME, 
       "id = ? ", 
       new String[]{Integer.toString(id)}); 
    } 

} 

Notes.java

public class Notes { 
    String text; 

    public Notes(String text) { 
     this.text = text; 
    } 

    public String getText() { 
     return text; 
    } 

    public void setText(String text) { 
     this.text = text; 
    } 
} 

NotesCustomAdapter.java

public class NotesCustomAdapter extends ArrayAdapter{ 
    private Context context; 
    private ArrayList<Notes> notes; 

    public NotesCustomAdapter(Context context, int textViewResourceId, ArrayList objects) { 
     super(context,textViewResourceId, objects); 

     this.context= context; 
     notes=objects; 

    } 

    private class ViewHolder 
    { 
     TextView text; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     ViewHolder holder=null; 
     if (convertView == null) 
     { 
      LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = vi.inflate(R.layout.notes_details, null); 

      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.text); 
      convertView.setTag(holder); 

     } 
     else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     Notes textNotes = notes.get(position); 
     holder.text.setText(textNotes.getText()); 
     return convertView; 
    } 
} 

LogCat

la première ligne dit:

java.lang.RuntimeException: Unable to start activity ComponentInfo{agenda.com/agenda.com.MainActivity}: android.database.sqlite.SQLiteException: unknown database Notes (code 1): while compiling: create table Notes.user(_id integer primary key AUTOINCREMENT NOT NULL,n_text) 

enter image description here

+5

Renommer '' Notes.user' à Notes_user'since il n'est pas un nom de table valide. –

+0

Ça a marché! Merci beaucoup. –

Répondre

2

Pourquoi Notes.user? Vous mettez un point inutile. Allez comparer au lien que vous avez référencé.

Il suffit d'utiliser Notes ou UserNotes