12.28

package com.example.myapp;

 

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.widget.TextView;

 

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    private TextView dataTextView;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        dataTextView = findViewById(R.id.dataTextView);

 

        // 查询 Content Provider 中的数据

        Uri uri = Uri.parse("content://com.example.provider/data");

        String[] projection = {"_id", "name", "age"};

        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

 

        StringBuilder stringBuilder = new StringBuilder();

        if (cursor != null && cursor.moveToFirst()) {

            do {

                int id = cursor.getInt(cursor.getColumnIndex("_id"));

                String name = cursor.getString(cursor.getColumnIndex("name"));

                int age = cursor.getInt(cursor.getColumnIndex("age"));

                stringBuilder.append("ID: ").append(id).append(", Name: ").append(name).append(", Age: ").append(age).append("\n");

            } while (cursor.moveToNext());

            cursor.close();

        }

 

        dataTextView.setText(stringBuilder.toString());

    }

}

posted @ 2024-12-28 19:48  赵千万  阅读(2)  评论(0编辑  收藏  举报