简体   繁体   English

菜单按钮工作,软按钮不起作用。 基本相同的代码

[英]Menu buttons work, Soft buttons not working. Same Code basically

Title says it all, Im new to SQL and trying to change the selection the user makes but placing buttons in the screen and not use the MENU button. 标题说明了一切,我是SQL的新手并尝试更改用户所做的选择,但是在屏幕上放置按钮而不使用MENU按钮。 Seems like the buttons aren't instantiated but the code looks right to me...what am i missing?? 似乎按钮没有实例化,但代码看起来对我来说...我错过了什么?

package com.example.worldcountriesbooks;

import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ViewCountry extends Activity implements OnClickListener{


   private long rowID;
   private TextView nameTv;
   private TextView capTv;
   private TextView codeTv; 
   private TextView newEt;




   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.view_country);
      Button a = (Button)findViewById(R.id.editbutton);
       Button b = (Button)findViewById(R.id.deletebutton);
      a.setOnClickListener(this);
      b.setOnClickListener(this); //Set them up right here...

      setUpViews();
      Bundle extras = getIntent().getExtras();
      rowID = extras.getLong(CountryList.ROW_ID); 
   }

   private void setUpViews() {
       nameTv = (TextView) findViewById(R.id.nameText);
       capTv = (TextView) findViewById(R.id.capText);
       codeTv = (TextView) findViewById(R.id.codeText);
       newEt = (TextView)findViewById(R.id.newText);

   }

   @Override
   protected void onResume()
   {
      super.onResume();
      new LoadContacts().execute(rowID);
   } 

   private class LoadContacts extends AsyncTask<Long, Object, Cursor> 
   {
      DatabaseConnector dbConnector = new DatabaseConnector(ViewCountry.this);

      @Override
      protected Cursor doInBackground(Long... params)
      {
         dbConnector.open();
         return dbConnector.getOneContact(params[0]);
      } 

      @Override
      protected void onPostExecute(Cursor result)
      {
         super.onPostExecute(result);

         result.moveToFirst();
         // get the column index for each data item
         int nameIndex = result.getColumnIndex("name");
         int capIndex = result.getColumnIndex("cap");
         int codeIndex = result.getColumnIndex("code");
         int newIndex = result.getColumnIndex("newb");


         nameTv.setText(result.getString(nameIndex));
         capTv.setText(result.getString(capIndex));
         codeTv.setText(result.getString(codeIndex));
         newEt.setText(result.getString(newIndex));


         result.close();
         dbConnector.close();
      }
   } 


   @Override
   public boolean onCreateOptionsMenu(Menu menu) 
   {
      super.onCreateOptionsMenu(menu);
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.view_country_menu, menu);
      return true;
   }


   @Override
   public boolean onOptionsItemSelected(MenuItem item) 
   {
      switch (item.getItemId())
      {
         case R.id.editItem:
            Intent addEditContact =
               new Intent(this, AddEditCountry.class);

            addEditContact.putExtra(CountryList.ROW_ID, rowID);
            addEditContact.putExtra("name", nameTv.getText());
            addEditContact.putExtra("cap", capTv.getText());
            addEditContact.putExtra("code", codeTv.getText());
            addEditContact.putExtra("newb", newEt.getText());

            startActivity(addEditContact); 
            return true;

         case R.id.deleteItem:
            deleteContact();
            return true;

         default:
            return super.onOptionsItemSelected(item);
      } 
   }
   private void deleteContact()
   {

      AlertDialog.Builder alert = new AlertDialog.Builder(ViewCountry.this);

      alert.setTitle(R.string.confirmTitle); 
      alert.setMessage(R.string.confirmMessage); 

      alert.setPositiveButton(R.string.delete_btn,
         new DialogInterface.OnClickListener()
         {
            public void onClick(DialogInterface dialog, int button)
            {
               final DatabaseConnector dbConnector = 
                  new DatabaseConnector(ViewCountry.this);

               AsyncTask<Long, Object, Object> deleteTask =
                  new AsyncTask<Long, Object, Object>()
                  {
                     @Override
                     protected Object doInBackground(Long... params)
                     {
                        dbConnector.deleteContact(params[0]); 
                        return null;
                     } 

                     @Override
                     protected void onPostExecute(Object result)
                     {
                        finish(); 
                     }
                  };

               deleteTask.execute(new Long[] { rowID });               
            }
         }
      );

      alert.setNegativeButton(R.string.cancel_btn, null).show();
   }

public void onClick(View arg0) {
     switch (arg0.getId())
      {
         case R.id.editItem:
            Intent addEditContact =
               new Intent(this, AddEditCountry.class);

            addEditContact.putExtra(CountryList.ROW_ID, rowID);
            addEditContact.putExtra("name", nameTv.getText());
            addEditContact.putExtra("cap", capTv.getText());
            addEditContact.putExtra("code", codeTv.getText());
            addEditContact.putExtra("newb", newEt.getText());

                startActivity(addEditContact); 
                break;

             case R.id.deleteItem:
                deleteContact();
                break;//finish them up here and they do nothing...

    }
    }
}

Now the menu buttons work great so not sure whats up...Thanks for looking 现在菜单按钮效果很好,所以不确定是什么...感谢您的关注

The menu buttons work great because the switch statement for it is proper. 菜单按钮工作得很好,因为它的switch语句是正确的。

Your onClick is not working properly however. 但是,你的onClick无法正常工作。 This is because The cases are for different ids than what the buttons provide. 这是因为这些案件都是不同的 ID不是什么按钮提供。 You want R.id.editbutton and R.id.deletebutton instead. 你想要R.id.editbuttonR.id.deletebutton

Your method should look like: 您的方法应如下所示:

public void onClick(View arg0) {
     switch (arg0.getId())
      {
         case R.id.editbutton: //updated
            Intent addEditContact =
               new Intent(this, AddEditCountry.class);

            addEditContact.putExtra(CountryList.ROW_ID, rowID);
            addEditContact.putExtra("name", nameTv.getText());
            addEditContact.putExtra("cap", capTv.getText());
            addEditContact.putExtra("code", codeTv.getText());
            addEditContact.putExtra("newb", newEt.getText());

                startActivity(addEditContact); 
                break;

             case R.id.deletebutton: //updated
                deleteContact();
                break;

    }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM