2012-02-15 5 views
1

검색된 매개 변수가 유효하지 않은 경우 축배 메시지를 사용하고 싶습니다.토스트 메시지 사용에 대한 설명

**Database.java** 

    package samples.employeephone; 

    import android.content.ContentValues; 
    import android.content.Context; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteOpenHelper; 

    public class Database extends SQLiteOpenHelper { 

     public static final String DATABASE_NAME = "employee_directory"; 

     public Database(Context context) { 
      super(context, DATABASE_NAME, null, 1); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      String sql = "CREATE TABLE IF NOT EXISTS employee (" + 
          "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
          "firstName TEXT, " + 
          "lastName TEXT, " + 
          "title TEXT, " + 
          "officePhone TEXT, " + 
          "cellPhone TEXT, " + 
          "email TEXT, " + 
          "managerId INTEGER)"; 
      db.execSQL(sql); 

      ContentValues values = new ContentValues(); 

      values.put("firstName", "xyz"); 
      values.put("lastName", "abc"); 
      values.put("title", "PL"); 
      values.put("officePhone", "123456"); 
      values.put("cellPhone", "123456"); 
      values.put("email", "[email protected]"); 
      db.insert("employee", "lastName", values); 

      values.put("firstName", "eeee"); 
      values.put("lastName", "aaaa"); 
      values.put("title", "PM"); 
      values.put("officePhone", "234576"); 
      values.put("cellPhone", "2344556"); 
      values.put("email", "[email protected]"); 
      values.put("managerId", "1"); 
      db.insert("employee", "lastName", values); 

      values.put("firstName", "ash"); 
      values.put("lastName", "l"); 
      values.put("title", "POC"); 
      values.put("officePhone", "454454"); 
      values.put("cellPhone", "5454646"); 
      values.put("email", "[email protected]"); 
      values.put("managerId", "1"); 
      db.insert("employee", "lastName", values); 

      values.put("firstName", "ddd"); 
      values.put("lastName", "yxv"); 
      values.put("title", "AS"); 
      values.put("officePhone", "654545"); 
      values.put("cellPhone", "5454545"); 
      values.put("email", "[email protected]"); 
      values.put("managerId", "2"); 
      db.insert("employee", "lastName", values); 

      values.put("firstName", "din"); 
      values.put("lastName", "sss"); 
      values.put("title", "ers"); 
      values.put("officePhone", "25545"); 
      values.put("cellPhone", "65454"); 
      values.put("email", "[email protected]"); 
      values.put("managerId", "2"); 
      db.insert("employee", "lastName", values); 

      values.put("firstName", "sis"); 
      values.put("lastName", "and"); 
      values.put("title", "VP"); 
      values.put("officePhone", "878788"); 
      values.put("cellPhone", "88787"); 
      values.put("email", "[email protected]"); 
      values.put("managerId", "2"); 
      db.insert("employee", "lastName", values); 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      db.execSQL("DROP TABLE IF EXISTS employees"); 
      onCreate(db); 
     } 

    } 

**PhonebookActivity.java** 

package samples.employeephone; 

//import samples.employeedirectory.R; 
import android.app.Activity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

public class PhonebookActivity extends Activity { 
    protected EditText searchText; 
    protected SQLiteDatabase db; 
    protected Cursor cursor; 
    protected ListAdapter adapter; 
    protected ListView employeeList; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     db = (new Database(this)).getWritableDatabase(); 
     searchText = (EditText) findViewById (R.id.searchText); 
     employeeList = (ListView) findViewById (R.id.list); 
    } 

    public void search(View view) { 
     // || is the concatenation operation in SQLite 
     cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?", 
         new String[]{"%" + searchText.getText().toString() + "%"}); 
     adapter = new SimpleCursorAdapter(
       this, 
       R.layout.employee_list_item, 
       cursor, 
       new String[] {"firstName", "lastName", "title"}, 
       new int[] {R.id.firstName, R.id.lastName, R.id.title}); 
     employeeList.setAdapter(adapter); 
    } 

} 

and my xml files are, 

**main.xml** 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/searchText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

     <Button 
      android:id="@+id/searchButton" 
      android:text="Search" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="search"/> 

    </LinearLayout> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 

</LinearLayout> 

**employee_list_item.xml** 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="8px"> 

    <TextView 
     android:id="@+id/firstName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:id="@+id/lastName" 
     android:layout_marginLeft="6px" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/firstName"/> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/firstName"/> 

</RelativeLayout> 

이제 입력 한 이름이 유효하지 않은 경우 내 검색하는 동안, 나는 그것을 보여주고 싶은, 내가 "잘못된 검색"여기

내 샘플 코드가 같은 토스트 메시지를 보여주고 싶은, 말 토스트 메시지, 여기 어떻게 활용할 수 있습니까?

저는이 플랫폼을 처음 사용하기 때문에 친절한 도움을받을 수 있습니다.

+2

그래서? 코드에서 질문과 관련된 내용을 찾을 수 없습니다. 토스트는 어디에서 만들어야합니까? – Egor

+0

안녕하세요, values.put ("firstName", "xyz"); 만약 내가 (안녕하세요, 안녕하세요 말할 수없는 메시지를 통해 표시하는 것입니다 안녕하세요 말할 수 ... .... (firstName == "xyz") 그것은 유효한 정보를 표시합니다 다른 사람은 토스트 메시지를 표시해야합니다 "무효 검색" – mahi

+0

물론 if-else 절을 ​​사용해야합니다. – Egor

답변

0

내가 제대로 귀하의 요청을 이해한다면, 당신은 당신의 DB 쿼리 후이 뭔가를 원하는 : 커서 카운트가 0 인 경우 검색에

if (cursor.getCount()==0) { 
    Context context = getApplicationContext(); 
    CharSequence text = "Invalid search"; 
    int duration = Toast.LENGTH_SHORT; 

    Toast toast = Toast.makeText(context, text, duration); 
    toast.show(); 
} else 
{ 
    ... update your view .... 
} 
1

(보기보기) 방법은, 당신의 어댑터를 초기화하기 전에 확인합니다. 그렇다면 토스트를 수행하십시오.

Toast.makeText(getContext(), "Invalid search", Toast.LENGTH_SHORT).show();