@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
int myId = position;
final int item_id = cursor.getInt(cursor.getColumnIndex(databaseHelper.USER_TABLE_COLUMN_ID));
PreferencesUtil.setIntPreferences(ListViewActivity.this,"myId",myId);
}
PreferencesUtil.java
public static void setIntPreferences(Context context, String key, int value) {
SharedPreferences p = context.getSharedPreferences("pref", context.MODE_PRIVATE);
SharedPreferences.Editor editor = p.edit();
editor.putInt(key, value);
editor.commit();
}
public static int getIntPreferences(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
int myIntValue = sp.getInt("your_int_key", -1);
return myIntValue;
}
In other classes
int myId = PreferencesUtil.getIntPreferences(detailUserActivity.this,"myId");
log.i("otherClass","myId = "myId);
It says myid=-1 in the log ㅜ-1 Is there a way to send the value of 0 to myId when you click on the item in position 0?
android java
The setIntPreferences(), getIntPreferences() function has code written to access the files "pref", "your_pref", respectively. This means that the default value of -1 is returned because the position is stored in prep.xml and the value is read in your_prep.xml. Use the same name to access one file.
© 2024 OneMinuteCode. All rights reserved.