when I code a ListActivity to list 3 rows with reading data from the database, it showed me an error saying: with an unexpected result. The application can't run OK.
with the code as below:
Using java Syntax Highlighting
- package cn.com.child;
- import android.content.ContentProvider;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.net.Uri;
- import android.util.Log;
- public class DataProvider extends ContentProvider {
- private static final String DATABASE_NAME="bus.db";
- private static final int DATABASE_VERSION=2;
- private static final String STATION_TABLE_NAME="station";
- private static class DatabaseHelper extends SQLiteOpenHelper{
- DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- // TODO Auto-generated method stub
- db.execSQL("CREATE TABLE"+STATION_TABLE_NAME+"("+"s_id"+"TEXT,"+"s_name"+"TEXT"+");");
- String sql_1="insert into"+STATION_TABLE_NAME+"(s_id,s_name)values('1','hcz');";
- String sql_2="insert into"+STATION_TABLE_NAME+"(s_id,s_name)values('2','lsdx');";
- String sql_3="insert into"+STATION_TABLE_NAME+"(s_id,s_name)values('3','xal');";
- try {
- db.execSQL(sql_1);
- db.execSQL(sql_2);
- db.execSQL(sql_3);
- } catch (SQLException e) {
- Log.e("ERROR",e.toString());
- }
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // TODO Auto-generated method stub
- }
- }
- private DatabaseHelper mOpenHelper;
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public String getType(Uri uri) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public Uri insert(Uri uri, ContentValues values) {
- // TODO Auto-generated method stub
- return uri;
- }
- @Override
- public boolean onCreate() {
- // TODO Auto-generated method stub
- mOpenHelper = new DatabaseHelper(getContext());
- return true;
- }
- @Override
- public Cursor query(Uri uri, String[] projection, String selection,
- String[] selectionArgs, String sortOrder) {
- // Get the database and run the query
- SQLiteDatabase db = mOpenHelper.getReadableDatabase();
- Cursor c = db.query(STATION_TABLE_NAME,projection, null,null,null,null,null);
- return c;
- }
- @Override
- public int update(Uri uri, ContentValues values, String selection,
- String[] selectionArgs) {
- // TODO Auto-generated method stub
- return 0;
- }
- }
Parsed in 0.059 seconds, using GeSHi 1.0.8.4
and the data ListActivity
Using java Syntax Highlighting
- package cn.com.child;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import android.app.ListActivity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class data extends ListActivity {
- public static final String AUTHORITY="cn.com.provider.child";
- public static final Uri CONTENT_URI=Uri.parse("content://"+AUTHORITY+"/station");
- private static final String [] PROJECTION=new String []{
- "s_id","s_name"};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- //Return the intent that started this activity.
- Intent intent=getIntent();
- if(intent.getData()==null)
- intent.setData(CONTENT_URI);
- Cursor cur=getContentResolver().query(getIntent().getData(),PROJECTION,null,null,null);
- ArrayList<Map<String,Object>> coll =new ArrayList<Map<String,Object>>();
- Map<String,Object>item;
- cur.moveToFirst();
- while(!cur.isAfterLast()){
- item=new HashMap<String, Object>();
- item.put("c1",cur.getString(0)+","+cur.getString(1));
- coll.add(item);
- cur.moveToNext();
- }
- this.setListAdapter(new SimpleAdapter(this,coll,android.R.layout.simple_list_item_1,new String[]{"c1"},new int[]{
- android.R.id.text1}));
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- // TODO Auto-generated method stub
- // super.onListItemClick(l, v, position, id);
- finish();
- }
- }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
with the XML;
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="cn.com.child" android:versionCode="1"
- android:versionName="1.0.0">
- <application android:icon="@drawable/icon"
- android:label="@string/app_name">
- <activity android:name=".data"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category
- android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <provider android:name="DataProvider"
- android:authorities="cn.com.provider.child">
- </provider>
- </application>
- </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
someone tell me where is the error. I also have read the database tutorials written by plusminus months before, but still can't find the error. please someone help




