2017-11-06 3 views
0

데이터베이스 (SQL)에서 가져온 데이터가이 사용자 프로필에 있습니다. 따라서 textview 값은 사용자 입력이 아닌 데이터베이스에서 가져옵니다. 데이터베이스에서 Textview으로 설정된 값을 저장하는 방법은 무엇입니까? 첨부 된 이미지.공유 환경 설정에 저장할 Textview 값을 추출하는 방법

데이터는 성공적으로 내가 활동이 내가 다시 데이터를 표시 할 수 있습니다 파괴 그렇게 할 때 공유 환경 설정에 그 값을 저장해야

image 1

을 표시됩니다.

데이터 손실

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

} 

}

+0

이미 문자열의 texviews 값을 사용하고 있습니다. 그리고 당신은이 가치들이 구원받지 못한다고 말하고 있습니까? – Umair

+0

데이터 손실의 원인은 무엇입니까? 액티비티를 파기하고 화면 회전과 같은 이벤트를 재현하는 이벤트라면'savedInstanceState' 번들을 사용할 수 있습니다. 읽기 : https://developer.android.com/guide/components/activities/activity-lifecycle.html –

+0

@Umair 예, 저장된 값을 표시하려고 할 때 내 공유 환경 설정에 저장되지 않습니다. 아무 것도주지 않습니다. – RJDC

답변

0

대신 생성자의 정적 기능을 확인하고 아래와 같이 주요 활동을 초기화합니다.

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

주요 활동

SharePreferences myPref; //Global Variable 

내부에서 onCreate는

myPref = SessionManger.initialize(getApplicationContext()); 

지금 당신은 당신의 공유의 환경 설정을 사용할 수 있습니다.

난 당신이

또한 SessionManager에 클래스 내부도 게터를 넣어 recomment 것이다 Private_Mode 값이 0인가? 안드로이드 상수 Context.MODE_PRIVATE에 정의 된 것을 사용할 수 있습니까?

관련 문제