jeudi 28 avril 2016

Application crashes when pressed on SQLite retrieve button

i tried to make an application with SQLite database. I made everything like in this tutorial https://www.youtube.com/watch?v=d_PjDM4nFxI but i changed "players" to "books" and didn't add the "position". Application turns on, but it crashes ("Unfortunately, Library has stopped") when pressed on Retrieve button. Maybe someone can help me? Thanks a lot!

Error:

04-28 16:16:27.889 2326-2326/com.example.kompiuteris.library E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.kompiuteris.library, PID: 2326 android.content.res.Resources$NotFoundException: String resource ID #0x1 at android.content.res.Resources.getText(Resources.java:312) at android.content.res.Resources.getString(Resources.java:400) at android.content.Context.getString(Context.java:409) at com.example.kompiuteris.library.MainActivity$2.onClick(MainActivity.java:72) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

DBAdapter.java

static final String ROW_ID ="id";
static final String NAME ="name";
static final String TAG = "DBAdapter";

static final String DBNAME="m_DB";
static final String TBNAME="m_TB";
static final int DBVERSION='1';

static final String CREATE_TB="CREATE TABLE m_TB(id INTEGER PRIMARY KEY AUTOINCREMENT,"
        + "name TEXT NOT NULL);";

final Context c;
SQLiteDatabase db;
DBHelper helper;
public DBAdapter(Context ctx) {
    this.c = ctx;
    helper = new DBHelper(c);
}


private static class DBHelper extends SQLiteOpenHelper {
    public DBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(CREATE_TB);
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("DBAdapter","Upgrading DB");
        db.execSQL("DROP TABLE IF EXISTS m_TB");
        onCreate(db);
    }
}
public DBAdapter openDB()
{
    try {
        db=helper.getWritableDatabase();
    } catch (SQLException e)
    {
        Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show();
    }
    return this;
}

public void close()
{
    helper.close();
}

public long add(String name)
{
    try {
        ContentValues cv = new ContentValues();
        cv.put(NAME,name);
        return db.insert(TBNAME,ROW_ID,cv);
    } catch (SQLException e)
    {
        e.printStackTrace();
    }
    return 0;
}

public Cursor getAllNames()
{
    String[] columns={ROW_ID,NAME};
    return db.query(TBNAME,columns,null,null,null,null,null);
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

ListView lv;
EditText nameTxt;
Button savebtn,retrievebtn;
ArrayList<String> books = new ArrayList<String>();
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    nameTxt=(EditText)findViewById(R.id.editText);

    savebtn=(Button)findViewById(R.id.saveBtn);
    retrievebtn=(Button)findViewById(R.id.retrieveBtn);

    lv = (ListView)findViewById(R.id.listView1);

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_selectable_list_item,books);
    final DBAdapter db = new DBAdapter(this);

    savebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            db.openDB();
            long result = db.add(nameTxt.getText().toString());

            if(result > 0)
            {
                nameTxt.setText("");
            }else
            {
                Toast.makeText(getApplicationContext(),"Failure", Toast.LENGTH_SHORT).show();
            }
            db.close();
        }
    });

    retrievebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            books.clear();
            db.openDB();
            Cursor c = db.getAllNames();

            while (c.moveToNext())
            {
                String name = getString(1);
                books.add(name);
            }
            lv.setAdapter(adapter);
            db.close();
        }
    });
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(),books.get(position), Toast.LENGTH_SHORT).show();
        }
    });
}

Aucun commentaire:

Enregistrer un commentaire