Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. package com.example.preferences;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.content.SharedPreferences;
  8. import android.view.Menu;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.ListView;
  15.  
  16. public class MainActivity extends Activity implements OnClickListener {
  17. Button ketqua;
  18. Button kick;
  19. Button dondep;
  20. EditText nhap;
  21. ListView hienthi;
  22. ArrayList<String> arrayList;
  23. ArrayAdapter<String> adap;
  24.  
  25. int index = 0;
  26. final String PREFS = "Prefenrences";
  27. SharedPreferences myfrefs;
  28. SharedPreferences.Editor myeditor;
  29.  
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. kick = (Button) findViewById(R.id.bt_kick);
  35. ketqua = (Button) findViewById(R.id.bt_hienthi);
  36. dondep = (Button) findViewById(R.id.bt_xoa);
  37. nhap = (EditText) findViewById(R.id.ed_nhap);
  38. hienthi = (ListView) findViewById(R.id.lv_hienthi);
  39. arrayList = new ArrayList<String>();
  40.  
  41. adap = new ArrayAdapter<String>(this,
  42. android.R.layout.simple_list_item_1, arrayList);
  43.  
  44. kick.setOnClickListener(this);
  45. ketqua.setOnClickListener(this);
  46. dondep.setOnClickListener(this);
  47. myfrefs = getSharedPreferences(PREFS, MainActivity.MODE_PRIVATE);
  48. myeditor = myfrefs.edit();
  49.  
  50. }
  51.  
  52. @Override
  53. public boolean onCreateOptionsMenu(Menu menu) {
  54. // Inflate the menu; this adds items to the action bar if it is present.
  55. getMenuInflater().inflate(R.menu.main, menu);
  56. return true;
  57. }
  58.  
  59. @Override
  60. public void onClick(View arg0) {
  61. // TODO Auto-generated method stub
  62. // arrayList.add(nhap.getText().toString());
  63. if (arg0.getId() == kick.getId()) {
  64. myeditor.putString(String.valueOf(index), nhap.getText().toString());
  65.  
  66. myeditor.commit();
  67.  
  68. arrayList.add(myfrefs.getString(String.valueOf(index), nhap
  69. .getText().toString()));
  70.  
  71.  
  72.  
  73.  
  74.  
  75. } else if (arg0.getId() == ketqua.getId()) {
  76.  
  77. for (int i = 0; i <= index; i++) {
  78. myeditor.putString(String.valueOf(i), nhap.getText().toString());
  79. myeditor.commit();
  80.  
  81.  
  82. hienthi.setAdapter(adap);
  83. }
  84.  
  85. } else if (arg0.getId() == dondep.getId()) {
  86.  
  87. hienthi.setAdapter(null);
  88. }
  89.  
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement