The article structure

Demand in this paper, the

A colleague from the technical department proposed to save the recently entered five key information on the APP, and supports the drop-down selection. This is also for the convenience of customers in the current multiple input information, to help them quickly input.

Implementation approach

The requirements you want to achieve at the moment

  • You want the implementation to save the key information entered by the user.
  • This is triggered by clicking the drop-down on the right, allowing the user to select the message that has been sent successfully.
  • Use SharedPreferences to save the data after each APP exit.
  • After the data is sent successfully, the data stored in the background is updated for logical judgment.

Code logic

The picture below is the final realization effect. When entering the id and key, click the send button, and the data will be automatically saved to the array in the background after success. Click the drop-down icon on the right to pop it up.

After that, we added the label of “Clear history”, that is, every time we add and update the background array, the next label of the array is “Clear history”.

s_btnDown.setOnClickListener(this); // Focus on itCopy the code
case R.id.btnDown: showListPopulWindow(); // Call the PopuWindow function break;Copy the code

When you click on it, it fires the PopuWindow function, which binds its drop-down box to the bottom of the TextBox TAB.

private void showListPopulWindow() { final DeviceKeySecretManager list = ((MainActivity)getActivity()).deviceKeySecretManager; // Final ListPopupWindow ListPopupWindow; listPopupWindow = new ListPopupWindow(getActivity()); listPopupWindow.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list.getKeyList())); / / android built-in layout, or design your own style listPopupWindow. SetAnchorView (s_etAppKey); Listpopupwindow.setmodal (true) listPopupwindow.setmodal (true); ListPopupWindow. SetOnItemClickListener (new AdapterView. OnItemClickListener () {/ / set a click listener @ Override public void onItemClick(AdapterView<? > adapterView, View view, int i, long l) { if (list.KeySecretSum==i){ list.Clear(); // Click clear s_etAppKey.settext (""); // Display the selected option contents on the EditText s_etAppSecret.settext (""); }else{ s_etAppKey.setText(list.getKeyList()[i]); S_etappsecret.settext (list.getSecretList()[I]); // Display the selected options in EditText s_etAppSecret.settext (list.getSecretList()[I]); } listPopupWindow.dismiss(); // If already selected, hide it}}); listPopupWindow.show(); // Display the ListPopWindow}Copy the code

Logical classes for key management:

It is used to cache the historical key information after sending successfully, bind it to the drop-down list later, and save and extract the data into the cache when the APP exits and first loads.

/** * Id and key management * Store a maximum of five keys, and loop overwrite starts with the first one. */ class DeviceKeySecretManager { public DeviceKeySecretManager() { CurrentSaveIndex = 0; } public String[] getKeyList() { return KeyList; } public String[] getSecretList() { return SecretList; } /** * add the new key and secret to the keystore * 1, check whether there is a key in the keystore, if there is a key, update the secret value * 2, add the key/secret value directly, if there is no key, add the secret value directly. */ public void addBufferKeyAndSecret(String key, String secret) { if (IntegerConversion.UseLoop(KeyList,key)) { int index=0; for (int i=0; i<KeyList.length; i++) { if (KeyList[i].equals(key)){ index=i; break; } } KeyList[index]=key; SecretList[index]=secret; } else { if (KeySecretSum == 5) { CurrentSaveIndex = CurrentSaveIndex == 5 ? 0 : CurrentSaveIndex; KeyList[CurrentSaveIndex] = key; SecretList[CurrentSaveIndex] = secret; CurrentSaveIndex++; } else { KeyList[CurrentSaveIndex] = key; SecretList[CurrentSaveIndex] = secret; CurrentSaveIndex++; KeySecretSum++; KeyList[CurrentSaveIndex] = "Clear history "; } } } public void Clear() { CurrentSaveIndex = 0; KeySecretSum = 0; for (int i = 0; i < KeyList.length; i++) { KeyList[i] = null; } for (int i = 0; i < SecretList.length; i++) { SecretList[i] = null; } } public int CurrentSaveIndex = 0; Public int KeySecretSum = 0; // Total number of keys. A maximum of 5 keys can be stored. private String[] KeyList = new String[6]; private String[] SecretList = new String[5]; }Copy the code

When the APP exits and loads for the first time, the data is saved and extracted locally.

/** * Read saved file */ private void getSavedPreference() {try {SharedPreferences pref = this.getSharedPreferences(getResources().getString(R.string.app_name), MODE_PRIVATE); int sum=pref.getInt("KeySecretSum", 0); for (int i=0; i<=sum; i++){ deviceKeySecretManager.getKeyList()[i]=pref.getString("Key"+i, ""); } for (int i=0; i<sum; i++){ deviceKeySecretManager.getSecretList()[i]=pref.getString("Secret"+i, ""); } deviceKeySecretManager.CurrentSaveIndex=sum==5? 0:sum; deviceKeySecretManager.KeySecretSum=sum; } catch (Exception ex) {}} /** * Save file ** / private void setSavePreference() {try {SharedPreferences = getSharedPreferences(getResources().getString(R.string.app_name), MODE_PRIVATE); SharedPreferences.Editor edit = pref.edit(); edit.putInt("KeySecretSum", deviceKeySecretManager.KeySecretSum); For (int I =0; i<=deviceKeySecretManager.KeySecretSum; i++){ edit.putString("Key"+i, deviceKeySecretManager.getKeyList()[i]); } for (int i=0; i<deviceKeySecretManager.KeySecretSum; i++){ edit.putString("Secret"+i, deviceKeySecretManager.getSecretList()[i]); } edit.commit(); } catch (Exception ex) { } }Copy the code

The following is the business logic when the send is successful:

@Override public void onClick(View v) { switch (v.getId()) { case R.id.btnSendData: if (! DeviceManager. GetInstance (). DeviceIsConnected ()) {tu. ToastShow (context, "equipment disconnected, unable to communicate." ); return; } the if (DeviceManager getInstance (). DeviceIsBusy ()) {tu. ToastShow (context, "device is busy, please wait..." ); return; } try { String key,secret; key=s_etAppKey.getText().toString(); secret=s_etAppSecret.getText().toString(); if (key.length()<=0||secret.length()<=0|| TextUtils.isEmpty(key)||TextUtils.isEmpty(secret)){ tu.ToastShow(context, "Identifiers and keys cannot be empty!" ); return; } // Call method concatenation string, send to the next machine device. int nResult = DeviceManager.getInstance().WriteRTKData(context, new byte[]{}); If (nResult > 0) {tu.toastshow (context, "argument successfully written "); ((MainActivity)getActivity()).deviceKeySecretManager.addBufferKeyAndSecret(key,secret); }} catch (Exception ex) {tu.toastshow (context, "failed to write argument!") ); } break; Case R.i.D.bbtnClearData: // Clear only the current id and key s_etAppKey.settext (""); s_etAppSecret.setText(""); break; case R.id.btnDown: showListPopulWindow(); // Call the PopuWindow function break; default: break; }}Copy the code

Conclusion:

Through the above business analysis, the code implementation can implement the specific requirements and keep a history of the last five. For the same problem/requirement, different people have different solutions. No one can say whose method is wrong, only that whose method is the most effective so far.

Small remarks

A person’s struggle, like pregnancy, a long time, will always be seen.

Life is short, I don’t want to pursue what I can’t see, I just want to grasp what I can see.

I am zai said, thank you for reading, I hope to progress and grow with you.

If it helps you, please like and forward.