Reputation: 5
In My DataBaseAdapter Class, I write a method getAll like that
public List<AllUserInfor> getAllInfor(int id) {
List<AllUserInfor> allInfor = new ArrayList<AllUserInfor>();
String selectQuery = "SELECT Name, Gender FROM MY_TABLE where _id = '"+id+"' ";
Cursor cursor = sqLiteDatabase.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
AllUserInfor alluserinfor = new AllUserInfor();
alluserinfor.setName(cursor.getString(1));
alluserinfor.setGender(cursor.getString(2));
allInfor.add(alluserinfor);
} while (cursor.moveToNext());
}
return allInfor;
}
and in the second activity, I have
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btShow:
// I get id from the first activity
Bundle extras = getIntent().getExtras();
id = extras.getInt("Roomid");
List<AllUserInfor> userInfor = mySQLiteAdapter.getAllInfor(id);
for (AllUserInfor aui : userInfor){
tvname.setText(aui.getName());
tvgender.setText(aui.getGender());
}
break;
}
}
this is the way I get id from first activity
lvroom.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// TODO Auto-generated method stub
cursor = (Cursor) lvroom.getItemAtPosition(position);
int item_id = cursor.getInt(cursor.getColumnIndex(SQLiteAdapter.KEY_ID));
Intent i = new Intent();
Bundle bundle = new Bundle();
bundle.putInt("Roomid", item_id);
i.putExtras(bundle);
i.setClass(FirstActivity.this, SecondActivity.class);
startActivityForResult(i, 0);
}
After I hit the Show button inf second activity, nothing change, its mean that the name and gender are not showed in second layout. Where are my mistakes? Help me
this is my logcat
03-10 18:01:20.790: E/AndroidRuntime(1224): FATAL EXCEPTION: main
03-10 18:01:20.790: E/AndroidRuntime(1224): java.lang.NullPointerException
03-10 18:01:20.790: E/AndroidRuntime(1224): at com.superman.medreport.SecondActivity.onClick(SecondActivity.java:127)
03-10 18:01:20.790: E/AndroidRuntime(1224): at android.view.View.performClick(View.java:4204)
03-10 18:01:20.790: E/AndroidRuntime(1224): at android.view.View$PerformClick.run(View.java:17355)
03-10 18:01:20.790: E/AndroidRuntime(1224): at android.os.Handler.handleCallback(Handler.java:725)
03-10 18:01:20.790: E/AndroidRuntime(1224): at android.os.Handler.dispatchMessage(Handler.java:92)
03-10 18:01:20.790: E/AndroidRuntime(1224): at android.os.Looper.loop(Looper.java:137)
03-10 18:01:20.790: E/AndroidRuntime(1224): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-10 18:01:20.790: E/AndroidRuntime(1224): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 18:01:20.790: E/AndroidRuntime(1224): at java.lang.reflect.Method.invoke(Method.java:511)
03-10 18:01:20.790: E/AndroidRuntime(1224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-10 18:01:20.790: E/AndroidRuntime(1224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-10 18:01:20.790: E/AndroidRuntime(1224): at dalvik.system.NativeStart.main(Native Method)
03-10 18:01:24.802: E/Trace(1256): error opening trace file: No such file or directory (2)
Upvotes: 0
Views: 83
Reputation: 23638
Do not cast your id
value into the String
as its an Integer
in your table.
Change your criteria as below to get value:
"SELECT Name, Gender FROM MY_TABLE where _id = "+id+" ";
Just remove the single " ' "
and write as "+id+"
not '"+id+"'
Upvotes: 1
Reputation: 1862
use this :
"SELECT Name, Gender FROM MY_TABLE where _id ="+id+";
instead of
"SELECT Name, Gender FROM MY_TABLE where _id = '"+id+"' ";
Upvotes: 0
Reputation: 2002
Is it because _id is an integer? And you are passing a string
'"+id+"'
should be
"+id+"
Upvotes: 2