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());
}
}
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/18602770
千万千万赵千万