如何通过&QUOT [字符串]分开; - "并进行多列在Android? [英] how to separate [String] by "-" and make multiple column on android?

查看:108
本文介绍了如何通过&QUOT [字符串]分开; - "并进行多列在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个检索字典。其原料数据库是由分离 - 像

  Col​​umnA(Key_Word)ColumnB(Key_defination)
苹果 - 一类的水果
芒果 - 一个水果也

但我想使它3列(列C将Key_details)

  Col​​umnA ColumnB ColumnC(Key_details)
苹果 - 一个一种水果 - 圆形
芒果 - 一个水果也 - 发现在孟加拉国

当人们搜索,应用程序将显示列B和C.为什么我splited字符串的原因是我想说明Coumn B和C不同的窗口上。怎么做?这里是我的源头 -

  / **
 *包含逻辑从字典返回特定单词,
 *加载字典表时,需要创建它。
 * /
公共类DictionaryDatabase {
    私有静态最后弦乐TAG =DictionaryDatabase;    //列,我们将包含在字典表
    公共静态最后弦乐KEY_WORD = SearchManager.SUGGEST_COLUMN_TEXT_1;
    公共静态最后弦乐KEY_DEFINITION = SearchManager.SUGGEST_COLUMN_TEXT_2;    私有静态最后弦乐DATABASE_NAME =字典;
    私有静态最后弦乐FTS_VIRTUAL_TABLE =FTSdictionary;
    私有静态最终诠释DATABASE_VERSION = 2;    私人最终DictionaryOpenHelper mDatabaseOpenHelper;
    私有静态最后的HashMap<字符串,字符串> mColumnMap = buildColumnMap();
    公共DictionaryDatabase(上下文的背景下){
        mDatabaseOpenHelper =新DictionaryOpenHelper(背景);
    }    私有静态HashMap的<字符串,字符串> buildColumnMap(){
        HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
        map.put(KEY_WORD,KEY_WORD);
        map.put(KEY_DEFINITION,KEY_DEFINITION);
        map.put(BaseColumns._IDROWID AS+
                BaseColumns._ID);
        map.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_IDROWID AS+
                SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);
        map.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_IDROWID AS+
                SearchManager.SUGGEST_COLUMN_SHORTCUT_ID);
        返回地图;
    }
    公共光标屏幕取词(ROWID的字符串,字符串[]列){
        字符串选择=ROWID =?;
        的String [] = selectionArgs两个新的String [] {} ROWID;        返回查询(选择,selectionArgs两个,列);
    }
    公共光标getWordMatches(查询字符串,字符串[]列){
        字符串选择= KEY_WORD +匹配吗?
        的String [] = selectionArgs两个新的String [] {查询+*};        返回查询(选择,selectionArgs两个,列);
    }
    私人光标查询(选择字符串,字符串[] selectionArgs两个,字符串[]列){
        SQLiteQueryBuilder建设者=新SQLiteQueryBuilder();
        builder.setTables(FTS_VIRTUAL_TABLE);
        builder.setProjectionMap(mColumnMap);        光标光标= builder.query(mDatabaseOpenHelper.getReadableDatabase()
                列,选择,selectionArgs两个,NULL,NULL,NULL);        如果(光标== NULL){
            返回null;
        }否则如果(!cursor.moveToFirst()){
            cursor.close();
            返回null;
        }
        返回游标;
    }    私有静态类DictionaryOpenHelper扩展SQLiteOpenHelper {        私人最终上下文mHelperContext;
        私人SQLiteDatabase mDatabase;
        私有静态最后弦乐FTS_TABLE_CREATE =
                    创建虚拟表+ FTS_VIRTUAL_TABLE +
                    使用FTS3(+
                    KEY_WORD +,+
                    KEY_DEFINITION +);;        DictionaryOpenHelper(上下文的背景下){
            超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
            mHelperContext =背景;
        }        @覆盖
        公共无效的onCreate(SQLiteDatabase DB){
            mDatabase = DB;
            mDatabase.execSQL(FTS_TABLE_CREATE);
            loadDictionary();
        }        私人无效loadDictionary(){
            新主题(新的Runnable接口(){
                公共无效的run(){
                    尝试{
                        借词();
                    }赶上(IOException异常五){
                        抛出新的RuntimeException(E);
                    }
                }
            })。开始();
        }        私人无效借词()抛出IOException
            Log.d(TAG加载的话...);
            最后的资源资源= mHelperContext.getResources();
            为InputStream的InputStream = resources.openRawResource(R.raw.definitions);
            读者的BufferedReader =新的BufferedReader(新的InputStreamReader(InputStream的));            尝试{
                串线;
                而((行= reader.readLine())!= NULL){
                    串[]字符串= TextUtils.split(行, - );
                    如果(strings.length 2)继续;
                    长的id = addWord(串[0] .trim(),字符串[1] .trim());
                    如果(ID℃,){
                        Log.e(TAG,无法添加一句话:+串[0] .trim());
                    }
                }
            } {最后
                reader.close();
            }
            Log.d(TAG,加载完成的话。);
        }        众长addWord(字符串文字,字符串的定义){
            ContentValues​​ initialValues​​ =新ContentValues​​();
            initialValues​​.put(KEY_WORD,字);
            initialValues​​.put(KEY_DEFINITION,定义);            返回mDatabase.insert(FTS_VIRTUAL_TABLE,空,initialValues​​);
        }        @覆盖
        公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
            Log.w(TAG,从版本升级数据库+ oldVersion +到
                    + NEWVERSION +,这将摧毁所有旧数据);
            db.execSQL(DROP TABLE IF EXISTS+ FTS_VIRTUAL_TABLE);
            的onCreate(DB);
        }
    }}


