字符串转换为数组(机器人) [英] Convert String to Array (android)

查看:142
本文介绍了字符串转换为数组(机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从游标得到一个字符串数据,但我不知道如何将其转换为阵列。我该怎么办呢?

 的String [] mString;
为(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()){
   mTitleRaw = cursor.getString(cursor.getColumnIndex(SBooksDbAdapter.KEY_TITLE_RAW));
}
mString = mTitleRaw ????
 

解决方案

您可以只换mTitleRaw到像这样的单个元素的数组:

  mString =新的String [] {m​​TitleRaw};
 

更新: 你可能需要的是所有行添加到一个数组,你可以用一个ArrayList做的,变异回像这样一个String []数组:

  ArrayList的字符串=新的ArrayList();
为(cursor.moveToFirst();!cursor.isAfterLast(); cursor.moveToNext()){
   串mTitleRaw = cursor.getString(cursor.getColumnIndex(SBooksDbAdapter.KEY_TITLE_RAW));
   strings.add(mTitleRaw);
}
斯汀[] mString =(字符串[])strings.toArray(新的String [strings.size());
 

I get a String data from Cursor, but I don't know how to convert it to Array. How can I do that?

String[] mString;
for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) {
   mTitleRaw = cursor.getString(cursor.getColumnIndex(SBooksDbAdapter.KEY_TITLE_RAW));
}
mString = mTitleRaw ????

解决方案

You could just wrap mTitleRaw into a single element array like so:

mString = new String[] { mTitleRaw };

Update: What you probably want is to add all the rows to a single array, which you can do with an ArrayList, and mutate back to a String[] array like so:

ArrayList strings = new ArrayList();
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
   String mTitleRaw = cursor.getString(cursor.getColumnIndex(SBooksDbAdapter.KEY_TITLE_RAW));
   strings.add(mTitleRaw);
}
Sting[] mString = (String[]) strings.toArray(new String[strings.size()]);

这篇关于字符串转换为数组(机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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