vendredi 29 avril 2016

Why is my SQLite database keep adding those data that have been added?

I just want my SQLite databse add the data one time, but My SQLite database keeps adding those data that have been added when I launch my app? How could I solve this problem? Can someone help me? I appreciate it.

Here is my java main class:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
public WebView web;
public DBHelper AsongHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initial();
    Insertdata();

}
public void initial() {
    AsongHelper = new DBHelper(this);
    AsongHelper.getWritableDatabase();
    AsongHelper.getReadableDatabase();
    ListView ml = (ListView) findViewById(R.id.listView1);
    List<listview_item>Songlist = AsongHelper.getSongsName();
    listview_adapter ada = new listview_adapter(this, Songlist);
    ml.setAdapter(ada);
    ml.setOnItemClickListener(this);
    web = (WebView) findViewById(R.id.webView1);
    WebSettings webSettings = web.getSettings();
    webSettings.setJavaScriptEnabled(true);
    web.getSettings().setCacheMode(webSettings.LOAD_CACHE_ELSE_NETWORK);
    web.setWebViewClient(
            new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }
            }
    );
    web.loadUrl("http://youtu.be/bMqKM9lqX7M");
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    List<String> UrlList =AsongHelper.geturl();
    web.loadUrl(UrlList.get(position));}
public void Insertdata(){
    AsongHelper.insertdata("小幸运", "https://youtu.be/bMqKM9lqX7M", "Jason Chen");
    AsongHelper.insertdata("Love Yourself","https://youtu.be/oyEuk8j8imI","Justin Bieber");
    AsongHelper.insertdata("Gentleman","https://youtu.be/lGVKrj6gpnI","Will Jay");
}
}

Here is my database class:

public class DBHelper extends SQLiteOpenHelper{

public static final String DATABASE_NAME = "ASong.op";
public static final String ASONG_TABLE_NAME = "Asongs";
public static final String ASONG_COLUMN_ID = "id";
public static final String ASONG_COLUMN_SONGSNAME = "SongsName";
public static final String ASONG_COLUMN_URL = "url";
public static final String ASONG_COLUMN_ARTIST = "artist";

public DBHelper(Context context)
{
    super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL( "create table Asongs " + "(id integer primary key, SongsName text,url text,artist text)"
    );
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS Asongs");
    onCreate(db);
}
public boolean insertdata  (String songsname, String url, String artist)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(ASONG_COLUMN_SONGSNAME,songsname);
    contentValues.put(ASONG_COLUMN_URL,url);
    contentValues.put(ASONG_COLUMN_ARTIST, artist);
    db.insert(ASONG_TABLE_NAME, null, contentValues);
    return true;
}
public List<listview_item> getSongsName(){
    SQLiteDatabase db = this.getReadableDatabase();
    List<listview_item> SongsNameList = new ArrayList<>();
    String[] SongsName={ASONG_COLUMN_SONGSNAME};

    Cursor cusor =  db.query(ASONG_TABLE_NAME,SongsName,null,null,null,null,null);
    //StringBuffer buffer = new StringBuffer();
    while(cusor.moveToNext()){
        int index1 = cusor.getColumnIndex(ASONG_COLUMN_SONGSNAME);
        String cid = cusor.getString(index1);
        //buffer.append(cid);
        SongsNameList.add(new listview_item(cid));
        cusor.close();
      ;}
    return SongsNameList;
}
public List<String> geturl(){
    List<String> SongsUrlList = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    String[] ColumnUrl={ASONG_COLUMN_URL};
    Cursor cusor =  db.query(ASONG_TABLE_NAME,ColumnUrl,null,null,null,null,null);
    StringBuffer buffer = new StringBuffer();
    while(cusor.moveToNext()){
        int index2 = cusor.getColumnIndex(ASONG_COLUMN_URL);
        String cid2 = cusor.getString(index2);
        buffer.append(cid2);
        SongsUrlList.add(buffer.toString());
    }
    cusor.close();
    return SongsUrlList;

}
}

Aucun commentaire:

Enregistrer un commentaire