2012-06-19 82 views
1

這個程序是爲了在Listview中顯示數據庫的類。我想通過給定的關鍵字添加在數據庫中搜索所需的數據。如何添加搜索代碼數據庫與關鍵字在同一類如何搜索數據庫中的一些關鍵字android

public class DisplayActivity extends Activity 
    { 

private ArrayList<String> arraylist=new ArrayList<String>(); 
private SQLiteDatabase MYdatabase; 
ListView listView; 
Button back; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.display); 
    listView=(ListView)findViewById(R.id.listView1); 

     DatabaseHelper db=new DatabaseHelper(this); 
     MYdatabase=db.getWritableDatabase(); 
    try { 
     Cursor c=MYdatabase.rawQuery("SELECt * FROM Student", null); 
     if(c!=null) 
     { 
      if(c.moveToFirst()) 
      { 
       do { 
        String name=c.getString(c.getColumnIndex("NAME")); 
        String age=c.getString(c.getColumnIndex("AGE")); 
        String address=c.getString(c.getColumnIndex("ADDRESS")); 
        arraylist.add("Name :"+name+"\n"+"Age :"+age+"\n"+"Address :"+address+"\n"); 

       } while (c.moveToNext()); 
      } 
     } 

     c.close(); 
     c.deactivate(); 

    } catch (SQLiteException e) { 

     Log.d(getClass().getSimpleName(), "could not create"+"or open the database"); 

    } 
    finally 
    { 
     if(MYdatabase!=null) 
     { 
      MYdatabase.close(); 
     } 
    } 
    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arraylist)); 
} 
public void Back(View v) 
{ 
    Intent back=new Intent(DisplayActivity.this,DbExampleActivity.class); 
    startActivity(back); 
} 
} 

以下是活動類代碼。 認爲它會很容易理解

public class DbExampleActivity extends Activity implements OnClickListener{ 
Button submit,select,search; 
AlertDialog di; 
private SQLiteDatabase sqLiteDatabase; 
private SQLiteStatement sqLiteStatement; 
private String name,age,address; 
private static final String TABLE_NAME="Student"; 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    submit=(Button)findViewById(R.id.savebutton); 
    select=(Button)findViewById(R.id.detailsbutton); 
    search=(Button)findViewById(R.id.searchbutton); 
    DatabaseHelper db=new DatabaseHelper(this); 
    sqLiteDatabase=db.getWritableDatabase(); 
    sqLiteStatement=sqLiteDatabase.compileStatement("insert into "+TABLE_NAME+"(name,age,address)values(?,?,?)"); 
    submit.setOnClickListener(this); 
    select.setOnClickListener(this); 
    search.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 
    case R.id.detailsbutton: 
     Intent in=new Intent(getApplicationContext(),DisplayActivity.class); 
     startActivity(in); 

     break; 
    case R.id.savebutton: 
     name=((EditText)findViewById(R.id.editText1)).getText().toString().trim(); 
     age=((EditText)findViewById(R.id.editText2)).getText().toString().trim(); 
     address=((EditText)findViewById(R.id.editText3)).getText().toString().trim(); 

     sqLiteStatement.bindString(1, name); 
     sqLiteStatement.bindString(2, age); 
     sqLiteStatement.bindString(3, address); 
     sqLiteStatement.executeInsert(); 

     Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_LONG).show(); 
     break; 
    case R.id.searchbutton: 
     Intent search=new Intent(getApplicationContext(),DisplayActivity.class); 
     startActivity(search); 
    default: 
     break; 
    } 

} 
} 

什麼代碼將被添加到該允許搜索使用關鍵字

+1

你可以使用這樣的查詢'「SELECT * FROM學生WHERE ID =」 + ID' – 2619

+0

謝謝......但這些關鍵字鍵入edittext..i硝基甲苯知道如何鏈接... dnt介意我是新手機android –

回答

2
EditText1=(EditText)findViewById(R.id.editText1); 
    EditText1.addTextChangedListener(new TextWatcher() { 

     @Override 
       public void onTextChanged(CharSequence s, int start, int before,int count) 
    { 
        populateListView(s.toString()); 

        } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 
    }); 

       public void populateListview(String s){ 

      DatabaseHelper db = new DatabaseHelper(this); 
      MYdatabase = db.getWritableDatabase(); 

    try { 
    Cursor c=MYdatabase.rawQuery(" SELECT * FROM Student WHERE City ID '"+s+%"'", null); 
    if(c!=null) 
    { 
     if(c.moveToFirst()) 
     { 
      do { 
       String name=c.getString(c.getColumnIndex("NAME")); 
       String age=c.getString(c.getColumnIndex("AGE")); 
       String address=c.getString(c.getColumnIndex("ADDRESS")); 
       arraylist.add("Name :"+name+"\n"+"Age :"+age+"\n"+"Address :"+address+"\n"); 

      } while (c.moveToNext()); 
     } 
    } 

    } 

    c.close(); 
    db.close(); 
    db=null; 

    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arraylist)); 
} 
      } 
+0

:omg ....非常感謝你...因爲早上我一直在開我的頭怎麼做....我們怎麼能發送這個數據庫一些其他的服務器 –

+0

對不起,我明白了。 ..請不要忘記點擊接受按鈕,如果帖子是有幫助的 – Vivekanand

+0

:我的先生只是要求我通過提供服務器的IP地址將數據庫文件發送到服務器 –

0

數據庫從我的理解,我認爲你可以使用LIKE子句

例如:

" SELECT * FROM Student WHERE City LIKE '"+ keyWord +%'" 
+0

我試過了,但我沒有工作......它顯示平淡無奇的窗口,沒有任何數據.....有沒有其他方法可以這麼做 –

0

替換光標C = MYdatabase.rawQuery( 「SELECT * FROM學生」,NULL); with Cursor c = MYdatabase.rawQuery(「SELECT * FROM Student WHERE id =」+ ID,null);

使用editText.getText()。toString()從EditText檢索ID;並在查詢數據庫時將其作爲參數傳遞。

+0

:謝謝......如何傳遞edittext作爲參數在數據庫中查詢 –

+0

通過名稱ID創建一個String對象,該對象將保存editText值。並將其傳遞給此Cursor c = MYdatabase.rawQuery(「SELECT * FROM Student WHERE id =」+ ID,null); – Vivekanand

+0

我使用了給出的代碼,但是...我無法得到正確的結果。 –

相關問題