When I clicked on Android on Item, the position was clearly marked as 0, but I sent the value to another class in the shared preferences mode, so when I read the value, it appears as -1.

Asked 2 years ago, Updated 2 years ago, 30 views

 @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

2022-09-22 11:04

1 Answers

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.


2022-09-22 11:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.