0

Don't know how to ask the question or maybe the title is wrong too . I have tried to find the answer everywhere but didn't get . Help . I am getting data from SQLite database in a ListView which contains four TextViews .

final String[] from = new String[] { DatabaseHelper._ID, DatabaseHelper.TITLE, DatabaseHelper.USERNAME, DatabaseHelper.PASSWORD };
final int[] to = new int[] { R.id.id, R.id.dataTitle, R.id.dataUsername, R.id.dataPassword };

DBManager dbManager = new DBManager(this);
dbManager.open();

Cursor cursor = dbManager.fetch();

ListView dataList = findViewById(R.id.dataList);
dataList.setEmptyView(findViewById(R.id.emptyData));

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contents, cursor, from, to, 0);
adapter.notifyDataSetChanged();
dataList.setAdapter(adapter);

Where fetch() is :

public Cursor fetch() {
            String[] columns = new String[] {
                    DatabaseHelper._ID,
                    DatabaseHelper.TITLE,
                    DatabaseHelper.USERNAME ,
                    DatabaseHelper.PASSWORD
            };
    Cursor cursor = database.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}

Now the problem is I have stored String data in database in encrypted format. I want to decrypt the Strings before showing in the ListView . Please help me . Thanks .

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sudipta Basak
  • 179
  • 13

2 Answers2

0

you should use AysncTask for retrieving data from database because if data is larger then UI may be unresponsive and hang and decrypt the string in fetch() method.

You should Load listview in onPostExecute method()

Example:

private class GetData extends AsyncTask<Void, Void, Void> {
    ArrayList<YourModel> youArray=null;

    @Override
    protected Void doInBackground(Void... voids) {
        youArray=dbManager.fetch() // Return decrypted array from fetch method.
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void result) {
      // Load your listview here...
       SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contents, cursor, from, to, 0);
       dataList.setAdapter(adapter);
       adapter.notifyDataSetChanged();
    }
}

Your ListView Adapter

public class LazyAdapter extends BaseAdapter {

private Context context=null;
 ArrayList<YourModel> data=new ArrayList<>;
private static LayoutInflater inflater=null;

public LazyAdapter(Context context, ArrayList<YourModel> data) {
    this.context = a;
    this.data=data;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.your_view, null);

    // Your subviews which needed to be initialized and set.
    TextView email = (TextView)vi.findViewById(R.id.email); // email
    TextView password = (TextView)vi.findViewById(R.id.password); // password



    User user = data.get(position);

    // Setting all values in listview
    email.setText(user.getEmail());
    password.setText(user.getPassword());

    return vi;
}}
Arpit bandil
  • 204
  • 2
  • 9
0

if i have understood your point correctly you can add encrypted data to ArrayList then encrypt in array list and next add to listview

Mehrdad
  • 1,477
  • 15
  • 17