【发布时间】:2015-10-06 17:25:19
【问题描述】:
我有以下 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<string-array name="my_array">
<item>rome 999 3 4 5 7 9 11 17</item>
</string-array>
我用这个结构初始化一个sqlite表:
public static abstract class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_SCHEDULE = "schedule";
}
这是我解析值并将它们放入表中的代码
for (String item : myArray){
String[] split = item.split("\\s+");
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, split[0]);
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, split[1]);
String schedule="";
String[] stringArray = item.split("\\s+");
int length = stringArray.length;
for (int i = 2; i < length; i++) {
System.out.println(i);
if(schedule.trim().length()>0){
schedule += split[i]+" ";
}else{
schedule = split[i];
}
}
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_SCHEDULE, schedule);
db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
}
现在,之后我对它发出一个 sqlite 请求,例如:
Cursor cursor = db.query(
FeedReaderContract.FeedEntry.TABLE_NAME,
projection,
FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE+" = " + string ,
null,
null,
null,
sortOrder
);
我是这样用光标新建一个edittext的:
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
result = result + cursor.getString(iRow) + " " + cursor.getString(iName) +
" " + cursor.getString(iSchedule) +
"\n";
}
TextView textview = new TextView(getApplicationContext());
textview.setText(result);
RelativeLayout myLayout;
myLayout = (RelativeLayout) findViewById(R.id.content_main);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView tv = (TextView) findViewById(R.id.button);
textview.setTextColor(Color.RED);
params.addRule(RelativeLayout.BELOW, tv.getId());
myLayout.addView(textview, params);
但是,如果我更改 xml 文件并在第二个元素中引入文本,例如:
<?xml version="1.0" encoding="utf-8"?>
<string-array name="my_array">
<item>rome line1 3 4 5 7 9 11 17</item>
</string-array>
如果我重复一些事情并通过查找文本 line1 来发出 sqlite 请求,则应用程序崩溃。即在上面的代码中,sqlite 只需要一个数字而不是一个文本。我从这样的代码中不明白这种行为的起源。另外,我怎样才能使它也与文本一起使用?
【问题讨论】:
-
你能贴出你实际解析字符串并将其写入数据库的代码吗?
标签: android xml sqlite android-edittext