AMcoder - javascript, python, java, html, php, sql

Ինչպես արդյունահանել Textview արժեքը՝ Համօգտագործվող նախապատվություններում պահելու համար

Ես ունեմ այս Օգտվողի պրոֆիլը, որտեղ տվյալները գալիս են Տվյալների բազայից (SQL), ուստի իմ textview արժեքը գալիս է տվյալների բազայից և ոչ թե օգտվողի մուտքագրումից: Ինչպես կարող եմ պահպանել այն արժեքը, որը սահմանված է իմ Textview-ի համար տվյալների բազայից: Պատկերները կցված են:

Տվյալները հաջողությամբ ցուցադրվեցին

պատկեր 1

Ես պետք է պահեմ այդ արժեքները Համօգտագործվող նախապատվություններում, որպեսզի երբ գործունեությունը ոչնչացվի, ես կարողանամ նորից ցուցադրել տվյալները:

Տվյալների կորուստ

պատկեր 2

Սա իմ ծածկագրերն են, որոնք մուտք են գործում տվյալների բազայից:

greetingTextView = (TextView) findViewById(R.id.greeting_text_view);
        totpoints = (TextView) findViewById(R.id.au_tpresult);
        totshare = (TextView) findViewById(R.id.au_tsresult);
        btnLogOut = (Button) findViewById(R.id.logout_button);
        cardshow = (ImageView) findViewById(R.id.card_stack);


        Intent intent = getIntent();
        String user = intent.getStringExtra("customers_firstname");
        String user1 = intent.getStringExtra("customers_lastname");
        String user2 = intent.getStringExtra("reward_points");
        String user3 = intent.getStringExtra("NoShares");
        String user4 = intent.getStringExtra("CardType_ID");
        String user5 = intent.getStringExtra("Card_No");


        greetingTextView.setText(user + " " + user1);
        totpoints.setText(user2);
        totshare.setText(user3);


        if (user4 == (null)) {
            ((ImageView) findViewById(R.id.card_stack)).setImageResource(R.drawable.thar_silver);
        } else if (user4.equals("0")) {
            ((ImageView) findViewById(R.id.card_stack)).setImageResource(R.drawable.thar_silver);
        } else if (user4.equals("1")) {
            ((ImageView) findViewById(R.id.card_stack)).setImageResource(R.drawable.thar_gold);

        }

        // Progress dialog
        btnLogOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                session.logoutUser();
                Toast.makeText(getApplicationContext(), "Session Ended", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(getApplicationContext(), LoginActivityEN.class);
                startActivity(i);
            }
        });

        session.checkLogin();

        //CARD NUMBER to Bitmap to Barcode


        // barcode data
        String barcode_data = user5;

        // barcode image
        Bitmap bitmap = null;
        bmbc = (ImageView) findViewById(R.id.bitmap_barcode);
        bmbc_text = (TextView) findViewById(R.id.bitmap_barcode_text);

        try {

            bitmap = encodeAsBitmap(barcode_data, BarcodeFormat.CODE_128, 800, 150);
            bmbc.setImageBitmap(bitmap);
            bmbc_text.setText(user5);


        } catch (WriterException e) {
            e.printStackTrace();

    String output_name, output_points, output_share, output_id;


    output_name = greetingTextView.getText().toString();
    output_points = totpoints.getText().toString();
    output_share = totshare.getText().toString();
    output_id =  bmbc_text.getText().toString();

    session.createUserSession(output_name, output_points, output_share, output_id);
        Toast.makeText(getApplicationContext(), "Session Override", Toast.LENGTH_SHORT).show();
        }

    session.getUserDetails();

    if (session != null) {
        SharedPreferences sharedPreferences = getSharedPreferences("AFCOOP", Context.MODE_PRIVATE);
        String getvaluefromname = sharedPreferences.getString("custoname", new String());
        String getvaluefrompoints = sharedPreferences.getString("totalpoints", new String());


        Toast.makeText(getApplicationContext(), "Not null", Toast.LENGTH_SHORT).show();
        greetingTextView.setText(getvaluefromname);
        totpoints.setText(getvaluefrompoints);



    } else if (session == null) {
    }
}

Վերջին մասը չի աշխատում, քանի որ միայն օգտագործողի մուտքագրումը պահվում է Համօգտագործվող նախապատվություններում: Ես պետք է հանեմ արժեքը Տեքստային տեսքից և պահեմ այն ​​Համօգտագործվող նախապատվություններում և ցուցադրեմ այն: Շնորհակալություն!

MySessionManager

// Համօգտագործվող նախապատվությունների խմբագիր Խմբագիր;

// Context
Context _context;

// Shared pref mode
int PRIVATE_MODE = 0;

// Sharedpref file name
private static final String PREF_NAME = "AFCOOP";

// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";

// User name (make variable public to access from outside)
public static final String KEY_NAME = "name";

// Email address (make variable public to access from outside)
public static final String KEY_EMAIL = "email";

// Email address (make variable public to access from outside)
public static final String KEY_UNAME = "custoname";

// Email address (make variable public to access from outside)
public static final String KEY_UPOINTS = "totalpoints";

// Email address (make variable public to access from outside)
public static final String KEY_USHARE = "totalshare";

// Email address (make variable public to access from outside)
public static final String KEY_UCARDNUM = "cardnumber";




// Constructor
public SessionManager(Context context){
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}

/**
 * Create login session
 * */
public void createLoginSession(String name, String password) {
    // Storing login value as TRUE
    editor.putBoolean(IS_LOGIN, true);

    // Storing name in pref
    editor.putString(KEY_NAME, name);

    // Storing email in pref
    editor.putString(KEY_EMAIL, password);

    // commit changes
    editor.commit();
}

