I HAVE TWO QUESTIONS:
1. I have a text file containing words and its equivalent score. This is how it looks like:
word_equivscore melody_3
I use text file to store a bunch of words and its corresponding score. It goes this way:
word on textview > click button to search the word > if the word is on the database/text file, add word in Listview > Else, clear textview.
Codes I am currently using:
public long insertData(String Word, int Score)
{
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(DBHelper.WORD, Word);
contentValues.put(DBHelper.SCORE, Score);
long id=db.insert(DBHelper.TABLE_NAME, null, contentValues);
return id;
}
public String getData(String word)
{
SQLiteDatabase db = helper.getWritableDatabase();
String[] columns={DBHelper.WORD, DBHelper.SCORE};
Cursor cursor=db.query(DBHelper.TABLE_NAME, columns, DBHelper.WORD+ "= '"+word+"'", null, null, null, null);
StringBuffer buffer = new StringBuffer();
while(cursor.moveToNext())
{
int index1 =cursor.getColumnIndex(DBHelper.WORD);
int index2 =cursor.getColumnIndex(DBHelper.SCORE);
String words =cursor.getString(index1);
String score =cursor.getString(index2);
buffer.append(score +"\n");
}
return buffer.toString();
}
static class DBHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME= "BoggleIT";
private static final String TABLE_NAME ="dbtable";
private static final String UID ="_id";
private static final String WORD ="word";
private static final String SCORE ="score";
private static final int DATABASE_VERSION=4;
private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME +" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+WORD+" VARCHAR(255), "+SCORE+" VARCHAR(255));";
private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME;
private Context context;
DBHelper(Context context) {
// TODO Auto-generated constructor stub
super (context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
//Message.message(context, "constructor called");
}
//onCreate here
2. How to restrict duplicate entry in listview? See my codes.
ArrayAdapter<String> adapter;
ArrayList<String> myList;
//onCreate
myList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, myList);
wordList.setAdapter(adapter);
//end of onCreate
String s1, s2, newData;
public void viewWord(View view) {
s1 = search.getText().toString();
s2 = dbHelper.getData(s1);
newData = text.getText().toString();
s1.trim();
s2.trim();
generatedString.trim();
//score if in database
calculate();
adapter = (ArrayAdapter) wordList.getAdapter();
adapter.add((String)newData);
I am using Android Studio and SQLite for my db.
P.S. I don't have any idea on these two.
Aucun commentaire:
Enregistrer un commentaire