将数据存储在Android Studio中的文本文件中
我有两个问题: 一,。我有一个文本文件,其中包含单词及其对应的分数。这就是它的样子: 单词/分数 旋律3 我使用文本文件来存储一堆单词及其相应的分数。它是这样的: 文字视图中的文字>单击按钮搜索文字>如果文字在数据库/文字文件中,请在Listview中添加文字>否则,清除文字视图 我当前使用的代码:将数据存储在Android Studio中的文本文件中,android,sqlite,Android,Sqlite,我有两个问题: 一,。我有一个文本文件,其中包含单词及其对应的分数。这就是它的样子: 单词/分数 旋律3 我使用文本文件来存储一堆单词及其相应的分数。它是这样的: 文字视图中的文字>单击按钮搜索文字>如果文字在数据库/文字文件中,请在Listview中添加文字>否则,清除文字视图 我当前使用的代码: public long insertData(String Word, int Score) { SQLiteDatabase db = helper.getWritabl
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
二,。如何限制listview中的重复条目?查看我的代码
阵列适配器;
ArrayList myList;
//一次创建
myList=新的ArrayList();
adapter=new ArrayAdapter(getApplicationContext(),android.R.layout.simple\u list\u item\u 1,myList);
setAdapter(适配器);
//onCreate结束
字符串s1,s2,newData;
公共void viewWord(视图){
s1=search.getText().toString();
s2=dbHelper.getData(s1);
newData=text.getText().toString();
s1.trim();
s2.trim();
generatedString.trim();
//在数据库中进行评分
计算();
adapter=(ArrayAdapter)字列表.getAdapter();
adapter.add((字符串)newData);
我正在为我的数据库使用Android Studio和SQLite
另外,我对这两个问题一无所知。扩展CursorAdapter并在bindview方法中编写代码你为什么要谈论一个
文本文件
?你使用的是一个数据库,它不是文本文件……哦,好吧,我一点都不知道。
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);