【问题标题】:Android SQLite show specific row on spinner selectedAndroid SQLite 在选定的微调器上显示特定行
【发布时间】:2021-10-01 01:57:08
【问题描述】:

我从 3 个难度中获得了创纪录的高分:简单、中等、困难。所有数据进入 1 个表。我有 1 个活动来显示所有的高分。

在那个活动中,我创建了一个微调器,让用户选择他们想要显示的难度数据。所有数据都会显示在listview(ScoreView)中。

这里我要做的是在微调器上选择用户时显示特定数据。

有什么办法吗?

这是我的数据库:

 public Data_Collect[] getScore() {
    Data_Collect[] data = null;
    String[] colums = {"Difficult", "BRedo", "BCorrect"};
    String orderBy = "Difficult ASC";
    Cursor cursor = getReadableDatabase().query(
            "score",
            colums,
            null, null, null, null,
            orderBy);
    if (cursor.getCount()>0 && cursor.moveToNext()) {

        data = new Data_Collect[cursor.getCount()];
        int counter = 0;

        do {

            data[counter] = new Data_Collect(
                    cursor.getString(0),
                    cursor.getInt(1),
                    cursor.getInt(2));
            counter++;

        }
        while (cursor.moveToNext());
    }

    cursor.close();
    return data;
}

这是我的活动:

ArrayAdapter<String> adapterDifficult = new ArrayAdapter<>(
            Score.this, android.R.layout.simple_list_item_1, Difficulty
    );
    SpinnerDifficulty.setAdapter(adapterDifficult);
    SpinnerDifficulty.setOnItemSelectedListener(this);

    new Thread(new Runnable() {
        @Override
        public void run() {
            RecordDB db = new RecordDB(Score.this);
            Data_Collect[] data = db.getScore();
            db.close();

            if (data != null) {
                adapter = new RecordAdapter(Score.this, R.layout.recordlist, R.id.text_Difficulty, data);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ScoreView.setAdapter(adapter);
                    }
                });
            }
        }
    }).start();

}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

【问题讨论】:

  • 嗨,卡尔文。我认为你最好将你的难度级别传递给 onItemSelect 的 getScore() 方法——例如getScodre(Difficult) — 并获取此级别的特定数据,然后通知适配器显示所需的数据。
  • 嗨,你的意思是把Runnable放在onItemSelect里面吗?但是如何显示特定的行?
  • 您应该更改查询并获取特定列,因为我意识到您将所需数据存储在特定列中:Cursor cursor = getReadableDatabase().query("score", colums, null, null, null , null, orderBy); to ——> Cursor cursor = getReadableDatabase().query("score", "Difficult", null, null, null, null, orderBy);
  • OIC,非常感谢!

标签: java android sqlite listview android-arrayadapter


【解决方案1】:

定义困难 = '简单'

Data_Collect[] data = null;
String[] colums = {"Difficult", "BRedo", "BCorrect"};
String orderBy = "Difficult ASC";
Cursor cursor = getReadableDatabase().query(
        "score",
        colums,
        "Difficult ='Easy'", null, null, null,
        orderBy);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多