【问题标题】:Why my strings can't be sorted like i want when i insert values from textview?当我从 textview 插入值时,为什么我的字符串不能像我想要的那样排序?
【发布时间】:2014-03-30 22:56:21
【问题描述】:

我有一个计数器来计算我点击按钮的次数。

public void onClick(View v) {

            counter++;
             text.setText("Score"+counter);



        }

然后我将这个值插入到我的数据库中

     text = (TextView) findViewById(R.id.textView1);
                final String Score = text.getText().toString();
                mySQLiteAdapter.openToWrite();

                mySQLiteAdapter.insert("sccore="Score);

                mySQLiteAdapter.close();

这是我的插入函数

public long insert(String content){
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_CONTENT, content);
    return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}

所以插入的计数器值是-> Score = 2 ,Score = 1 ,Score = 111, Score = 3, Score = 21

然后我想在另一个 texview 中看到这个分数:

 mySQLiteAdapter = new SQLiteAdapter(this);
    /*
     *  Open the same SQLite database
     *  and read all it's content.
     */
    mySQLiteAdapter = new SQLiteAdapter(this);
    mySQLiteAdapter.openToRead();
    String contentRead = mySQLiteAdapter.queueAll();
    listContent.setText(contentRead);
    mySQLiteAdapter.close();

我的函数 queueAll() 看起来像:

public String queueAll(){
    String[] columns = new String[]{KEY_CONTENT};
    Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
            null, null, null, null, KEY_CONTENT+" desc");
    String result = "";

    int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT);
    for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
        result = result + cursor.getString(index_CONTENT) + "\n";
    }

    return result;
}

好的,在文本视图中我看到了这个:

Score = 3  ,  Score=21 , Score=2 , Score=111, Score=1

但我想要

score=111, score=21, score=3, score=2, score=1.

为什么按 DESC 排序没有像我在 DESC 中想要的那样排序?为什么会这样想?为什么我插入的计数器值未按 DESC 排序?

public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_CONTENT = "Content";

//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
    "create table " + MYDATABASE_TABLE + " ("
    + KEY_CONTENT + " text not null);";

说实话,我的列 KEY_CONTENT 应该是 NUMBER 而不是 STRING。如何改变这个? 当我将 str 更改为 num 时,将按我想要的方式按 DESC 排序?

【问题讨论】:

  • 感谢您精心准备的问题。

标签: android database sqlite sorting textview


【解决方案1】:

您将号码存储为text,因此您可以获得字母排序顺序。

您可以在查询时将您的号码转换为integer

Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
        null, null, null, null, "CAST(" + KEY_CONTENT + " as integer) desc");

【讨论】:

  • 你是个巫师 :D。现在可以了。你能告诉我这是如何工作的吗? ""CAST(" + KEY_CONTENT + " as integer) desc")" 什么是强制转换?
  • @user3202351 它将您的列Content(存储为text)转换为integer 数据类型。由于您的所有记录都有一个有效整数,因此可以毫无问题地进行转换,然后根据数值进行排序。
  • 但是如果我插入的值是“qweq1qweqweScore=15”呢?它会投射什么?
  • 它将失败并出现错误,因为它无法投射。
  • 很高兴知道.. 无论如何感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
相关资源
最近更新 更多