将数据存储在Android Studio的文本文件中

时间:2015-03-14 08:31:11

标签: android sqlite

我有两个问题:

1。我有一个包含单词及其等效分数的文本文件。这就是它的样子:

word_equivscore melody_3

我使用文本文件来存储一堆单词及其相应的分数。就是这样:

textview>点击按钮搜索单词>如果单词在数据库/文本文件中,则在Listview中添加单词>另外,明确的文本视图。

我目前正在使用的代码:

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。如何在listview中限制重复输入?看我的代码。

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);

我正在为我的数据库使用Android Studio和SQLite。

P.S。我对这两个人一无所知。

2 个答案:

答案 0 :(得分:0)

  1. 那么问题是什么?
  2. 使用set而不是ArrayList作为myList变量。

答案 1 :(得分:0)

扩展CursorAdapter并在bindview方法中编写你的代码