构造函数 SimpleCursorAdapter(ShoppingList, int, Cursor, SQLiteDatabase, ListView) 未定义

发布于 2024-10-15 12:07:10 字数 2921 浏览 6 评论 0原文

当我使用 SimpleCursorAdapter 时,Eclipse 说构造函数 SimpleCursorAdapter(ShoppingList, int, Cursor, SQLiteDatabase, ListView) 未定义。

这是我的.java:

package com.shoppinglist;

import java.util.Locale;

import android.app.Dialog;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ShoppingList extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView listview = (ListView) findViewById(android.R.id.list);

        SQLiteDatabase db;
        db = openOrCreateDatabase("Items.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);

        final String CREATE_TABLE_ITEMS = 
            "CREATE TABLE tbl_items ("
            + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + "item_name TEXT);";
        db.execSQL(CREATE_TABLE_ITEMS);

        Cursor c = db.query(CREATE_TABLE_ITEMS, null, null, null, null, null, null);
        startManagingCursor(c);

        ListAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.rowlayout, c, db, listview);
        setListAdapter(adapter);

        Button button1main = (Button) findViewById(R.id.add);
        button1main.setOnClickListener(new OnClickListener()  {
            @Override
            public void onClick(View v)  {
            final Dialog additem = new Dialog(ShoppingList.this);
            additem.setContentView(R.layout.maindialog);
            final EditText et = (EditText)additem.findViewById(R.id.edittext);
            additem.setTitle("Type your item");
            additem.setCancelable(true);
            et.setHint("Type the name of an item...");

            Button button = (Button) additem.findViewById(R.id.cancel);
            button.setOnClickListener(new OnClickListener()  {
                @Override
                public void onClick(View v)  {
                    additem.dismiss();
                }
            });
            additem.show();

            Button ok = (Button) additem.findViewById(R.id.ok);
            ok.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    final String text = et.getText().toString();
                    additem.dismiss();
                    et.setText("");
                }
            });
       }
        });
    }
}

When I use a SimpleCursorAdapter, Eclipse says The constructor SimpleCursorAdapter(ShoppingList, int, Cursor, SQLiteDatabase, ListView) is undefined.

Here is my .java:

package com.shoppinglist;

import java.util.Locale;

import android.app.Dialog;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ShoppingList extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView listview = (ListView) findViewById(android.R.id.list);

        SQLiteDatabase db;
        db = openOrCreateDatabase("Items.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);

        final String CREATE_TABLE_ITEMS = 
            "CREATE TABLE tbl_items ("
            + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + "item_name TEXT);";
        db.execSQL(CREATE_TABLE_ITEMS);

        Cursor c = db.query(CREATE_TABLE_ITEMS, null, null, null, null, null, null);
        startManagingCursor(c);

        ListAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.rowlayout, c, db, listview);
        setListAdapter(adapter);

        Button button1main = (Button) findViewById(R.id.add);
        button1main.setOnClickListener(new OnClickListener()  {
            @Override
            public void onClick(View v)  {
            final Dialog additem = new Dialog(ShoppingList.this);
            additem.setContentView(R.layout.maindialog);
            final EditText et = (EditText)additem.findViewById(R.id.edittext);
            additem.setTitle("Type your item");
            additem.setCancelable(true);
            et.setHint("Type the name of an item...");

            Button button = (Button) additem.findViewById(R.id.cancel);
            button.setOnClickListener(new OnClickListener()  {
                @Override
                public void onClick(View v)  {
                    additem.dismiss();
                }
            });
            additem.show();

            Button ok = (Button) additem.findViewById(R.id.ok);
            ok.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    final String text = et.getText().toString();
                    additem.dismiss();
                    et.setText("");
                }
            });
       }
        });
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱情眠于流年 2024-10-22 12:07:10

我以前没有使用过 SimpleCursorAdapter ,但是 文档指出构造函数是:

public SimpleCursorAdapter (Context context, 
        int layout, Cursor c, String[] from, int[] to)

但是你的对构造函数的调用会传入 SQLiteDatabase 来代替 String 数组:

ListAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.rowlayout, c, db, listview);

I've not used a SimpleCursorAdapter before, but the documentation states that the constructor is:

public SimpleCursorAdapter (Context context, 
        int layout, Cursor c, String[] from, int[] to)

however your call to the constructor passes in an SQLiteDatabase in place of the String array:

ListAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.rowlayout, c, db, listview);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文