tags:

views:

35

answers:

1

I wanna set a textview as the values from SQLite when I click the ListView Here is my way:

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)); 

But there is a exception, here is the log:

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)

Here is the full code:

public class SQLiteTest2 extends Activity {

private DoDB ToDoDB;<br>
private Cursor myCursor;<br>
private int _id;<br>
private ListView myListView;<br>
public TextView textStudyUID;<br>
protected ListAdapter adapter1;<br>


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));
}
});

A: 

You'll not be able to access myCursor inside the anonymous function (onItemClick) due to scoping, but the cursor you're after is already being passed to the function. Retrieve it using:

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

You may want to consider naming the parameters correctly to improve readability. I.e.:

public void onItemClick(AdapterView<? extends Adapter> parent, View v, int position, long id)
Matty F
I use "private final Cursor myCursor" but it says needed "="
Kooper
Apologies, the first answer was wrong but I've updated with the correct method.
Matty F
I've changed like this:public void onItemClick(AdapterView<? extends Adapter> parent,android.view.View arg1, int position, long id) {CursorAdapter adapter = (CursorAdapter) parent.getAdapter(); Cursor myCursor = adapter.getCursor(); myCursor.moveToPosition(position);setContentView(R.layout.main2);_id = myCursor.getInt(9);textStudyUID.setText(myCursor.getString(0));
Kooper
But still exception :(
Kooper
What is the exception?
Matty F
Still this "java.lang.NullPointerException"
Kooper
Can you paste the second line (the one after NullPointerException), it should give you the line number of the code which is causing the exception. And then open up SQLiteTest2, find the line mentioned and paste it here
Matty F
Success! Really thank you:)
Kooper
How did you end up fixing it?Also, can you click the tick button next to the answer to accept it. This improves your accept rate and people will be more willing to answer your questions... currently you only have 25% accept rate which you should try to improve on.
Matty F