加载listview之前更改ListView TextView字符串/文本

时间:2018-07-02 11:24:25

标签: android listview android-sqlite simplecursoradapter

不知道怎么问问题,或者标题也错了。我试图在各处找到答案,但是没有得到。救命 。 我从 SQLite 数据库的 ListView 中获取数据,该数据库包含四个 TextView

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中。 请帮我 。 谢谢。

2 个答案:

答案 0 :(得分:0)

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

您应该在onPostExecute方法()中加载listview

示例:

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

答案 1 :(得分:0)

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