zoukankan      html  css  js  c++  java
  • 日记本开发


    虽然过程很曲折,但也还是跌跌撞撞写出来了

    第一次写,代码写的很乱七八糟,不要介意。。这个真的是在练练手,开始奋斗java4android


    1.控制listView的代码

    package com.my;
    
    import static com.my.MyOpenHelper.DB_NAME;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import android.R.color;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.AdapterView.OnItemLongClickListener;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
    
    
    public class Two extends Activity{
    	
    	private MyOpenHelper db;
    	private String tit;
    	private String dat;
    	private String con;
    	private String wea;
    	private int id;
    	
    	int i=0;
    	int  temp[] = new int [2000];
    	
    	public void onCreate(Bundle savedInstanceState){
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.index);
    		
    		ListView list=(ListView)findViewById(R.id.listView);
    		
    		//注册上下文
    		registerForContextMenu(list);
    		db = new MyOpenHelper(getApplicationContext(),DB_NAME);
    		//动态数组
    		ArrayList<HashMap<String,Object>> li=new ArrayList<HashMap<String,Object>>();
    		
    		Cursor cursor =db.query();
    		
    		while(cursor.moveToNext()){
    			HashMap<String,Object> item = new HashMap<String,Object>();
    			temp[i++] = cursor.getInt(cursor.getColumnIndex("d_id"));
    			item.put("d_title", cursor.getString(cursor.getColumnIndex("d_title")));
    			item.put("d_image", cursor.getString(cursor.getColumnIndex("d_image")));
    			item.put("d_data", cursor.getString(cursor.getColumnIndex("d_data")));
    			item.put("d_content", cursor.getString(cursor.getColumnIndex("d_content")));
    			li.add(item);
    		}
    		cursor.close();
    		
    		SimpleAdapter adatper=new SimpleAdapter(Two.this, li, R.layout.list_adapter, 
    				new String[]{"d_image","d_title","d_data"},
    				new int[]{R.id.item_image,R.id.item_title,R.id.item_data});
    		
    		list.setAdapter(adatper);
    		list.setCacheColorHint(color.transparent);
    		
    		list.setOnItemClickListener(new OnItemClickListener() {
    
    			@Override
    			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    					long arg3) {
    				//Toast.makeText(Two.this, "你点击了"+arg2+"行", 1000).show();
    				// TODO Auto-generated method stub
    				//获取选中的日记
    				HashMap<String,Object> item = (HashMap<String,Object>) arg0.getItemAtPosition(arg2);
    				Intent intent = new Intent(Two.this, Note_read.class);
    				
    				tit=String.valueOf(item.get("d_title"));
    				wea=String.valueOf(item.get("d_image"));
    				dat=String.valueOf(item.get("d_data"));
    				con=String.valueOf(item.get("d_content"));
    				
    				intent.putExtra("_ID", temp[arg2]);
    				intent.putExtra("TIT", tit);
    				intent.putExtra("DAT", dat);
    				intent.putExtra("WEA", wea);
    				intent.putExtra("CON", con);
    				startActivity(intent);
    				finish();
    			}
    			
    		});
    		
    		list.setOnItemLongClickListener(new OnItemLongClickListener() {
    
    			@Override
    			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
    					final int arg2, long arg3) {
    				// TODO Auto-generated method stub
    				new AlertDialog.Builder(Two.this)
    					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    					
    					@Override
    					public void onClick(DialogInterface dialog, int which) {
    						// TODO Auto-generated method stub
    						 
    						SQLiteDatabase mdb = db.getWritableDatabase();
    						mdb.delete("d_table", "d_id=?", new String [] {temp[arg2]+""});
    						mdb.close();
    						Toast.makeText(Two.this, "第"+temp[arg2]+"只被撕掉了。。", 1000).show();
    						Intent intent = new Intent(Two.this,Two.class);
    						startActivity(intent);
    						finish();
    					}
    				}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
    					
    					@Override
    					public void onClick(DialogInterface dialog, int which) {
    						// TODO Auto-generated method stub
    						
    					}
    				}).setTitle("你删个试试?!").create().show();
    				return false;
    			}
    			
    		});
    		
    	}
    	
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		
    		return true;
    	}
    	
    	@Override
    	public boolean onOptionsItemSelected(MenuItem item){
    		switch(item.getItemId()){
    		case R.id.insertId:
    			Intent intent=new Intent(Two.this,WrActivity.class);
    			intent.putExtra("cmd", 1);			
    			startActivity(intent);
    			finish();
    			break;
    		case R.id.exitId:
    			finish();
    			android.os.Process.killProcess(android.os.Process.myPid());
    		}
    		return super.onOptionsItemSelected(item);
    	}
    }
    


    本来应该要申明一个日记类implements Serializable

    但是在Activity之间传递信息的时候出错了...我就直接丢弃了,呵呵

    以及在修改删除数据库的时候,提取primary key的方法貌似有点复杂,就直接申明一个数组保存了,也真是醉了。。


    2.查看日记.class

    package com.my;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    
    public class Note_read extends Activity{
    	
    	public EditText wea;	//天气
    	public EditText tit;	//标题
    	public EditText cont;	//内容
    	public TextView dat;	//日期
    	private String d_wea;
    	private String d_tit;
    	private String d_con;
    	private String d_dat;
    	private int d_id;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.read);
            
            wea = (EditText)findViewById(R.id.image);
            tit = (EditText)findViewById(R.id.edit_10);
            cont = (EditText)findViewById(R.id.editView2);
            dat = (TextView)findViewById(R.id.editText1);
            
            Button b1 = (Button)findViewById(R.id.queding);
            Button b2 = (Button)findViewById(R.id.quxiao0);
            
            Intent intent = this.getIntent();
            d_id = intent.getExtras().getInt("_ID");
            d_tit = intent.getStringExtra("TIT");
            d_dat = intent.getStringExtra("DAT");
            d_con = intent.getStringExtra("CON");
            d_wea = intent.getStringExtra("WEA");
            
            loadDiaryData();
            
            setEditTextDisable();
            
            b1.setOnClickListener(new View.OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				Intent intent = new Intent(Note_read.this, WrActivity.class);
    				intent.putExtra("t_ID", d_id);
    				intent.putExtra("t_TIT", d_tit);
    				intent.putExtra("t_DAT", d_dat);
    				intent.putExtra("t_WEA", d_wea);
    				intent.putExtra("t_CON", d_con);
    				intent.putExtra("cmd", 0);
    				startActivity(intent);
    				finish();
    			}
    		});
            
            b2.setOnClickListener(new View.OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				Intent intent = new Intent(Note_read.this,Two.class);
    				startActivity(intent);
    				finish();
    			}
    		});
           
        }
    
    
        public void setEditTextDisable() {
    		// TODO Auto-generated method stub
    		wea.setText(d_wea);
    		tit.setText(d_tit);
    		dat.setText(d_dat);
    		cont.setText(d_con);
    	}
    
    
    	public void loadDiaryData() {
    		// TODO Auto-generated method stub
    		wea.setEnabled(false);
    		dat.setEnabled(false);
    		cont.setEnabled(false);
    		tit.setEnabled(false);
    	}
    
    
    	@Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
    }
    


    绑定了写日记的xml


    3.写日记.class


    package com.my;
    
    import java.text.SimpleDateFormat;
    import static com.my.MyOpenHelper.*;
    
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Intent;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class WrActivity extends Activity{
    	
    	int status = -1;		
    	int id = -1;			
    	
    	private EditText tianqi;		//天气
    	private EditText edit_1;		//标题
    	private TextView editView;		//日期
    	private EditText editText;		//内容
    	
    	private int Id;
    	private String Tit;
    	private String Dat;
    	private String Con;
    	private String Wea;
    	
    	MyOpenHelper myHelper;
    
    	public void onCreate(Bundle savedInstanceState){
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.write);
    		
    		Button button1=(Button)findViewById(R.id.queding);
    		Button button2=(Button)findViewById(R.id.quxiao0);
    		tianqi = (EditText)findViewById(R.id.image);
    		edit_1 = (EditText) findViewById(R.id.edit_10);
    		editView = (TextView)findViewById(R.id.editText1);
    		editText = (EditText)findViewById(R.id.editView2);
    		
    		SimpleDateFormat dataFormat=new SimpleDateFormat("yyyy-MM-dd");
    		editView.setText(dataFormat.format(new java.util.Date()));
    		
    		myHelper = new MyOpenHelper(this, DB_NAME,null,1);
    		SQLiteDatabase db = myHelper.getReadableDatabase();
    		Intent intent = getIntent();
    		Id = intent.getExtras().getInt("t_ID");
            Tit = intent.getStringExtra("t_TIT");
            Dat = intent.getStringExtra("t_DAT");
            Con = intent.getStringExtra("t_CON");
            Wea = intent.getStringExtra("t_WEA");
    		status = intent.getExtras().getInt("cmd");		
    		
    		if(status == 0){
    			tianqi.setText(Wea);
    			edit_1.setText(Tit);
    			editView.setText(Dat);
    			editText.setText(Con);
    		}
    		
    		button1.setOnClickListener(new View.OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				
    				String a = edit_1.getEditableText().toString().trim();
    				String b = editText.getEditableText().toString().trim();
    				
    				if(a.equals("")||b.equals("")){
    					Toast.makeText(WrActivity.this, "请输入标题和内容", 1000).show();
    					return ;
    				}
    				
    				switch(status){
    				case 0:
    					updata();
    					break;
    				case 1:
    					insert();
    					break;
    				}
    				
    				Intent intent = new Intent(WrActivity.this,Two.class);
    				intent.putExtra("id", MyOpenHelper.DB_NAME.indexOf(id));
    				startActivity(intent);
    			}
    		});
    		
    		button2.setOnClickListener(new View.OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				Intent intent = new Intent(WrActivity.this,Two.class);
    				startActivity(intent);
    			}
    		});
    		
    	}
    		
    	
    	public void updata(){
    		SQLiteDatabase db = myHelper.getWritableDatabase();
    		ContentValues values = new ContentValues();
    		values.put("d_title", edit_1.getText().toString());
    		values.put("d_image", tianqi.getText().toString());
    		values.put("d_data", editView.getText().toString());
    		values.put("d_content",editText.getText().toString());
    		db.update("d_table", values, "d_id=?", new String []{Id+""});
    		db.close();
    	}
    		
    		public void insert (){
    			SQLiteDatabase db = myHelper.getWritableDatabase();
    			ContentValues values=new ContentValues();
    			values.put("d_title", edit_1.getText().toString());
    			values.put("d_image", tianqi.getText().toString());
    			values.put("d_data", editView.getText().toString());
    			values.put("d_content",editText.getText().toString());
    			db.insert("d_table",null,values);
    			db.close();
    		}
    	
    }
    


    4.最后是数据库类

    package com.my;
    
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class MyOpenHelper extends SQLiteOpenHelper{
    
    	public final static String DB_NAME = "db_name";	
    	public final static String TABLE_NAME = "d_table";
    	public final static int VERSION=1;				
    	public final static String ID = "d_id";			
    	public final static String TITLE = "d_title";	
    	public final static String CONTENT = "d_content";
    	public final static String IMAGE = "d_image";	
    	public final static String DATATIME = "d_data";	
    
    	public MyOpenHelper(Context context, String name, CursorFactory factory,
    			int version) {	
    		super(context, name, factory, version);
    		// TODO Auto-generated constructor stub
    	}
    	
    	public MyOpenHelper(Context context, String name) {
    		super(context, name,null,VERSION);
    		// TODO Auto-generated constructor stub
    	}
    	
    	public MyOpenHelper(Context context, String name,int version) {
    		super(context, name, null, version);
    		// TODO Auto-generated constructor stub
    	}
    	
    	@Override
    	public void onCreate(SQLiteDatabase db) {
    		// TODO Auto-generated method stub
    		db.execSQL("create table d_table(d_id integer primary key autoincrement,d_title text,d_image text,d_data varchar(20),d_content text);");
    	}
    	
    	@Override
    	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    		// TODO Auto-generated method stub
    		System.out.println("updata");
    	}
    	
    	public Cursor query(){
    	SQLiteDatabase db = getWritableDatabase();
    
    	Cursor cursor = db.query("d_table", null, null, null, null, null, null);
    
    	return cursor;
    	}
    }
    



    嗯,代码写的相当的乱,大神看了以后勿喷~

  • 相关阅读:
    移动采编app
    分布式自动化测试
    appium --log-timestamp > appium.log
    处理安卓的弹窗
    Sublime text3修改tab键为缩进为四个空格
    安卓自动化测试——rf
    敏捷软件开发
    photoshop怎么旋转图片
    thinkPHP5.0模型实现软删除
    thinkPHP5.0数据查询表达式生成技巧
  • 原文地址:https://www.cnblogs.com/pngcui/p/4335893.html
Copyright © 2011-2022 走看看