daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. protected void onCreate(Bundle savedInstanceState) {
  2.     super.onCreate(savedInstanceState);
  3.     setContentView(R.layout.activity_main);
  4.  
  5.     SQLiteDatabase sqLiteDatabase = getBaseContext().openOrCreateDatabase("ContactsDB.db", MODE_PRIVATE, null);
  6.  
  7.     lv = (ListView) findViewById(R.id.contactListView2);
  8.     buttonAdd = (Button) findViewById(R.id.buttonAdd);
  9.  
  10.     String tempSQL = "CREATE TABLE IF NOT EXISTS contactsList(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,firstName TEXT,lastName TEXT,phone TEXT,email TEXT,address TEXT,note TEXT);";
  11.     sqLiteDatabase.execSQL(tempSQL);
  12.     Log.d(TAG, "onCreate: Table created");
  13.  
  14.     db = openOrCreateDatabase("ContactsDB.db", MODE_PRIVATE, null);
  15.  
  16.     //show name in listView
  17.  
  18.     Cursor cursor2 = db.rawQuery("SELECT firstName FROM contactsList;", null);
  19.     ArrayList ar = new ArrayList();//new
  20.  
  21.     while (cursor2.moveToNext()) {
  22.         ar.add(cursor2.getString(0));
  23.     }
  24.  
  25.     ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, ar);
  26.     lv.setAdapter(arrayAdapter);
  27.     Log.d(TAG, "onCreate: showlist : list showing in listview now.");
  28.  
  29.     //setting for showing the edit layout
  30.  
  31.     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  32.         @Override
  33.         public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  34.             Cursor cr = db.rawQuery("SELECT * FROM contactsList WHERE firstName='"+lv.getItemAtPosition(i).toString()+"'",null);
  35.             StringBuffer sb = new StringBuffer();
  36.             while(cr.moveToNext()){
  37.                 sb.append(cr.getString(0)+"n");
  38.             }
  39.             Intent showAddLayout = new Intent(MainActivity.this, editContact.class);
  40.             showAddLayout.putExtra("name",sb.toString());
  41.             startActivity(showAddLayout);
  42.         }
  43.     });
  44.  
  45.  
  46.  
  47.     //button click listener to show other LayOut or page
  48.  
  49.     buttonAdd.setOnClickListener(new View.OnClickListener() {
  50.         @Override
  51.         public void onClick(View view) {
  52.  
  53.             Intent showAddLayout = new Intent(MainActivity.this, addNewContact.class);
  54.             startActivity(showAddLayout);
  55.  
  56.  
  57.         }
  58.     });
  59.  
  60. }//end of onCreate
  61.    
  62. <item
  63.     android:id="@+id/action_search"
  64.     android:icon="@android:drawable/ic_search_category_default"
  65.     android:title="@string/menu_search"
  66.     app:actionViewClass="android.widget.SearchView"
  67.     app:showAsAction="ifRoom|collapseActionView" />
  68.    
  69. <?xml version="1.0" encoding="utf-8"?>
  70. <menu xmlns:app="http://schemas.android.com/apk/res-auto"
  71.     xmlns:android="http://schemas.android.com/apk/res/android">
  72.     <item
  73.         android:id="@+id/menu_search"
  74.         android:icon="@android:drawable/ic_search_category_default"
  75.         android:title="@string/menu_search"
  76.         app:actionViewClass="android.widget.SearchView"
  77.         app:showAsAction="ifRoom|collapseActionView" />
  78. </menu>
  79.    
  80. private SearchView mSearchView;
  81.    
  82. Intent intent = getIntent();
  83. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  84.     mSearchString = intent.getStringExtra(SearchManager.QUERY);
  85.     //System.out.println(query);
  86. }
  87.    
  88. getMenuInflater().inflate(R.menu.menu_main, menu);
  89. MenuItem searchMenuItem = menu.findItem(R.id.action_search);
  90. mSearchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
  91. setupSearchResult(searchMenuItem);
  92.    
  93. private void setupSearchResult(MenuItem searchItem) {
  94.     SearchManager searchManager =
  95.             (SearchManager) getSystemService(Context.SEARCH_SERVICE);
  96.     mSearchView.setSearchableInfo(
  97.             searchManager.getSearchableInfo(getComponentName()));
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top