2012-09-12 6 views
0

사전 응용 프로그램 용 코드가 있습니다. 사용자가 검색 상자에서 목록 항목을 클릭하면 두 개의 버튼이있는 대화 상자가 표시됩니다.하지만 에뮬레이터에서 테스트 한 결과 나타나지 않습니다. 사전 응용 프로그램을 작성하기 위해 다음 코드를 사용했습니다. 이것은 MainActivity calss입니다.대화 상자가 나타나지 않습니다

MyCode :

public class Shower extends Activity { 
    private TextView mTextView; 
     private ListView mListView; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_shower); 

     mTextView = (TextView) findViewById(R.id.text); 
     mListView = (ListView) findViewById(R.id.list); 

     handleIntent(getIntent()); 
    } 


    @Override 
    protected void onNewIntent(Intent intent) { 
     // Because this activity has set launchMode="singleTop", the system calls this method 
     // to deliver the intent if this actvity is currently the foreground activity when 
     // invoked again (when the user executes a search from this activity, we don't create 
     // a new instance of this activity, so the system delivers the search intent here) 
     handleIntent(intent); 
    } 

    private void handleIntent(Intent intent) { 
     if (Intent.ACTION_VIEW.equals(intent.getAction())) { 
      // handles a click on a search suggestion; launches activity to show word 
      Intent wordIntent = new Intent(this, WordActivity.class); 
      wordIntent.setData(intent.getData()); 
      startActivity(wordIntent); 
      finish(); 
     } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
      // handles a search query 
      String query = intent.getStringExtra(SearchManager.QUERY); 
      showResults(query); 
     } 
    } 

    /** 
    * Searches the dictionary and displays results for the given query. 
    * @param query The search query 
    */ 
    private void showResults(String query) { 

     Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null, 
           new String[] {query}, null); 

     if (cursor == null) { 
      // There are no results 
      mTextView.setText(getString(R.string.no_results, new Object[] {query})); 
     } else { 
      // Display the number of results 
      int count = cursor.getCount(); 
      String countString = getResources().getQuantityString(R.plurals.search_results, 
            count, new Object[] {count, query}); 
      mTextView.setText(countString); 

      // Specify the columns we want to display in the result 
      String[] from = new String[] { DictionaryDatabase.KEY_WORD, 
              DictionaryDatabase.KEY_DEFINITION }; 

      // Specify the corresponding layout elements where we want the columns to go 
      int[] to = new int[] { R.id.word, 
            R.id.definition }; 

      // Create a simple cursor adapter for the definitions and apply them to the ListView 
      SimpleCursorAdapter words = new SimpleCursorAdapter(this, 
              R.layout.result, cursor, from, to); 
      mListView.setAdapter(words); 

      // Define the on-click listener for the list items 
      mListView.setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        // Build the Intent used to open WordActivity with a specific word Uri 
        // Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class); 
        //Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI, 
                // String.valueOf(id)); 
        // wordIntent.setData(data); 
        //startActivity(wordIntent); 



      String title="Dictionary"; 
      String message="definition"; 
      AlertDialog.Builder dialog = new AlertDialog.Builder(Shower.this); 
       dialog.setTitle(title); 
       dialog.setMessage(message);    


       dialog.setCancelable(false); 
       dialog.setPositiveButton("Dictionary", 
        new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 


         Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class); 
         Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI, 
                 String.valueOf(id)); 
         wordIntent.setData(data); 
         startActivity(wordIntent); 

        } 
       }) ; 

       dialog.setNegativeButton("definition", 
        new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.dismiss(); 
        } 
       }); 
       dialog.show(); 
       } 


       }); 
     } 
     } 




    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     //getMenuInflater().inflate(R.menu.activity_shower, menu); 
     // return true; 

     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.activity_shower, menu); 

     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); 
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
     searchView.setIconifiedByDefault(false); 

     return true; 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.search: 
       onSearchRequested(); 
       return true; 
      default: 
       return false; 
     } 
    } 
} 

답변

2

이 시도 참조 :

  AlertDialog dialog = new AlertDialog.Builder(YourClassName.context).create(); 
    dialog.setTitle(""); 
    dialog.setMessage(""); 

    dialog.setIcon(""); 
    dialog.setButton(DialogInterface.BUTTON_POSITIVE,"Open whatever", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int which) { 
           // TODO Auto-generated method stub 
Intent intent = new Intent(YourClassName.context, OtherClassName.class); 
     //your code .... 
       } 
       }); 

      dialog.show(); 


    return true; 

     } 
3

당신은에 AlertDialog 빌더에 AlertDialog 생성해야합니다 .. 코드에서

AlertDialog alert=dialog.create(); 

alert.show(); 

. 이 link

+0

나는 내 다른 응용 프로그램에 대한 코드 위에 사용. 하지만 내 다른 응용 프로그램은 완벽하게 작동하고 있습니다. 그러나이 응용 프로그램이 작동하지 않습니다. – Ram

+0

위 코드를 지금 사용하고 확인하십시오. – Shalini

+0

내 코드 내부에서 작동하지 않습니다. – Ram

0
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setMessage("Are you sure you want to exit?") 
     .setCancelable(false) 
     .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       MyActivity.this.finish(); 
      } 
     }) 
     .setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }); 
AlertDialog alert = builder.create(); 
alert.show(); 
+1

그것은 내 코드 내에서 작동하지 않습니다 – Ram

관련 문제