关于从SQLite获取值

时间:2010-07-05 12:47:25

标签: android sqlite

当我点击textview时,我想将SQLite设为ListView的值 这是我的方式:

myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
public void onItemClick(AdapterView arg0, 
android.view.View arg1, int arg2, long arg3) { 
myCursor.moveToPosition(arg2); 
setContentView(R.layout.main2); 
_id = myCursor.getInt(0); 
textStudyUID.setText(myCursor.getString(0)); 

但是有一个例外,这是日志:

07-05 12:37:16.613: ERROR/AndroidRuntime(17689): java.lang.NullPointerException
07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at irdc.sqlitetest2.SQLiteTest2$1.onItemClick(SQLiteTest2.java:53)
07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at android.widget.ListView.performItemClick(ListView.java:3285)
07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at android.os.Handler.handleCallback(Handler.java:587)
07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at android.os.Handler.dispatchMessage(Handler.java:92)
07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at android.os.Looper.loop(Looper.java:123)
07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at java.lang.reflect.Method.invoke(Method.java:521)

07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-05 12:37:16.613: ERROR/AndroidRuntime(17689): at dalvik.system.NativeStart.main(Native Method)

以下是完整代码:

public class SQLiteTest2<View> extends Activity {       
    private DoDB ToDoDB;
    private Cursor myCursor;
    private int _id;
    private ListView myListView;
    public TextView textStudyUID;
    protected ListAdapter adapter1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   
        myListView = (ListView)findViewById(R.id.ListView);
        textStudyUID = (TextView)findViewById(R.id.widget40);
        ToDoDB = new DoDB(this);
        myCursor = ToDoDB.select();
        SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(this,R.layout.list,myCursor,new String[]{DoDB._StudyUID},new int[]{R.id.listTextView1});
        myListView.setAdapter(adapter1);

        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0,android.view.View arg1, int arg2,                                     long arg3) {
               myCursor.moveToPosition(arg2);
               setContentView(R.layout.main2);
               _id = myCursor.getInt(9);
               textStudyUID.setText(myCursor.getString(0));
        }
      });

1 个答案:

答案 0 :(得分:0)

由于作用域,您将无法在匿名函数(onItemClick)内访问myCursor,但是您之前的光标已经传递给该函数。使用以下方法检索它:

CursorAdapter adapter = (CursorAdapter) arg0.getAdapter();
Cursor myCursor = adapter.getCursor();

您可能需要考虑正确命名参数以提高可读性。即:

public void onItemClick(AdapterView<? extends Adapter> parent, View v, int position, long id)