さて、これは、CSV ファイルに保存されている sharedPreferences をエクスポートしようとしているアクティビティ クラスです。これは動作しません。私は何を間違っていますか?sharedPreferences 項目を CSV ファイルに正しく書き込むにはどうすればよいですか?
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Admin extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
Button btViewContacts = (Button)findViewById(R.id.btnViewContacts);
Button btDeleteContacts = (Button)findViewById(R.id.btnDeleteContacts);
Button btExportCSV = (Button)findViewById(R.id.btnExportCSV);
final Context context = this;
final SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(this);
btViewContacts.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Admin.this, Contacts.class));
}
});
btDeleteContacts.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//DISPLAY ALERT DIALOG
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); //show an alertDialog when user selects delete radio button and clicks process, prompt to confirm
//set title
alertDialogBuilder.setTitle("DELETE ALL CONTACTS?");
//set dialog message
alertDialogBuilder
.setMessage("Are you sure you want to delete ALL acquired contact info?")
.setCancelable(true)
//no button
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { //if user selects no, close dialog
// TODO Auto-generated method stub
//if clicked this will close the dialog and do nothing.
dialog.cancel();
}
})
//yes button
.setPositiveButton("Yes", new DialogInterface.OnClickListener() { //if user selects yes, clear the shared preferences and display confirmation message when deleted.
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//if this button is clicked it will erase the values in memory
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.commit();
//displays toast message confirming deletion of race info
Toast.makeText(Admin.this, "Contact Info Deleted.", Toast.LENGTH_SHORT).show();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
Map<String, ?> allEntries = sharedPref.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
Log.d("TAG", entry.getKey() + ": " + entry.getValue().toString());
}
btExportCSV.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
generateCsvFile(Environment.getExternalStorageDirectory().getPath());
}
});
}
private void generateCsvFile(String sFileName)
{
final SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(this);
String delimiter = ",";
try
{
FileWriter writer = new FileWriter(sFileName);
writer.append("First Name");
writer.append(',');
writer.append("Last Name");
writer.append(',');
writer.append("Email");
writer.append(',');
writer.append("Phone");
writer.append(',');
writer.append("Address");
writer.append(',');
writer.append("City");
writer.append(',');
writer.append("State");
writer.append(',');
writer.append("Zip");
writer.append('\n');
Map<String, ?> allEntries = sharedPref.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
Map<String,?> all = sharedPref.getAll();
Iterator it = all.entrySet().iterator();
Map.Entry pairs = (Map.Entry)it.next();
if (pairs.getKey().toString().startsWith("contactid_")) {
String strContact = sharedPref.getString((String)pairs.getKey(), "");
String[] data = TextUtils.split(strContact, delimiter);
writer.write(strContact);
writer.append('\n');
}
Toast.makeText(Admin.this, "ContAcq's Exported to .CSV.", Toast.LENGTH_SHORT).show();
//generate whatever data you want
writer.flush();
writer.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}