Advertisement
frozenking

XActivity.java

May 11th, 2012
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. package xx.x;
  2.  
  3. import android.app.Activity;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteException;
  7. import android.os.Bundle;
  8. import android.text.Editable;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14.  
  15. public class XActivity extends Activity {
  16.     /** Called when the activity is first created. */
  17.     @Override
  18.     public void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.main);
  21.        final TextView t = (TextView) findViewById(R.id.tv);
  22.      
  23.        final Button s = (Button) findViewById(R.id.button1);
  24.        final EditText et = (EditText) findViewById(R.id.et);
  25.        s.setOnClickListener(new OnClickListener() {
  26.        
  27.         public void onClick(View arg) {
  28.             // TODO Auto-generated method stub
  29.             String ee = et.getText().toString();
  30. //Code for database
  31.               SQLiteDatabase db;    
  32.               try{
  33.                 db = SQLiteDatabase.openDatabase("data/data/xx.x/databases/dictionary", null,0);
  34.                 String q = "SELECT * FROM LIST where( wlist like '"+ee+"%')"+"ORDER BY wlist";
  35.                   Cursor c = db.rawQuery(q, null);
  36.                   if(c.getCount() == 0)
  37.                   {
  38.                      
  39.                       t.setText("Not found1");
  40.                   }
  41.                   else  {
  42.                       String j="";
  43.                       c.moveToFirst();
  44.                       do {
  45.                           String cm = c.getString(c.getColumnIndex("wlist"));
  46.                          
  47.                         j = j+"\n"+cm;   
  48.                           } while (c.moveToNext());              
  49.                       t.setText(j);    
  50.                 c.close();
  51.                 db.close();  
  52.             }
  53.                 }
  54.                 catch(SQLiteException e){
  55.            
  56.                 db = openOrCreateDatabase("dictionary", MODE_PRIVATE, null);
  57.                 db.execSQL("CREATE TABLE IF NOT EXISTS LIST(wlist varchar);");
  58.                          
  59.                      
  60.                    db.execSQL("INSERT INTO LIST VALUES('add');");
  61.                    db.execSQL("INSERT INTO LIST VALUES('imran');");  
  62.                    db.execSQL("INSERT INTO LIST VALUES('rana');");
  63.                    db.execSQL("INSERT INTO LIST VALUES('role');");
  64.                    
  65.                 String q = "SELECT * FROM LIST where( wlist like '"+ee+"%')"+"ORDER BY wlist";
  66.                           Cursor c = db.rawQuery(q, null);
  67.                           if(c.getCount() == 0)
  68.                           {
  69.                               t.setText("Not found2");
  70.                           }
  71.                           else  {
  72.                               String j="";
  73.                               c.moveToFirst();
  74.                               do {
  75.                                   String cm = c.getString(c.getColumnIndex("wlist"));
  76.                                  
  77.                                 j = j+"\n"+cm;   
  78.                                   } while (c.moveToNext());              
  79.                               t.setText(j);    
  80.                         c.close();
  81.                         db.close();  
  82.                     }    
  83.                 }          
  84.                    
  85.         }
  86.     });
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement