Android高级控件小练习

效果图


MainActivity

package com.example.week3_lihuihui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.week3_day5_homework.db.MySqliteHelper;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.NotificationCompat;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button btn;//声明编辑按钮
	private List<Map<String, Object>> list;//存放查询数据的list
	private MySqliteHelper helper;//声明数据库
	private ListView lv;
	//存放用户信息
	private String name;
	private String address;
	private String love;
	//声明适配器
	private MyAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);//加载布局
		btn = (Button) findViewById(R.id.btn);
		lv = (ListView) findViewById(R.id.lv);
		//实例化数据库
		helper = new MySqliteHelper(MainActivity.this);
		//得到数据
		getDate();
		//实例化是适配器
		adapter = new MyAdapter();
		//为lv绑定适配器
		lv.setAdapter(adapter);
		//为lv绑定上下文菜单
		registerForContextMenu(lv);
		/**
		 * 编辑按钮监听器
		 */
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						SecondActivity.class);
				startActivity(intent);
			}
		});
	}

	/**
	 * 自定义适配器
	 */
	public class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {

			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View view, ViewGroup arg2) {
			View v = LayoutInflater.from(getApplicationContext()).inflate(
					R.layout.item, null);
			TextView tv1 = (TextView) v.findViewById(R.id.TextView1);
			TextView tv2 = (TextView) v.findViewById(R.id.TextView2);
			TextView tv3 = (TextView) v.findViewById(R.id.TextView3);
			String name = list.get(arg0).get("name").toString();
			String address = list.get(arg0).get("address").toString();
			String love = list.get(arg0).get("love").toString();
			tv1.setText(name);
			tv2.setText(love);
			tv3.setText(address);
			return v;
		}
	}

	/**
	 * 从数据库中查询数据
	 */
	protected void getDate() {
		list = new ArrayList<Map<String, Object>>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from person";
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			Map<String, Object> map = new HashMap<String, Object>();
			name = cursor.getString(cursor.getColumnIndex("name"));
			address = cursor.getString(cursor.getColumnIndex("address"));
			love = cursor.getString(cursor.getColumnIndex("love"));
			map.put("name", name);
			map.put("address", address);
			map.put("love", love);
			list.add(map);

		}
		db.close();

	}

	/**
	 * 实时更新数据
	 */
	@Override
	protected void onRestart() {
		getDate();
		lv.setAdapter(new MyAdapter());
		super.onRestart();
	}

	/**
	 * 菜单
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.main, menu);
	}

	/**
	 * 长点击ListView时弹出上下文菜单
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		/*
		 * 删除
		 */
		case R.id.delete:
			// 从item里 获取绑定的listView的一些信息
			AdapterContextMenuInfo contextMenuInfo = (AdapterContextMenuInfo) item
					.getMenuInfo();

			list.remove(contextMenuInfo.position);
			SQLiteDatabase db = helper.getReadableDatabase();
			String sql = "delete from person where name='" + name + "'";
			db.execSQL(sql);
			Toast.makeText(MainActivity.this, "删除", 0).show();
			onRestart();
			break;
		/**
		 * 分享
		 */
		case R.id.share:
			/**
			 * 监听长点击事件,得到长点击信息
			 */
			lv.setOnItemLongClickListener(new OnItemLongClickListener() {

				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					name = list.get(arg2).get("name").toString();
					address = list.get(arg2).get("address").toString();
					love = list.get(arg2).get("love").toString();
					return false;
				}
			});
			NotificationCompat.Builder builder = new NotificationCompat.Builder(
					MainActivity.this);
			builder.setSmallIcon(R.drawable.ic_launcher);
			builder.setContentTitle("标题");
			builder.setContentText("内容");
			Intent intent = new Intent(MainActivity.this, Message.class);
			intent.putExtra("name", name);
			intent.putExtra("address", address);
			intent.putExtra("love", love);
			PendingIntent intent2 = PendingIntent.getActivity(
					MainActivity.this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
			builder.setContentIntent(intent2);
			NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
			manager.notify(1, builder.build());
			break;

		default:
			break;
		}
		return super.onContextItemSelected(item);
	}
}
编辑页面--SecondActivity

package com.example.week3_lihuihui;

import com.example.week3_day5_homework.db.MySqliteHelper;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

public class SecondActivity extends Activity implements OnClickListener {

	private Button finish;
	private EditText et_name, et_add;
	private CheckBox cb1, cb2, cb3;
	private MySqliteHelper helper;
	private String intersing1 = "";
	private String intersing2 = "";
	private String intersing3 = "";
	private String name;
	private String address;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		finish = (Button) findViewById(R.id.finish);
		et_add = (EditText) findViewById(R.id.et_add);
		et_name = (EditText) findViewById(R.id.et_name);
		cb1 = (CheckBox) findViewById(R.id.cb1);
		cb2 = (CheckBox) findViewById(R.id.cb2);
		cb3 = (CheckBox) findViewById(R.id.cb3);

		helper = new MySqliteHelper(SecondActivity.this);
		/**
		 * 兴趣监听器
		 */
		// 兴趣爱好选择监听器
		cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

				if (arg1) {
					intersing1 = cb1.getText().toString();
				} else {
					intersing1 = "";
				}

			}
		});
		cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

				if (arg1) {
					intersing2 = cb2.getText().toString();
				} else {
					intersing2 = "";
				}

			}
		});

		cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if (arg1) {
					intersing3 = cb3.getText().toString();
				} else {
					intersing3 = "";
				}
			}
		});

		finish.setOnClickListener(this);

	}

	@Override
	public void onClick(View arg0) {
		dialog();

	}

	/**
	 * dialog对话框
	 */
	public void dialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				SecondActivity.this);
		builder.setTitle("标题");
		builder.setMessage("确认提交吗?");
		builder.setPositiveButton("确认",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						name = et_name.getText().toString().trim();
						address = et_add.getText().toString().trim();
						SQLiteDatabase db = helper.getReadableDatabase();
						String sql = "insert into person values('" + name
								+ "','" + address + "','" + intersing1
								+ intersing2 + intersing3 + "')";
						db.execSQL(sql);
						finish();
					}
				});

		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				});
		builder.create();
		builder.show();
	}
}
创建数据库页面---MySqliteHelper

package com.example.week3_day5_homework.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 1 建立一个MySqliteHelper类,继承SQLiteOpenHelper,并实现方法
 * 2 重写方法
 */
public class MySqliteHelper extends SQLiteOpenHelper {
	// 数据库名称
	private static String NAME = "info.db";
	// 版本号
	private static int VERSION = 1;

	/**
	 * 构造方法
	 * 
	 * @param context
	 *            :上下文
	 * @param name
	 *            :数据库名称
	 * @param factory
	 *            :游标工厂
	 * @param version
	 *            :数据库版本
	 */
	public MySqliteHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	// 改写构造方法
	public MySqliteHelper(Context context) {
		super(context, NAME, null, VERSION);
	}

	/**
	 * 数据库第一次创建时,回调此方法
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		// 创建数据库表
		String sql = "create table person(name varchar(60),address varchar(26),love varchar(20))";
		// 执行数据库语句
		db.execSQL(sql);
	}

	/**
	 * 数据库升级 数据版本发生改变时调用,newVersion>oldVersion
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if(newVersion>oldVersion){
			Log.i("===onUpgrade===", "数据库版本升级");
		}
	}

	/**
	 * 数据库版本降级,只有数据库版本发生重大错误时,数据版本发生改变时调用,newVersion<oldVersion
	 */
	@Override
	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		super.onDowngrade(db, oldVersion, newVersion);
		if(newVersion<oldVersion){
			Log.i("===onDowngrade===", "数据库版本降级");
		}
	}
	/**
	 * 每次打开数据库时调用,主要是验证数据库是否打开
	 */
	@Override
	public void onOpen(SQLiteDatabase db) {
		super.onOpen(db);
		Log.i("===onOpen===", "数据库打开");
	}

}
显示通知信息页面

package com.example.week3_lihuihui;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Message extends Activity {

	private TextView tv1, tv2, tv3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message);
		tv1 = (TextView) findViewById(R.id.TextView1);
		tv2 = (TextView) findViewById(R.id.TextView2);
		tv3 = (TextView) findViewById(R.id.TextView3);

		Intent intent = getIntent();
		String name = intent.getStringExtra("name");
		String address = intent.getStringExtra("address");
		String love = intent.getStringExtra("love");

		tv1.setText(name);
		tv2.setText(love);
		tv3.setText(address);
		NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		manager.cancelAll();// 将所有的通知全部取消

	}
}
布局文件--主界面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
   >

    <LinearLayout
        android:id="@+id/l1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/vt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="130dp"
            android:text="主页"
            android:textSize="19sp" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="90dp"
            android:text="编辑" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/l1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           
           />

        
    </LinearLayout>

</LinearLayout>

第二个页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
      >

    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="130dp"
            android:text="信息"
            android:textSize="19sp" />

        <Button
            android:id="@+id/finish"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="90dp"
            android:text="完成" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="19sp" 
            android:text="姓名"/>
        <EditText 
            android:id="@+id/et_name"
             android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/LinearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="19sp" 
            android:text="住址"/>
        <EditText 
            android:id="@+id/et_add"
             android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/LinearLayout4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <TextView 
            android:id="@+id/inter"
             android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="19sp" 
            android:text="爱好"
            />
        <CheckBox 
            android:id="@+id/cb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="吃饭"
            />
          <CheckBox 
            android:id="@+id/cb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="睡觉"
            />
            <CheckBox 
            android:id="@+id/cb3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="听歌"
            />
    </LinearLayout>

</LinearLayout>
item界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
      >

    <RelativeLayout
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="74dp" >

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textColor="#ff0000"
           />
        <TextView 
            android:id="@+id/TextView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/TextView1"
              android:textSize="18sp"
            android:textColor="#ff0000"
         
            />

        <TextView
            android:id="@+id/TextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="24dp"
              android:textSize="18sp"
            android:textColor="#ff0000"
            />

    </RelativeLayout>

</LinearLayout>
显示通知界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
     >
    <TextView 
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="内容"
        android:gravity="center"
          android:textSize="18sp"
            android:textColor="#ff0000"/>
   


    <RelativeLayout
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="74dp" >

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
              android:textSize="18sp"
            android:textColor="#ff0000"
           />
        <TextView 
            android:id="@+id/TextView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/TextView1"
              android:textSize="18sp"
            android:textColor="#ff0000"
         
            />

        <TextView
            android:id="@+id/TextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="24dp"
              android:textSize="18sp"
            android:textColor="#ff0000"
            />

    </RelativeLayout>


</LinearLayout>
菜单界面

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/delete"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="删除"/>
    <item
        android:id="@+id/share"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="分享"/>

</menu>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值