当我使用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("");
}
});
}
});
}
}
答案 0 :(得分:1)
之前我没有使用SimpleCursorAdapter
,但documentation表示构造函数是:
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);