2013年7月5日 19:49:29

玩游戏什么的是最让人高兴的事了,特别是班会活动大家一起玩~

现在要写的代码,一个工程里那么多,感觉不好贴出来啊~


主要思想点:

1,操作对象的Dao类里可写各种操作方法,如删除:

private SQLiteDatabase db;
public ClassDao(Context context){
db = DBUtil.getInstance(context);
}
  @param id
public void deleteClass(int id){
db.execSQL("delete from t_class where id=?",new Object[]{id});
}

2,在Bean里定义常量好调用:

public static final String AUTHORITY = "com.lovo.studentmanager.class";
public static final class Data implements BaseColumns{
public static final String NAME = "c_name";
public static final Uri CONTENT_URI = Uri.parse("content://"+AUTHORITY + "/data");
}

3,在自定义ContentProvider里:

oncreate里: //注册URI
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.lovo.studentmanager.class", "data", 1);
uriMatcher.addURI("com.lovo.studentmanager.class", "data/#", 2);

db = DBUtil.getInstance(this.getContext());
return true;

insert里:

		long id = db.insert(TABLE_NAME, null, values);
		if(id >= 0){
			uri = ContentUris.withAppendedId(uri, id);
		}
		return uri;
 query里:
Cursor cursor = null;
		switch (uriMatcher.match(uri)) {
			case CLASS_WITHOUT_ID:
				cursor = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
				break;
			case CLASS_WITH_ID:	
				long id = ContentUris.parseId(uri);
				if(selection != null){
					selection += " and _id="+id;
				}else{
					selection = "_id="+id;
				}
				cursor = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
				break;
			default:
				break;
		}
		return cursor;

4,主界面里专门定义个方法初始化数据,在onResume里可调用和更新(Adapter的notifyDataSetChanged();),

	/**
	 * 初始化数据
	 */
	public void loadData() {
		classList.clear();
		Cursor cursor = getContentResolver().query(ClassBean.Data.CONTENT_URI,
				null, null, null, null);
		while (cursor.moveToNext()) {
			//新个班级bean对象,找到游标所指向的班级uri的_ID和name到bean里
			ClassBean classBean = new ClassBean();
			long classId = cursor.getInt(cursor
					.getColumnIndex(ClassBean.Data._ID));
			classBean.setId(classId);
			classBean.setClassName(cursor.getString(cursor
					.getColumnIndex(ClassBean.Data.NAME)));

			List studentList = new ArrayList();
			// 根据班级id查询学生信息
			Cursor stuCursor = getContentResolver().query(
					StudentBean.Data.CONTENT_URI, null,
					StudentBean.Data.CLASS_ID + "=" + classId, null, null);
			//循环出所有查找到的学生加入studentList集合
			while (stuCursor.moveToNext()) {
				StudentBean studentBean = new StudentBean();
				studentBean.setId(stuCursor.getInt(stuCursor
						.getColumnIndex(StudentBean.Data._ID)));
				studentBean.setName(stuCursor.getString(stuCursor
						.getColumnIndex(StudentBean.Data.NAME)));
				studentBean.setAge(stuCursor.getInt(stuCursor
						.getColumnIndex(StudentBean.Data.AGE)));
				studentList.add(studentBean);
			}
			stuCursor.close();//关掉游标
			classBean.setStudentList(studentList);//班级对象里有学生集合
			classList.add(classBean);
		}
		cursor.close();
	}

5,实现点击接口的onClick里,switch选择case

	@Override
	public void onClick(View v) {
		// if(v == addStudentBtn){
		// Intent intent = new Intent();
		// intent.setAction("com.lovo.studentmanager.add");
		// startActivity(intent);
		// }

		switch (v.getId()) {
		case R.id.main_btn_add_student:
			Intent intent = new Intent();
			intent.setAction("com.lovo.studentmanager.add");
			startActivity(intent);
			break;
		case R.id.main_btn_add_class:
			Intent intent2 = new Intent(MainActivity.this,AddClassActivity.class);
//			intent2.setAction("com.lovo.studentmanager.add");
			startActivity(intent2);
			break;
		default:
			break;
		}

	}
6,学生显示自定义BaseExpandableListAdapter的adapte

 getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent)方法里:

if (convertView == null) {
			convertView = context.getLayoutInflater().inflate(
					R.layout.student_list_content, null);
		}
		TextView tv = (TextView) convertView
				.findViewById(R.id.student_list_content_tv_name);
		tv.setText(classList.get(groupPosition).getStudentList()
				.get(childPosition).getName());
		Button updateBtn = (Button) convertView
				.findViewById(R.id.student_list_content_btn_update);
		Button delBtn = (Button) convertView
				.findViewById(R.id.student_list_content_btn_del);


		updateBtn.setOnClickListener(this);
		updateBtn.setTag(R.layout.student_list_content, 1);
		updateBtn.setTag(classList.get(groupPosition).getStudentList()
				.get(childPosition).getId());
		delBtn.setOnClickListener(this);
		delBtn.setTag(R.layout.student_list_content, 2);
		delBtn.setTag(classList.get(groupPosition).getStudentList()
				.get(childPosition).getId());
		return convertView;

7,同一个Activity界面可被俩个方法跳转,.只要在intent.setAction为其中某项即可

        <activity android:name="com.lovo.ui.StudentEditActivity" >
            <intent-filter >
                <action android:name="com.lovo.studentmanager.add" />
                <action android:name="com.lovo.studentmanager.update" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
8,自定义Adapter的

@Override
public View getView(int position, View convertView, ViewGroup parent)里:为空在加载:

		if (convertView == null) {
			convertView = context.getLayoutInflater().inflate(
					R.layout.student_list_content, null);
		}

9,编辑或者修改界面,得到Action判断是什么操作:isAdd是个布尔值,而分别操作:

String action = getIntent().getAction();
		//判断是否为增加或修改
		if (action.equals("com.lovo.studentmanager.add")) {
			isAdd = true;
		} else {
			isAdd = false;
		}
10,

	/**
	 * 根据学生id查询学生的信息
	 * 
	 * @param id
	 */
	public void searchStudent(long id) {

		Uri uri = ContentUris.withAppendedId(StudentBean.Data.CONTENT_URI, id);
		Cursor cursor = getContentResolver().query(uri, null, null, null, null);
		if (cursor.moveToFirst()) {// 只有1条数据,直接if到开头即可
			nameText.setText(cursor.getString(cursor
					.getColumnIndex(StudentBean.Data.NAME)));
			ageText.setText(cursor.getString(cursor
					.getColumnIndex(StudentBean.Data.AGE)));
			int classId = cursor.getInt(cursor
					.getColumnIndex(StudentBean.Data.CLASS_ID));
			for (int i = 0; i < classList.size(); i++) {
				classSpinner.setSelection(i);
			}
		}
	}
11,点击确定按钮看是进行增加还是修改学生:

			String name = nameText.getText().toString();
			int age = Integer.parseInt(ageText.getText().toString());
			int classId = Integer.parseInt(classSpinner.getSelectedView()
					.getTag().toString());
			ContentValues values = new ContentValues();
			values.put(StudentBean.Data.NAME, name);
			values.put(StudentBean.Data.AGE, age);
			values.put(StudentBean.Data.CLASS_ID, classId);

			if (isAdd) {// 增加学生;
				getContentResolver().insert(StudentBean.Data.CONTENT_URI,
						values);
			} else {//修改学生
				Uri uri = ContentUris.withAppendedId(
						StudentBean.Data.CONTENT_URI,
						getIntent().getLongExtra("studentId", 0));//Long还要改另外个地方
				getContentResolver().update(uri, values, null, null);
			}
//			classSpinner.setSelection(1);//实验?吧这个方法里其他所有注释;问题,前面啥东西顺序调到后边
			finish();



凌波丽,满满的绷带伤心的怀

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值