我正在构建一个简单的应用程序,我想从手机中检索所有联系人并将其显示在textView中。使用加载器CallBack方法,但按下加载按钮后我得到一个空白屏幕。还提供了相同的读写权限。在下面发布我的代码,请帮忙。 提前致谢
MainActivity.java
package com.example.sumeet.contentproviderdemoyoutube;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, LoaderManager.LoaderCallbacks<Cursor> {
TextView textView;
Button button;
String TAG = "Show";
private String mColumnProjection[] = new String[]{
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.Contacts.CONTACT_STATUS,
ContactsContract.Contacts.HAS_PHONE_NUMBER};
private String mSelectionClause = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + "='Simpi'";
private String[] mSelectionArguments = new String[]{"Simpi"};
private String mOrderBy = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv_show);
button = (Button) findViewById(R.id.load);
// ContentResolver contentResolver = getContentResolver();
// Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
// mColumnProjection,
// mSelectionClause,
// null,
// null);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.load:
getLoaderManager().initLoader(1, null, this);
Toast.makeText(this, "Button Clicked", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
if (i == 1) {
Toast.makeText(this, "Inside onloader", Toast.LENGTH_SHORT).show();
return new CursorLoader(this, ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
}
return null;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
Toast.makeText(this, "OnFinish", Toast.LENGTH_SHORT).show();
if (cursor != null && cursor.getCount() > 0) {
StringBuilder stringBuilder = new StringBuilder("");
while (cursor.moveToNext()) {
stringBuilder.append(cursor.getString(0) + "," + cursor.getString(1) + "," + cursor.getString(2) + "\n");
}
textView.setText(stringBuilder.toString());
} else {
textView.setText("No Contacts to display");
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
Toast.makeText(this, "onLoader", Toast.LENGTH_SHORT).show();
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sumeet.contentproviderdemoyoutube.MainActivity">
<Button
android:id="@+id/load"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load" />
<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sumeet.contentproviderdemoyoutube">
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
将onClick()
中的init loader调用更改为
getLoaderManager().initLoader(1, null, this).forceLoad();
同样documentation说明
Blockquote必须使用
CursorLoader(Context, Uri, String[], String, String[], String)
或使用CursorLoader(Context)
创建一个空实例并使用setUri(Uri)
填充所需的参数,使用要执行的查询的完整信息构建CursorLoader。 ,setSelection(String)
,setSelectionArgs(String[])
,setSortOrder(String)
和setProjection(String[])
。
尝试将此信息提供给CursorLoader构造函数