在加载列表视图之前更改 ListView TextView Strings/Text

Changing the ListView TextView Strings/Text before loading of the listview

不知道怎么问,或者标题也写错了。我试图到处寻找答案,但没有得到。帮助 。 我从 SQLite 数据库中的 ListView 中获取数据,其中包含四个 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);

其中 fetch() 是:

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;
}

现在的问题是我已将字符串数据以加密格式存储在数据库中。我想在 ListView 中显示之前 decrypt 字符串。 请帮我 。 谢谢.

您应该使用 AysncTask 从数据库中检索数据,因为如果数据较大,则 UI 可能没有响应并在 fetch() 方法中挂起和解密字符串。

您应该在 onPostExecute 方法中加载列表视图()

示例:

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();
    }
}

您的 ListView 适配器

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;
}}

如果我正确理解了您的观点,您可以将加密数据添加到 ArrayList,然后在数组列表中加密,然后添加到列表视图