0

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. 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

4

1 回答 1

2

菜单按钮很好用,因为它的 switch 语句是正确的。

但是,您的 onClick 无法正常工作。这是因为这些案例的ID 与按钮提供的 ID不同。你想要R.id.editbuttonR.id.deletebutton而不是。

您的方法应如下所示:

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;

    }
    }
}
于 2012-12-25T02:19:09.343 回答