2016-01-23 12 views
-2

我在製作配方應用程序。我通過配方的一部分搜索工作確定android:按配料搜索

public class MainActivity extends Activity { 
    protected ListView lv; 
    protected ListAdapter adapter; 
    SQLiteDatabase db; 
    Cursor cursor; 
    EditText et_db; 

    @SuppressWarnings("deprecation") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     db = (new DB_Recipe(this)).getWritableDatabase(); 
     lv = (ListView) findViewById(R.id.lv); 
     et_db = (EditText) findViewById(R.id.et); 

     try { 
      cursor = db.rawQuery("SELECT * FROM recipe ORDER BY name ASC", null); 
      adapter = new SimpleCursorAdapter(this, R.layout.isi_lv, cursor, 
        new String[] { "name", "ingredients", "img" }, new int[] { 
        R.id.tv_name, R.id.tvIngredients, R.id.imV }); 
      lv.setAdapter(adapter); 
      lv.setTextFilterEnabled(true); 
      lv.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View v, 
            int position, long id) { 
        detail(position); 

       } 
      }); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

    @SuppressWarnings("deprecation") 
    public void search_db(View v) { 
     String edit_db = et_db.getText().toString(); 
     if (!edit_db.equals("")) { 
      try { 
       cursor = db.rawQuery("SELECT * FROM recipe WHERE name LIKE ?", 
         new String[] { "%" + edit_db + "%" }); 
       adapter = new SimpleCursorAdapter(
         this, 
         R.layout.isi_lv, 
         cursor, 
         new String[] { "name", "ingredients", "img" }, 
        new  int[] { R.id.tv_name, R.id.tvIngredients, R.id.imV });  
       if (adapter.getCount() == 0) { 
        Toast.makeText(
          this, 
          "Recipe not found " + edit_db 
            + "", Toast.LENGTH_SHORT).show(); 
      } else {  
        lv.setAdapter(adapter); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } else { 
      try { 
       cursor = db.rawQuery("SELECT * FROM recipe ORDER BY name ASC", 
         null); 
       adapter = new SimpleCursorAdapter(
         this, 
         R.layout.isi_lv, 
         cursor, 
         new String[] { "name", "ingredients", "img" }, 
         new int[] { R.id.tv_name, R.id.tvIngredients, R.id.imV }); 
       lv.setAdapter(adapter); 
       lv.setTextFilterEnabled(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    public void detail(int position) { 
     int im = 0; 
     String _id = ""; 
     String name = ""; 
     String ingredients = ""; 
     String preparation = ""; 
     if (cursor.moveToFirst()) { 
      cursor.moveToPosition(position); 
      im = cursor.getInt(cursor.getColumnIndex("img")); 
      name = cursor.getString(cursor.getColumnIndex("name")); 
      ingredients = cursor.getString(cursor.getColumnIndex("ingredients")); 
      preparation = cursor.getString(cursor.getColumnIndex("preparation")); 
     } 

     Intent iIntent = new Intent(this, DB_Parse.class); 
     iIntent.putExtra("dataIM", im); 
     iIntent.putExtra("dataName", name); 
     iIntent.putExtra("dataIngredients", ingredients); 
     iIntent.putExtra("dataPreparation", preparation); 
     setResult(RESULT_OK, iIntent); 
     startActivityForResult(iIntent, 99); 
    } 

} 

反正是有,我可以改變周圍的這個,這樣就可以通過原料替代配方搜索或如何,我會去這樣做

回答

0

如果你想只是配料進行搜索,你按食譜名稱進行搜索,你可以改變你的public void search_db(View v)查詢:

cursor = db.rawQuery("SELECT * FROM recipe WHERE name LIKE ?", 
new String[] { "%" + edit_db + "%" }); 

到:

cursor = db.rawQuery("SELECT * FROM recipe WHERE ingredients LIKE ?", 
new String[] { "%" + edit_db + "%" });