属性里面不能有this

来源:互联网 发布:番茄 软件 编辑:程序博客网 时间:2024/04/25 19:18

可以放到构造器或是onCreate()中

package com.example.cpa;import android.content.ContentResolver;import android.content.ContentValues;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    public static final String URI_STR="content://com.wy.cpb";    public static final Uri URI_CP=Uri.parse(URI_STR);//    ContentResolver mContentResolver= this.getContentResolver();//这样不对,要放到构造器中    ContentResolver mContentResolver;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mContentResolver= this.getContentResolver();    }    public void go1(View view) {        Toast.makeText(this, "增加数据", Toast.LENGTH_SHORT).show();        ContentValues mContentValues=new ContentValues();        mContentValues.put("username","wy");        mContentValues.put("sex","boy");        mContentResolver.insert(URI_CP,mContentValues);//        ContentValues contentValues=new ContentValues();//        contentValues.put("username","erhuang999");//        contentValues.put("sex","母");//        Uri insert = contentResolver.insert(URI_CP, contentValues);//        long id= ContentUris.parseId(insert);//        Toast.makeText(this, insert.toString()+  "   id值为:" + id, Toast.LENGTH_SHORT).show();    }//    public void go2(View view) {////        Toast.makeText(this, "查询全部数据", Toast.LENGTH_SHORT).show();////        Cursor cursor = contentResolver.query(URI_CP, new String[]{"username", "sex"}, "sex=?", new String[]{"man"}, null);////        StringBuilder stringBuilder=new StringBuilder();////        while (cursor.moveToNext()){////            stringBuilder.append(cursor.getString(cursor.getColumnIndex("username")));////            stringBuilder.append("\t");////            stringBuilder.append(cursor.getString(cursor.getColumnIndex("sex")));////            stringBuilder.append("\r\n");////        }////        cursor.close();////        tv.setText(stringBuilder.toString());////        //content://com.sj.cp/mycpall//        String uriStr=URI_STR+"/mycpall";//        Uri uri = Uri.parse(uriStr);//        contentResolver.query(uri,null,null,null,null);//    }////    public void go3(View view) {//        Toast.makeText(this, "查询指定ID数据", Toast.LENGTH_SHORT).show();////        //content://com.sj.cp/mycp/3//        String uriStr=URI_STR+"/mycp/3";//        Uri uri = Uri.parse(uriStr);//        contentResolver.query(uri,null,null,null,null);////    }////    public void go4(View view) {//        Toast.makeText(this, "删除数据", Toast.LENGTH_SHORT).show();//        contentResolver.delete(URI_CP,"username=?",new String[]{"dahuang2"});////    }////    public void go5(View view) {//        Toast.makeText(this, "修改数据", Toast.LENGTH_SHORT).show();//        ContentValues contentValues=new ContentValues();//        contentValues.put("username","erhuang6666");//        contentResolver.update(URI_CP,contentValues,"username=?",new String[]{"dahuang6"});//    }}
0 0
原创粉丝点击