TQC-204 SQList與Menu

TQC-204 SQList與Menu

這是TQC的204題,

程式碼為自行撰寫,

僅供參考。

 

主要是練習SQList,

不難,但要上手需要一點時間寫寫看,

如果沒有把握,可以先進command模式,

用命令先下,

確認資料庫、資料表、資料是否都存在,

再寫程式。

 


package COM.TQC.GDD02;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class GDD02 extends Activity
{
  private mySQLiteOpenHelper gaaSQLiteOpenHelper;
  private Cursor myCursor;
  private ListView myListView;
  private EditText myEditText;
  private int _id;
  protected final static int MENU_ADD = Menu.FIRST;
  protected final static int MENU_EDIT = Menu.FIRST + 1;
  protected final static int MENU_DELETE = Menu.FIRST + 2;
  


  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    findview();
    setlisenter();

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
    super.onCreateOptionsMenu(menu);
    menu.add(0, MENU_ADD, 0, "MENU_ADD");
    menu.add(0, MENU_EDIT, 0, "MENU_EDIT");
    menu.add(0, MENU_DELETE, 0, "MENU_DELETE");

    return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    super.onOptionsItemSelected(item);
    switch (item.getItemId())
    {
      case MENU_ADD:
        this.addTodo();
        break;
      case MENU_EDIT:
        this.editTodo();
        break;
      case MENU_DELETE:
        this.deleteTodo();
        break;
    }
    return true;
  }

  
  
  public void findview()
  {
	  	myListView = (ListView) this.findViewById(R.id.myListView);
	    myEditText = (EditText) this.findViewById(R.id.myEditText);
  }
  
  public void setlisenter()
  {
	  	gaaSQLiteOpenHelper = new mySQLiteOpenHelper(this);    
	    myCursor = gaaSQLiteOpenHelper.select();    
	    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list,
	        myCursor, new String[]
	        { gaaSQLiteOpenHelper.FIELD_TEXT }, new int[]
	        { R.id.listTextView1 });
	    myListView.setAdapter(adapter);
	    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
	    {
	      public void onItemClick(AdapterView arg0, View arg1, int arg2,
	          long arg3)
	      {
	        
	        myCursor.moveToPosition(arg2);        
	        _id = myCursor.getInt(0);        
	        myEditText.setText(myCursor.getString(1));
	      }
	    });
	    myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
	        {
	          public void onItemSelected(AdapterView arg0, View arg1, int arg2,
	              long arg3)
	          {
	        	  myCursor.moveToPosition(arg2);              
	              _id = myCursor.getInt(0);              
	              myEditText.setText(myCursor.getString(1));
	          }
	          
	          public void onNothingSelected(AdapterView arg0)
	          {
	        	  myEditText.setText("");
	          }
	        });
  }
  
  private void addTodo()
  {
	  if (this.myEditText.getText().toString().equals(""))
	        return;
	      mySQLiteOpenHelper localmySQLiteOpenHelper = this.gaaSQLiteOpenHelper;
	      String str = this.myEditText.getText().toString();
	      long l = localmySQLiteOpenHelper.insert(str);
	      boolean bool = this.myCursor.requery();
	      this.myListView.invalidateViews();
	      this.myEditText.setText("");
	      this._id = 0;
  }

  private void editTodo()
  {
	  if (this.myEditText.getText().toString().equals(""))
	      return;
	    mySQLiteOpenHelper localmySQLiteOpenHelper = this.gaaSQLiteOpenHelper;
	    int i = this._id;
	    String str = this.myEditText.getText().toString();
	    localmySQLiteOpenHelper.update(i, str);
	    boolean bool = this.myCursor.requery();
	    this.myListView.invalidateViews();
	    this.myEditText.setText("");
	    this._id = 0;
  }

  private void deleteTodo()
  {
	  if (this._id == 0)
	      return;
	    mySQLiteOpenHelper localmySQLiteOpenHelper = this.gaaSQLiteOpenHelper;
	    int i = this._id;
	    localmySQLiteOpenHelper.delete(i);
	    boolean bool = this.myCursor.requery();
	    this.myListView.invalidateViews();
	    this.myEditText.setText("");
	    this._id = 0;
  }
}

 


package COM.TQC.GDD02;

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

public class mySQLiteOpenHelper extends SQLiteOpenHelper
{
	private static final String DATABASE_NAME = "todo_db";
	  private static final int DATABASE_VERSION = 1;
	  public static final String FIELD_TEXT = "todo_text";
	  public static final String FIELD_id = "_id";
	  private static final String TABLE_NAME = "todo_table";

	  public mySQLiteOpenHelper(Context paramContext)
	  {
	    super(paramContext, "todo_db", null, 1);
	  }

	  public void delete(int paramInt)
	  {
	    SQLiteDatabase localSQLiteDatabase = getWritableDatabase();
	    String[] arrayOfString = new String[1];
	    String str = Integer.toString(paramInt);
	    arrayOfString[0] = str;
	    int i = localSQLiteDatabase.delete("todo_table", "_id = ?", arrayOfString);
	  }

	  public long insert(String paramString)
	  {
	    SQLiteDatabase localSQLiteDatabase = getWritableDatabase();
	    ContentValues localContentValues = new ContentValues();
	    localContentValues.put("todo_text", paramString);
	    return localSQLiteDatabase.insert("todo_table", null, localContentValues);
	  }

	  public void onCreate(SQLiteDatabase paramSQLiteDatabase)
	  {
	    paramSQLiteDatabase.execSQL("CREATE TABLE todo_table (_id INTEGER primary key autoincrement,  todo_text text)");
	  }

	  public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
	  {
	    paramSQLiteDatabase.execSQL("DROP TABLE IF EXISTS todo_table");
	    onCreate(paramSQLiteDatabase);
	  }

	  public Cursor select()
	  {
	    SQLiteDatabase localSQLiteDatabase = getReadableDatabase();
	    String str1 = null;
	    String[] arrayOfString = null;
	    String str2 = null;
	    String str3 = null;
	    String str4 = null;
	    return localSQLiteDatabase.query("todo_table", null, str1, arrayOfString, str2, str3, str4);
	  }

	  public void update(int paramInt, String paramString)
	  {
	    SQLiteDatabase localSQLiteDatabase = getWritableDatabase();
	    String[] arrayOfString = new String[1];
	    String str = Integer.toString(paramInt);
	    arrayOfString[0] = str;	    
	    ContentValues localContentValues = new ContentValues();
	    localContentValues.put("todo_text", paramString);
	    int i = localSQLiteDatabase.update("todo_table", localContentValues, "_id = ?", arrayOfString);
	  }
	}

 

 

自我LV~