如何在列表视图中显示使用Filewriter保存的项目 [英] how do i display an item saved using filewriter in a list view

查看:57
本文介绍了如何在列表视图中显示使用Filewriter保存的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用文件编写器在我的应用程序中保存敏感的用户详细信息,该文件已保存到指定的目标位置.我想在列表视图的另一个类中显示保存的信息,如何将信息显示在列表视图中,什么是最好的方法?

I am saving sensitive user details in my app using file writer, the file is saved to a specified destination. i want to display the saved information in another class in a list view, how do i go about displaying the information into a list view and what is the best way to do this?.

任何人都可以建议采取任何措施

can anybody suggest any means to go about this

保存信息的活动

public class PasswordKeeperActivity extends Activity implements 
AdapterView.OnItemSelectedListener {

// initialise
EditText username, password, note;
Button save, reset;
public String savedata = Environment.getExternalStorageDirectory().toString();

String[] countryNames={"Google", "Yahoo", "Facebook", "Twitter", "Instagram", "BBM", "Skype", "Other"};
int flags[] = {R.drawable.google, R.drawable.yahoo, R.drawable.facebook, R.drawable.twitter, R.drawable.instagram, R.drawable.bbm, R.drawable.skype, R.drawable.other};

private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
    Manifest.permission.READ_EXTERNAL_STORAGE,
    Manifest.permission.WRITE_EXTERNAL_STORAGE
  };
//  for inflating the menu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}

// on selection of the menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.view_passwords:
    Intent intent = new Intent(this, PasswordView.class);
    startActivity(intent);
    return true;

default:
    return super.onOptionsItemSelected(item);
}
}

public void onItemSelected(AdapterView<?> parent, View view,
                       int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)

}

public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

initialise();

//Getting the instance of Spinner and applying OnItemSelectedListener on 
it
Spinner spin = (Spinner) findViewById(R.id.planets_spinner);
spin.setOnItemSelectedListener(this);

CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), 
flags, countryNames);
spin.setAdapter(customAdapter);

//to set the site Edit Text to get the focus




  // save the data to the textfile
  save.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        // creates hidden directory if not existing
        File dir = new File(getCacheDir(), "/sk/");
        if (!dir.exists()) {
            dir.mkdirs();
        }

        // saving data part
        String sFileName = getCacheDir() + "/sk/logp.csv";
        try {
            FileWriter writer = new FileWriter(sFileName, true);

            String  countryNames, sUser, sPass, sAdd;

            countryNames =
            sUser = username.getText().toString();
            sPass = password.getText().toString();
            sAdd = note.getText().toString();



            if ((sUser.equals("")) && (sPass.equals("")) && 
    (sAdd.equals(""))) {
                Toast.makeText(getBaseContext(), "Please Enter At least 
    one Field",
                        Toast.LENGTH_SHORT).show();

            } else {
                if (sUser.equals(""))
                    sUser = "null";
                if (sPass.equals(""))
                    sPass = "null";
                if (sAdd.equals(""))
                    sAdd = "null";


                // encrypting the passwords before saving
                SimpleCrypto mcrypt = new SimpleCrypto();
                sPass = SimpleCrypto.bytesToHex( mcrypt.encrypt(sPass) );
                //sPass = SimpleCrypto.encrypt("fugly", sPass);




                writer.append(sUser);
                writer.append(',');

                writer.append(sPass);
                writer.append(',');
                writer.append(sAdd);

                writer.append('\n');

                // generate whatever data you want

                writer.flush();
                writer.close();

                Toast.makeText(getBaseContext(), "Password Saved!",
                        Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(PasswordKeeperActivity.this, 
    PasswordView.class);
                String[] myStrings = new String[] {"Google", "Yahoo", 
   "Facebook", "Twitter", "Instagram", "BBM", "Skype", "Other"};
                int logo[]  = new int[] {R.drawable.google, 
   R.drawable.yahoo, R.drawable.facebook, R.drawable.twitter, 
   R.drawable.instagram, 
   R.drawable.bbm, R.drawable.skype, R.drawable.other};
                intent.putExtra("strings", myStrings);
                intent.putExtra("logos", logo);
                startActivity(intent);
            }

        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }

    }
});

 // Reset
 reset.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        countryNames.equals("Google");
        note.setText("");
        username.setText("");
        password.setText("");
        Toast.makeText(getBaseContext(), "Field(s) Cleared!",
                Toast.LENGTH_SHORT).show();
    }
});

}



public void initialise() {


username = (EditText) findViewById(R.id.input_name);
password = (EditText) findViewById(R.id.input_email);
note = (EditText) findViewById(R.id.input_password);

save = (Button) findViewById(R.id.buttonSave);
reset = (Button) findViewById(R.id.ButtonReset);
}

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to 
 grant permissions
 *
  * @param activity
  */
 public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, 
Manifest.permission.WRITE_EXTERNAL_STORAGE);

 if (permission != PackageManager.PERMISSION_GRANTED) {
    // We don't have permission so prompt the user
    ActivityCompat.requestPermissions(
            activity,
            PERMISSIONS_STORAGE,
            REQUEST_EXTERNAL_STORAGE
    );
}
}
}

推荐答案

使用FileReader检索文件.您应该使用一个单独的类,该类存储使用FileWriter和FileReader加载和保存文件的方法.从那里,您可以向该类添加一个引用对象,并在整个项目中使用这些方法在需要时加载和保存数据.

Use a FileReader to retrieve files. You should use a separate class that stores methods to load and save the files using the FileWriter and FileReader. From there you can add a reference object to that class and use the methods across your entire project to load and save data when its necessary.

下面是如何使用FileReader从文件中检索数据的示例.

Below is a sample of how you can use a FileReader to retrieve data from a file.

File file = new File("Hello.txt");

FileReader fr = new FileReader(file);
char[]a = new char[100];

for(char c : a) {
    System.out.println(c);
}

fr.close();

这篇关于如何在列表视图中显示使用Filewriter保存的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