Android Studio - 从SQLite数据库加载RecyclerView,在EditText中输入一个单词。
创始人
2024-08-15 22:00:26
0

以下是一个使用SQLite数据库加载RecyclerView,并在EditText中输入一个单词的Android Studio示例代码:

  1. 首先,在你的项目中添加以下依赖项到你的app的build.gradle文件中:
dependencies {
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'androidx.sqlite:sqlite:2.1.0'
}
  1. 创建一个名为Word的Java类,用于表示数据库中的单词:
public class Word {
    private int id;
    private String word;

    public Word(int id, String word) {
        this.id = id;
        this.word = word;
    }

    public int getId() {
        return id;
    }

    public String getWord() {
        return word;
    }
}
  1. 创建一个名为DatabaseHelper的Java类,用于管理SQLite数据库:
public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "words.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "words";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_WORD = "word";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_WORD + " TEXT)";
        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public void insertWord(String word) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_WORD, word);
        db.insert(TABLE_NAME, null, contentValues);
        db.close();
    }

    public List getAllWords() {
        List wordList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);

        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
                String word = cursor.getString(cursor.getColumnIndex(COLUMN_WORD));
                Word newWord = new Word(id, word);
                wordList.add(newWord);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();

        return wordList;
    }
}
  1. 创建一个名为WordAdapter的Java类,用于将单词列表显示在RecyclerView中:
public class WordAdapter extends RecyclerView.Adapter {
    private List wordList;

    public class WordViewHolder extends RecyclerView.ViewHolder {
        public TextView wordTextView;

        public WordViewHolder(View view) {
            super(view);
            wordTextView = view.findViewById(R.id.wordTextView);
        }
    }

    public WordAdapter(List wordList) {
        this.wordList = wordList;
    }

    @Override
    public WordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.word_item, parent, false);
        return new WordViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(WordViewHolder holder, int position) {
        Word word = wordList.get(position);
        holder.wordTextView.setText(word.getWord());
    }

    @Override
    public int getItemCount() {
        return wordList.size();
    }
}
  1. res/layout目录下创建一个名为activity_main.xml的XML布局文件,用于显示RecyclerView和EditText:


    

    
  1. res/layout目录下创建一个名为`word_item

相关内容

热门资讯

透视免费!poker mast... 透视免费!poker master安卓版外挂(透视)竟然真的有挂(详细教程)(有挂教学)-哔哩哔哩,...
揭秘关于!云扑克辅助软件(透视... 揭秘关于!云扑克辅助软件(透视)其实是真的有挂(详细教程)(有挂了解)-哔哩哔哩1、许多玩家不知道云...
我来教大家!鱼扑克有挂(辅助挂... 我来教大家!鱼扑克有挂(辅助挂)的确真的有挂(详细教程)(有挂介绍)-哔哩哔哩1、玩家可以在鱼扑克有...
重要通知!德州wpk辅助真的(... 重要通知!德州wpk辅助真的(透视)果真是真的有挂(详细教程)(有挂攻略)-哔哩哔哩是一款可以让一直...
终于懂了!pokerx智能软件... 终于懂了!pokerx智能软件(透视)果真真的有挂(详细教程)(有挂了解)-哔哩哔哩;(需添加指定薇...
玩家亲测!fishpoker俱... 玩家亲测!fishpoker俱乐部有挂(透视)果真真的有挂(详细教程)(有挂详情)-哔哩哔哩是一款可...
一分钟了解!德州ai辅助神器w... 一分钟了解!德州ai辅助神器wpk(辅助挂)确实真的有挂(详细教程)(有挂方法)-哔哩哔哩;德州ai...
终于懂了!德州全自动辅助(辅助... 相信很多朋友都在电脑上玩过德州全自动辅助吧,但是很多朋友都在抱怨用电脑玩起来不方便。为此小编给大家带...
透明讲解!德州全自动辅助(透视... 透明讲解!德州全自动辅助(透视)原来是真的有挂(详细教程)(有挂技巧)-哔哩哔哩相信很多朋友都在电脑...
玩家必备攻略!红龙扑克模拟器(... 玩家必备攻略!红龙扑克模拟器(辅助挂)的确是真的有挂(详细教程)(有挂总结)-哔哩哔哩1、让任何用户...