文章标题andriod的ContentResolver内容访问者

在java中代码中

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private ContentResolver cr;
    private EditText et_main;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取内容访问者
        cr = getContentResolver();
        et_main = (EditText) findViewById(R.id.et_main);
    }
    public void getdata(View view){
        if (TextUtils.isEmpty(et_main.getText())){
            //如果查询为空就查所有
            uri = Uri.parse("content://com.zking.g150831_android16_sqlite.person/persons");

        }
        else {
            //否则查询单个
            String id=et_main.getText().toString();
            uri = Uri.parse("content://com.zking.g150831_android16_sqlite.person/persons/"+id);
        }
        //

        Cursor cursor= cr.query(uri,null,null,null,null);

        //单个查询
        //01.条件查询_id=3
        //02.网页查询?id=4

        //03.URI匹配器



        //SimpleCursorAdaper
        while (cursor.moveToNext()){
            int id=cursor.getInt(cursor.getColumnIndex("_id"));
            String name=cursor.getString(cursor.getColumnIndex("name"));
            int age=cursor.getInt(cursor.getColumnIndex("age"));
            Log.i("text","_"+id+"_"+name+"_"+age);
        }
    }
}

你可能感兴趣的:(安卓,andriod)