解决方案

 的String [] = ARR string.split( - );

串#分裂(字符串正则表达式)


  

返回一个字符串周围拆分此字符串计算的数组
  给定的正前pression的比赛。


编辑:

 长ID = addWord(串[0] .trim(),字符串[1] .trim());

上面的行应该是:

 长ID = addWord(串[0] .trim(),字符串[1] .trim(),字符串[2] .trim());

和改变你的addWord功能我下面提到。

 众长addWord(串词,定义字符串,字符串的详细信息){
        ContentValues​​ initialValues​​ =新ContentValues​​();
        initialValues​​.put(KEY_WORD,字);
        initialValues​​.put(KEY_DEFINITION,定义);
        initialValues​​.put(KEY_DETAILS,详细说明);        返回mDatabase.insert(FTS_VIRTUAL_TABLE,空,initialValues​​);
    }

I'm working on a Searchable dictionary. Its Raw database is separated by "-" like

ColumnA(Key_Word)     ColumnB(Key_defination)
Apple                 - One kind of Fruit
Mango                 - One fruit also

But I want to make it 3 column, (Column C will be Key_details)

ColumnA          ColumnB                 ColumnC (Key_details)
Apple          - One kind of Fruit     - Round shape
Mango          - One fruit also        - Found in Bangladesh

When people search A, application will show Column B and C. The reason why i splited string is I want to show Coumn B and C on different window. How to do that? Here is my source-

/**
 * Contains logic to return specific words from the dictionary, and
 * load the dictionary table when it needs to be created.
 */
public class DictionaryDatabase {
    private static final String TAG = "DictionaryDatabase";

    //The columns we'll include in the dictionary table
    public static final String KEY_WORD = SearchManager.SUGGEST_COLUMN_TEXT_1;
    public static final String KEY_DEFINITION = SearchManager.SUGGEST_COLUMN_TEXT_2;

    private static final String DATABASE_NAME = "dictionary";
    private static final String FTS_VIRTUAL_TABLE = "FTSdictionary";
    private static final int DATABASE_VERSION = 2;

    private final DictionaryOpenHelper mDatabaseOpenHelper;
    private static final HashMap<String,String> mColumnMap = buildColumnMap();


    public DictionaryDatabase(Context context) {
        mDatabaseOpenHelper = new DictionaryOpenHelper(context);
    }

    private static HashMap<String,String> buildColumnMap() {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put(KEY_WORD, KEY_WORD);
        map.put(KEY_DEFINITION, KEY_DEFINITION);
        map.put(BaseColumns._ID, "rowid AS " +
                BaseColumns._ID);
        map.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, "rowid AS " +
                SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);
        map.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, "rowid AS " +
                SearchManager.SUGGEST_COLUMN_SHORTCUT_ID);
        return map;
    }


    public Cursor getWord(String rowId, String[] columns) {
        String selection = "rowid = ?";
        String[] selectionArgs = new String[] {rowId};

        return query(selection, selectionArgs, columns);


    }


    public Cursor getWordMatches(String query, String[] columns) {
        String selection = KEY_WORD + " MATCH ?";
        String[] selectionArgs = new String[] {query+"*"};

        return query(selection, selectionArgs, columns);
    }


    private Cursor query(String selection, String[] selectionArgs, String[] columns) {
        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(FTS_VIRTUAL_TABLE);
        builder.setProjectionMap(mColumnMap);

        Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(),
                columns, selection, selectionArgs, null, null, null);

        if (cursor == null) {
            return null;
        } else if (!cursor.moveToFirst()) {
            cursor.close();
            return null;
        }
        return cursor;
    }

    private static class DictionaryOpenHelper extends SQLiteOpenHelper {

        private final Context mHelperContext;
        private SQLiteDatabase mDatabase;
        private static final String FTS_TABLE_CREATE =
                    "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE +
                    " USING fts3 (" +
                    KEY_WORD + ", " +
                    KEY_DEFINITION + ");";

        DictionaryOpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            mHelperContext = context;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            mDatabase = db;
            mDatabase.execSQL(FTS_TABLE_CREATE);
            loadDictionary();
        }

        private void loadDictionary() {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        loadWords();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();
        }

        private void loadWords() throws IOException {
            Log.d(TAG, "Loading words...");
            final Resources resources = mHelperContext.getResources();
            InputStream inputStream = resources.openRawResource(R.raw.definitions);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            try {
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] strings = TextUtils.split(line, "-");
                    if (strings.length < 2) continue;
                    long id = addWord(strings[0].trim(), strings[1].trim());
                    if (id < 0) {
                        Log.e(TAG, "unable to add word: " + strings[0].trim());
                    }
                }
            } finally {
                reader.close();
            }
            Log.d(TAG, "DONE loading words.");
        }

        public long addWord(String word, String definition) {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_WORD, word);
            initialValues.put(KEY_DEFINITION, definition);

            return mDatabase.insert(FTS_VIRTUAL_TABLE, null, initialValues);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
            onCreate(db);
        }
    }

}

解决方案

String[] arr = string.split("-");

String#split(String regex)

Returns the array of strings computed by splitting this string around matches of the given regular expression.

EDIT:

long id = addWord(strings[0].trim(), strings[1].trim());

Above line should be:

long id = addWord(strings[0].trim(), strings[1].trim(),strings[2].trim());

And change your addWord function as I mentioned below.

public long addWord(String word, String definition,String details) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_WORD, word);
        initialValues.put(KEY_DEFINITION, definition);
        initialValues.put(KEY_DETAILS, details);

        return mDatabase.insert(FTS_VIRTUAL_TABLE, null, initialValues);
    }

这篇关于如何通过&QUOT [字符串]分开; - &QUOT;并进行多列在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