I want to create a PreferenceFragment in android. There should be an item that allows the usere to import his old database into the app. Before I found the PreferenceFragment I used my own SettingsFragment, but as this seems to be better(and for everything else exxept this it is atm) I tried using this.
My Problem is that I used startActivityForResult with an intent to get the file and import the database. This was called with a buttonclick. In the new Preference Fragment I don't really have a way to get clicks.
Here is my Settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<com.myContainer.myApp.helper.ImportDatabaseSettingsDialog
android:id="@+id/database1"
android:key="import_database"
android:title="@string/importDatabaseFromDB" />
<SwitchPreference
android:key="pref_beta"
android:title="@string/enableBeta"
android:summary="@string/enableBetaSummary"
android:defaultValue="false" />
<Preference
android:id="@+id/database2"
android:key="import_database"
android:title="@string/importDatabaseFromDB" />
<ListPreference
android:id="@+id/database3"
android:key="import_database"
android:title="@string/importDatabaseFromDB" />
</PreferenceScreen>
The idead I had
a) I tried to get OnClickListeners
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
View data1 = mSettingsFragmentView.findViewById(R.id.database1);
View data2 = mSettingsFragmentView.findViewById(R.id.database2);
View data3 = mSettingsFragmentView.findViewById(R.id.database3);
data1.setOnClickListener(this);
data2.setOnClickListener(this);
data3.setOnClickListener(this);
}
I get this: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
So I can't get the items from settings.xml I guess.
b) The other Idea I had was using a DialogPreference. I use the same settings.xml as above.
package com.pi314.mylife.helper;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Toast;
import com.pi314.mylife.R;
import static android.app.PendingIntent.getActivity;
import static android.support.v4.app.ActivityCompat.startActivityForResult;
/**
* Created on 20.09.2015.
*/
public class ImportDatabaseSettingsDialog extends DialogPreference {
private static final int SELECT_DATABASEFILE = 1;
public ImportDatabaseSettingsDialog(Context context, AttributeSet attrs) {
super(context, attrs);
//setDialogLayoutResource(R.layout.numberpicker_dialog);
setDialogMessage(R.string.ARE_YOU_SURE_YOU_WANT_IMPORT);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
setDialogIcon(null);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
Log.d("ml Settings", "importing database");
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent finalIntent = Intent.createChooser(intent, "Select databasefile");
startActivityForResult(finalIntent, SELECT_DATABASEFILE);
}
}
}
But I can't use startActivityForResult as I don't have an Acitivity in my DialogPreference.
c) The best way I could think would just be to have an item like in that opens the intent when clicking
The closest I found help I found was this. It doesn't sound like a great way and si also 3 years old and I don't really get how get the DialogPreference Object in my settings class:
startactivityforresult from dialogpreference (non activity)
Helpfull links:
http://developer.android.com/reference/android/preference/DialogPreference.html
I solved it myself. So here is the solution, if someone is interested. I now use Preferences in settings.xml and created onPreferenceClickListener in my fragment.
settings.xml:
<Preference
android:key="export_database"
android:summary="@string/exportDatabasefileSummary"
android:title="@string/exportDatabasefile" />
<Preference
android:key="import_database"
android:summary="@string/importDatabaseFromDBSummary"
android:title="@string/importDatabaseFromDB" />
Fragment:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
Preference preferenceImportDatabase = findPreference("import_database");
preferenceImportDatabase.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
importDatabaseFromDB();
return false;
}
});
Preference preferenceExportDatabase = findPreference("export_database");
preferenceExportDatabase.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
exportDatabasefile();
return false;
}
});
}