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 .

2
  • Did you try custom adapter? you can refer this link stackoverflow.com/questions/8166497/… Commented Jul 2, 2018 at 11:28
  • I could not understand how to implement it here . Can you guide me ? @Jack Commented Jul 2, 2018 at 11:34

2 Answers 2

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;
}}
Sign up to request clarification or add additional context in comments.

9 Comments

how to decrypt the string in fetch() method ?
You should use a custom Adapter and model for that.
I am very new in android development . Can you guide me step by step please ?
Alright i will, can you use recyclerView or it has specific requirement for listview?
I need listview
|
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

4 Comments

How can I decrypt the data in arraylist ?
String encryptedString = encrypt("My String","MySecretKey") for encryption ... And String decryptedString = decrypt(encryptedString,"MySecretKey") for decryption
there is a separate class for this two methods
test a code like this: final String[] from = new String[] { decrypt(DatabaseHelper.TITLE,"MySecretKey") , ......... }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.