2

您好,我知道这是一个基本问题,但我很困惑该怎么做。我已经硬编码了我的第一个应用程序。以下是我尝试过的:

 String Search  = SearchAuto.getText().toString();

 if(SearchAuto.getText().toString().equals("Chowchow") || 
                    SearchAuto.getText().toString().equals("Chihuahua")|| 
                    SearchAuto.getText().toString().equals("German Shephered")|| 
                    SearchAuto.getText().toString().equals("Beagle")|| 
                    SearchAuto.getText().toString().equals("Shih tzu")|| 
                    SearchAuto.getText().toString().equals("Siberian Husky")|| 
                    SearchAuto.getText().toString().equals("Pug")|| 
                    SearchAuto.getText().toString().equals("Poodle")|| 
                    SearchAuto.getText().toString().equals("Pomeranian")|| 
                    SearchAuto.getText().toString().equals("Labrador Retriever")){
                Intent myIntent = new Intent(Search.this, Dogs.class);
                myIntent.putExtra(strDog, SearchAuto.getText().toString().toLowerCase(Locale.ENGLISH));
                startActivity(myIntent);
                SearchAuto.setText("");
            }
            else if(SearchAuto.getText().toString().equals("American Shorthair") || 
                    SearchAuto.getText().toString().equals("Bengal")|| 
                    SearchAuto.getText().toString().equals("Himalayan")|| 
                    SearchAuto.getText().toString().equals("Maine Coon")|| 
                    SearchAuto.getText().toString().equals("Manx")|| 
                    SearchAuto.getText().toString().equals("Persian")|| 
                    SearchAuto.getText().toString().equals("Ragdoll")|| 
                    SearchAuto.getText().toString().equals("Russian Blue")|| 
                    SearchAuto.getText().toString().equals("Siamese")|| 
                    SearchAuto.getText().toString().equals("Sphynx")){
                Intent myIntent = new Intent(Search.this, Cats.class);
                myIntent.putExtra(strCat, SearchAuto.getText().toString().toLowerCase(Locale.ENGLISH));
                startActivity(myIntent);
                SearchAuto.setText("");
            }

但我希望我的数据是动态的,所以我创建了一个数据库来存储我的数据。

现在我的问题是如何创建查询/更改上述代码以满足我的需求?

这是我迄今为止尝试过的:

DBHelper.class

  public Cursor fetchbByAnimalType(String animalType, String breed) throws SQLException {

      Cursor mCursor = null;
      if (animalType == null  ||  animalType.length () == 0)  {
       mCursor = myDataBase.query(DB_TABLE, new String[] { KEY_ID, KEY_BREED },
         null, null, null, null, null);

      }
      else {
          mCursor = myDataBase.query(DB_TABLE, new String[] { KEY_ID, KEY_BREED },
         KEY_ANIMALTYPE + " like '%" + animalType + "%' AND " + KEY_BREED + breed, null, null, null, null);
      }

      if (mCursor != null) {
       mCursor.moveToFirst();
      } 

      return mCursor;
}

public Cursor fetchbBreedByName(CharSequence breed) throws SQLException {

      Cursor mCursor = null;
      if (breed == null  ||  breed.length () == 0)  {
       mCursor = myDataBase.query(DB_TABLE, new String[] { KEY_ID, KEY_ANIMALTYPE, KEY_DESCRIPTION, 
               KEY_DIET, KEY_SHELTER, KEY_HYGIENE, KEY_MEDICATION, KEY_BREED },
         null, null, null, null, null);

      }
      else {
          mCursor = myDataBase.query(DB_TABLE, new String[] { KEY_ID, KEY_ANIMALTYPE, KEY_DESCRIPTION, 
               KEY_DIET, KEY_SHELTER, KEY_HYGIENE, KEY_MEDICATION, KEY_BREED },
         KEY_BREED + " like '%" + breed + "%'", null, null, null, null);
      }

      if (mCursor != null) {
       mCursor.moveToFirst();
      } 

      return mCursor;
}

搜索类

  public void onClick(View arg0) {
            String Search  = SearchAuto.getText().toString();

            Cursor cursor = dbHelper.fetchbBreedByName(Search);

            String strID, strBreed, strAnimalType;

            strID = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
            strBreed = cursor.getString(cursor.getColumnIndexOrThrow("breed"));
            strAnimalType = cursor.getString(cursor.getColumnIndexOrThrow("animaltype"));

            Log.d("Search", "Search for " + strBreed + " ID is " + strID);


            if(Search.equals(strBreed) || strAnimaltype){ <<---- I don't know what condition I have to put in here!!!!!! I need help
                    Intent myIntent = new Intent(Search.this, Dogs.class);
                    myIntent.putExtra(strDog, Search);
                    startActivity(myIntent); 
            }

   }

我在我遇到问题的行上加上了一个指示符。虽然我没有运行它,但我知道它会崩溃。任何想法如何实现我想要的?我真的需要这方面的帮助。谢谢。

4

2 回答 2

1

What i would do:

create two ArrayLists, say, DogsList and CatsList, and add the respective objects (to be specific, String) into it.

String[] DogsArray = new String {"Chihuahua", "", "", ...};
ArrayList<String> DogsList = new ArrayList<String>(Arrays.asList(DogsArray));

String[] CatsArray = new String {"American Shorthair", "", "", ...};
ArrayList<String> CatsList = new ArrayList<String>(Arrays.asList(CatsArray));

Then,

if(DogsList.contains(SearchAuto.getText().toString()))
{
Intent myIntent = new Intent(Search.this, Dogs.class);
                myIntent.putExtra(strDog, SearchAuto.getText().toString().toLowerCase(Locale.ENGLISH));
                startActivity(myIntent);
}
else if(CatsList.contains(SearchAuto.getText().toString()))
{
Intent myIntent = new Intent(Search.this, Cats.class);
                myIntent.putExtra(strCat, SearchAuto.getText().toString().toLowerCase(Locale.ENGLISH));
                startActivity(myIntent);
}

Hope this thing helps you.

于 2013-10-05T14:31:11.253 回答
0

假设 SQLite 正在工作,我想既然你已经有了strBreedand strAnimalType,你可以这样说

if(strAnimalType.equalsIgnoreCase("cat") { /* or the value to indicate cat */
    Intent myIntent = new Intent(Search.this, Cats.class);
    myIntent.putExtra(strCat, strBreed);
    startActivity(myIntent);
} else if(strAnimalType.equalsIgnoreCase("dog") { /* or the value to indicate dog */
    Intent myIntent = new Intent(Search.this, Dogs.class);
    myIntent.putExtra(strDog, strBreed);
    startActivity(myIntent);
}
于 2013-10-05T14:08:34.847 回答