SimpleCursorAdapter IllegalArgumentException:列不存在

时间:2015-02-07 14:23:52

标签: android sqlite android-listview simplecursoradapter

我已经阅读了类似的帖子,但没有一个像我一样使用外部数据库。这是我的问题。我有以下代码并且运行良好:

public class ArchaeologyMuseums extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_archaeology_museums);
    String[] from = new String[]{"museum_name", "museum_category", "museum_open_now", "museum_distance"};
    int[] to = new int[]{R.id.item_name, R.id.item_category, R.id.item_open_now, R.id.item_distance};
    DataBaseHelper myDbHelper = new DataBaseHelper(this);
    try {

        myDbHelper.createDataBase();

    } catch (IOException ioe) {
    }

    try {

        myDbHelper.openDataBase();
    } catch (SQLException sqle) {

        throw sqle;

    }


    Cursor c = myDbHelper.getArchaeologicalData();
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                getApplicationContext(), R.layout.list_layout, c, from, to);

        ListView list = (ListView) findViewById(R.id.listView1);

        list.setAdapter(adapter);}}   

然后我更改了我的数据库并添加了3个新列:museum_name_greek,museum_category_greek,museum_greek_hours并使用以下代码:

public class ArchaeologyMuseums extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_archaeology_museums);
    String[] gr = new String[]{"museum_greek_name", "museum_greek_category", "museum_greek_hours", "museum_distance"};
    String[] from = new String[]{"museum_name", "museum_category", "museum_open_now", "museum_distance"};
    int[] to = new int[]{R.id.item_name, R.id.item_category, R.id.item_open_now, R.id.item_distance};
    DataBaseHelper myDbHelper = new DataBaseHelper(this);
    String locale = Locale.getDefault().getLanguage();
    try {

        myDbHelper.createDataBase();

    } catch (IOException ioe) {
    }

    try {

        myDbHelper.openDataBase();
    } catch (SQLException sqle) {

        throw sqle;

    }


    Cursor c = myDbHelper.getArchaeologicalData();

    if(locale.equals("el")) {
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                getApplicationContext(), R.layout.list_layout, c, gr, to);

        ListView list = (ListView) findViewById(R.id.listView1);

        list.setAdapter(adapter);}

    else {
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                getApplicationContext(), R.layout.list_layout, c, from, to);

        ListView list = (ListView) findViewById(R.id.listView1);

        list.setAdapter(adapter);}}}  

因此,如果locale是“el”,我会得到IllegalArgumentException:列museum_greek_name不存在。我尝试了2个不同的游标调用(c,c_gr)但没有修复。有什么建议吗?

0 个答案:

没有答案