public void createUserSession(String custoname, String totalpoints, String totalshare, String cardnumber){
    // Storing login value as TRUE
    editor.putBoolean(IS_LOGIN, true);

    // Storing name in pref
    editor.putString(KEY_UNAME, custoname);

    // Storing email in pref
    editor.putString(KEY_UPOINTS, totalpoints);
    // Storing email in pref

    editor.putString(KEY_USHARE, totalshare);
    // Storing email in pref

    editor.putString(KEY_UCARDNUM, cardnumber);

    // commit changes
    editor.commit();
}

/**
 * Check login method wil check user login status
 * If false it will redirect user to login page
 * Else won't do anything
 * */
public void checkLogin(){
    // Check login status
    if(!this.isLoggedIn()){
        // user is not logged in redirect him to Login Activity
        Intent i = new Intent(_context, LoginActivityEN.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Starting Login Activity
        _context.startActivity(i);
    }

}





/**
 * Get stored session data
 * */
public HashMap<String, String> getUserDetails(){
    HashMap<String, String> user = new HashMap<String, String>();
    // user name
    user.put(KEY_NAME, pref.getString(KEY_NAME, null));

    // user email id
    user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));

    user.put(KEY_UNAME, pref.getString(KEY_UNAME, null));

    user.put(KEY_UPOINTS, pref.getString(KEY_UPOINTS, null));

    user.put(KEY_USHARE, pref.getString(KEY_USHARE, null));

    user.put(KEY_UCARDNUM, pref.getString(KEY_UCARDNUM, null));



    // return user
    return user;
}

/**
 * Clear session details
 * */
public void logoutUser(){
    // Clearing all data from Shared Preferences
    editor.clear();
    editor.commit();

    // After logout redirect user to Loing Activity
    Intent i = new Intent(_context, LoginActivityEN.class);
    // Closing all the Activities
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    // Add new Flag to start new Activity
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    // Staring Login Activity
    _context.startActivity(i);
}

/**
 * Quick check for login
 * **/
// Get Login State
public boolean isLoggedIn(){
    return pref.getBoolean(IS_LOGIN, false);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    setContentView(R.layout.activity_user);

}

}



Պատասխանները:


1

Կառուցողի փոխարեն ստատիկ ֆունկցիա կազմեք և այն սկզբնավորեք ձեր հիմնական գործունեության մեջ, ինչպես ստորև:

public static void initialize(Context context){
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}

Հիմնական գործունեություն

SharePreferences myPref; //Global Variable

Inside onCreate

myPref = SessionManger.initialize(getApplicationContext());

Այժմ դուք կարող եք օգտագործել ձեր ընդհանուր նախադրյալները:

Ես խորհուրդ կտայի ձեզ տեղադրել getter-ը նաև SessionManager Class-ի ներսում

Նաև Private_Mode արժեքը 0 է: Կարող եք փորձել օգտագործել Android Constants Context.MODE_PRIVATE-ում սահմանվածը:

06.11.2017
Նոր նյութեր

Օգտագործելով Fetch Vs Axios.Js-ը՝ HTTP հարցումներ կատարելու համար
JavaScript-ը կարող է ցանցային հարցումներ ուղարկել սերվեր և բեռնել նոր տեղեկատվություն, երբ դա անհրաժեշտ լինի: Օրինակ, մենք կարող ենք օգտագործել ցանցային հարցումը պատվեր ներկայացնելու,..

Տիրապետել հանգստության արվեստին. մշակողի ուղեցույց՝ ճնշման տակ ծաղկելու համար
Տիրապետել հանգստության արվեստին. մշակողի ուղեցույց՝ ճնշման տակ ծաղկելու համար Ինչպե՞ս հանգստացնել ձեր միտքը և աշխատեցնել ձեր պրոցեսորը: Ինչպես մնալ հանգիստ և զարգանալ ճնշման տակ...

Մեքենայի ուսուցում բանկային և ֆինանսների ոլորտում
Բարդ, խելացի անվտանգության համակարգերը և հաճախորդների սպասարկման պարզեցված ծառայությունները բիզնեսի հաջողության բանալին են: Ֆինանսական հաստատությունները, մասնավորապես, պետք է առաջ մնան կորի..

Ես AI-ին հարցրի կյանքի իմաստը, այն ինչ ասում էր, ցնցող էր:
Այն պահից ի վեր, երբ ես իմացա Արհեստական ​​ինտելեկտի մասին, ես հիացած էի այն բանով, թե ինչպես է այն կարողանում հասկանալ մարդկային նորմալ տեքստը, և այն կարող է առաջացնել իր սեփական արձագանքը դրա..

Ինչպես սովորել կոդավորումը Python-ում վագրի պես:
Սովորելու համար ծրագրավորման նոր լեզու ընտրելը բարդ է: Անկախ նրանից, թե դուք սկսնակ եք, թե առաջադեմ, դա օգնում է իմանալ, թե ինչ թեմաներ պետք է սովորել: Ծրագրավորման լեզվի հիմունքները, դրա..

C++-ի օրական բիթ(ե) | Ամենաերկար պալինդրոմային ենթաշարը
C++ #198-ի ամենօրյա բիթ(ե), Ընդհանուր հարցազրույցի խնդիր. Ամենաերկար պալինդրոմային ենթատող: Այսօր մենք կանդրադառնանք հարցազրույցի ընդհանուր խնդրին. Ամենաերկար palindromic substring...

Kydavra ICAReducer՝ ձեր տվյալների ծավալայինությունը նվազեցնելու համար
Ի՞նչ է ICAReducer-ը: ICAReducer-ն աշխատում է հետևյալ կերպ. այն նվազեցնում է նրանց միջև բարձր փոխկապակցված հատկանիշները մինչև մեկ սյունակ: Բավականին նման է PCAreducer-ին, չնայած այն..