Java 从所有方法访问SimpleCrsorAdapter
我试图在我的类中设置一个“SimpleCursorAdapter”对象变量,以便所有的方法都可以访问它,所有这些都是非常标准的东西。目前,我的“OnCreate”方法中出现一个错误,显示以下错误: 错误:Java 从所有方法访问SimpleCrsorAdapter,java,android,Java,Android,我试图在我的类中设置一个“SimpleCursorAdapter”对象变量,以便所有的方法都可以访问它,所有这些都是非常标准的东西。目前,我的“OnCreate”方法中出现一个错误,显示以下错误: 错误: cursorAdapter cannot be resolved to a type. 下面是类中声明的SimpleCursorAdapter: package com.example.flybase2; import android.app.AlertDialog; import and
cursorAdapter cannot be resolved to a type.
下面是类中声明的SimpleCursorAdapter:
package com.example.flybase2;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
public class ViewAppointments extends ListActivity implements OnClickListener {
SimpleCursorAdapter cursorAdapter;
ListView searchedAppView;
ImageButton searchAppoints;
EditText searchAppName;
Long id;
DBHandlerApp delApp = new DBHandlerApp(this, null, null);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appointmentview);
searchedAppView = (ListView)findViewById(android.R.id.list);
searchAppoints = (ImageButton) findViewById(R.id.btnSearchAppointName);
searchAppName = (EditText) findViewById(R.id.inputAppointName);
DBHandlerApp DBAppointments = new DBHandlerApp(this, null, null);
DBHandlerApp searchApps = new DBHandlerApp(this, null, null);
searchApps.open();
Cursor cursor = searchApps.getAppointmentsData();
searchApps.close();
startManagingCursor(cursor);
@SuppressWarnings("static-access")
String [] from = new String [] {DBAppointments.KEY_NAMEAPP, DBAppointments.KEY_TYPEAPP, DBAppointments.KEY_TIMEAPP, DBAppointments.KEY_DATEAPP, DBAppointments.KEY_COMMENTAPP};
int [] to = new int [] {R.id.txtAppointName, R.id.txtAppointType, R.id.txtAppointTime, R.id.txtAppointDate, R.id.txtAppointCom};
@SuppressWarnings("deprecation")
//ERROR: cursorAdapter cannot be resolved.
cursorAdapter = new SimpleCursorAdapter(this, R.layout.setappointviews, cursor, from, to);
searchedAppView.setAdapter(cursorAdapter);
searchedAppView.setTextFilterEnabled(true);
searchAppoints.setOnClickListener(this);
searchAppName.addTextChangedListener(new TextWatcher()
{
@Override
public void afterTextChanged(Editable s) {
cursorAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
};
你不需要那个东西。你可以做:
searchedAppView.getAdapter(); //listView.getAdapter()
无论您想在活动中的什么位置。您的问题在于注释的位置
@SuppressWarnings("deprecation")
位于导致错误的
onCreate()
内部。是否导入了android.widget.SimpleCursorAdapter
?你最近清理过你的项目吗?@A--C我导入了“导入android.support.v4.widget.SimpleCursorAdapter;”我已经清理了这个项目,没有什么区别。如果我把我的全班都贴出来会有帮助吗?当然,肯定是很小的东西。@A--C我已经把全班都贴上去了。我无法想象为什么,但我想知道我的textwatcher使用“onCreate”方法是否会引起问题?目前,这也不起作用。但这可能是因为它看不到光标或适配器对象!此外,您不能在方法之外初始化delApp
,因为它需要有效的上下文。(在方法之外使用this
时要小心。)初始化delApp
的最早时间是在onCreate()中。