2012年3月10日 星期六

[TQC+ Android] 2-10 簡單記事 Use SimpleCursorAdapter , ListView , SQLiteDatabase





設計一個簡單記事本 , 按下Add時將EditText裡的值寫到資料庫中(MY_DB) , 新增成功後將值顯示在下方的 ListView , 點選到ListView任一筆的資料時將值帶入EditText並可Update或是Delete , 程式碼如下。




package COM.TQC.GDD02;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ListView;


public class GDD02 extends Activity
{
 private static final String DBNAME = "MY_DB";
 private static final String TABLENAME = "MY_TABLE";
 private static final String FIELD01_NAME = "_id";
 private static final String FIELD02_NAME = "_text1";
 private SQLiteDatabase dataBase;
 private android.database.Cursor cursor;
 private int _id = -1;
 private EditText EditText01;
 private Button Button01;
 private Button Button02;
 private Button Button03;
 private ListView ListView01;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        EditText01 = (EditText) findViewById(R.id.EditText01);
        Button01 = (Button) findViewById(R.id.BtnAdd);
        Button02 = (Button) findViewById(R.id.BtnUpdate);
        Button03 = (Button) findViewById(R.id.BtnDelete);     
        ListView01 = (ListView) findViewById(R.id.ListView01); 
        
        Button02.setEnabled(false);
        Button03.setEnabled(false);
        
        String CREATE_SQL = "create table if not exists "+TABLENAME+" ("+FIELD01_NAME+" INTEGER PRIMARY KEY, "+FIELD02_NAME+" TEXT)";
        dataBase = this.openOrCreateDatabase(DBNAME , MODE_PRIVATE , null);
        dataBase.execSQL(CREATE_SQL);
     updateAdapter();
     //Update ListView
        
        Button01.setOnClickListener(new View.OnClickListener()
        {
   @Override
   public void onClick(View arg0)
   {
    // TODO Auto-generated method stub
    if(!EditText01.getText().equals(""))
    {
     insert(EditText01.getText().toString());
    }
  }});
        
        Button02.setOnClickListener(new View.OnClickListener()
        {
   @Override
   public void onClick(View arg0)
   {
    // TODO Auto-generated method stub
    if(_id!=-1 && !EditText01.getText().equals(""))
    {
     update(_id, EditText01.getText().toString());
     Button02.setEnabled(false);
     Button03.setEnabled(false);
    }
    
  }});
        
        Button03.setOnClickListener(new View.OnClickListener()
        {
   @Override
   public void onClick(View arg0)
   {
    // TODO Auto-generated method stub
    if(_id!=-1)
    {
     delete(_id);
     Button02.setEnabled(false);
     Button03.setEnabled(false);
    }    
   }});
        
        ListView01.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener()
        {
   @Override
   public void onItemClick(android.widget.AdapterView arg0,
     View arg1, int arg2, long arg3)
   {
    // TODO Auto-generated method stub
    
    _id = (int) arg3;
    //arg3 is Primary Key, not arg2
    EditText01.setText(((CheckedTextView)arg1.findViewById(R.id.CheckedTextView01)).getText());
    Button02.setEnabled(true);
          Button03.setEnabled(true);
          Log.d("Test", "The position of the view in the adapter arg2:"+arg2);
          Log.d("Test", "The row id of the item that was clicked arg3:"+arg3);
   }});
    }
    
    private void insert(String text)
    {
     dataBase.execSQL("INSERT INTO "+TABLENAME+" ("+FIELD02_NAME+") "+"values ('"+text+"')");        
     updateAdapter();
     EditText01.setText("");
    }
    
    private void update(int id,String text)
    {
     dataBase.execSQL("UPDATE "+TABLENAME+" SET "+FIELD02_NAME+"='"+text+"' WHERE "+FIELD01_NAME+"="+id);        
     updateAdapter();
     EditText01.setText("");        
    }
    
    private void delete(int id)
    {
     dataBase.execSQL("DELETE FROM "+TABLENAME+" WHERE "+FIELD01_NAME+"="+id);        
     updateAdapter(); 
     Log.d("Test", "id:"+id);
     if(cursor.getCount()==0) _id=-1;
    }
    
    private void updateAdapter()
    {
     cursor = dataBase.rawQuery("SELECT * FROM "+TABLENAME, null);
     
     android.widget.SimpleCursorAdapter adapter = new android.widget.SimpleCursorAdapter(this, R.layout.list,
          cursor, new String[]
                { FIELD02_NAME }, new int[]
                { R.id.CheckedTextView01 });
     //R.layout.list 代表每一個Row view 的xml定義檔
     ListView01.setAdapter(adapter);
    }

 @Override
 protected void onDestroy()
 {
  // TODO Auto-generated method stub
  cursor.close();
  dataBase.close();
  super.onDestroy();  
 }    
}





接下來是main.xml


   
  
  
 
 
  
  
  
 
 
 





還有ListView中Row View的定義檔 list.xml


  
  








P.S. 題目中所要求的Variable和Method皆會保留 , 也會根據題目所要求的流程去實作 , 縱使題目要求繞遠路....


1 則留言:

  1. 請問您可以幫忙開發android程式嗎 tpc2002@gmail.com

    回覆刪除

Google Analytics