android - 无法应用 Arraylist

标签 android arraylist

我正在尝试获取密码描述文本输入以填充我的应用程序的数组 View 。

enter image description here

不幸的是,我的部分 while(cursor.movetoNext()) 说我的字符串不能应用于数组列表。我正在尝试确定要更改我的 updateList 方法中的内容以适应我的密码项。

这是我的更新列表方法。

   private void updateList(){
           ArrayList<String> tasklist = new ArrayList<>();
           SQLiteDatabase db = mHelper.getReadableDatabase();
           Cursor cursor = db.query( PasswordContract.PasswordEntry.TABLE, new String[]{PasswordContract.PasswordEntry._ID, PasswordContract.PasswordEntry.COL_PASSWORD, PasswordContract.PasswordEntry.COL_PASS_DESC},
                   null, null, null, null, null);

           while (cursor.moveToNext()){
               String password = cursor.getString(cursor.getColumnIndex(PasswordContract.PasswordEntry.COL_PASSWORD));
               String description = cursor.getString(cursor.getColumnIndex(PasswordContract.PasswordEntry.COL_PASS_DESC));
               tasklist.add(new PasswordItem(password, description));

if (mAdapter == null){
           mAdapter = new ArrayAdapter<>(this,
                   R.layout.item_password,
                   R.id.password,
                   R.id.password_description,
                   tasklist);
           mPasswordListView.setAdapter(mAdapter);
}else{
           mAdapter.clear();
           mAdapter.addAll(String.valueOf(tasklist));
           mAdapter.notifyDataSetChanged();
       }
       cursor.close();
       db.close();

我正在尝试弄清楚如何将我的自定义适配器连接到我的更新列表方法。

public class CustomAdapter extends ArrayAdapter<String>{

    private Context mContext;
    private List<String> mStringList;
    public CustomAdapter(Context context, List<String> mStringList) {
        super(context, R.layout.item_password);
        this.mContext = context;
        this.mStringList = new ArrayList<>();
        this.mStringList = mStringList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_password, parent, false);

        TextView your_first_text_view = (TextView) convertView.findViewById(R.id.password);
        TextView your_second_text_view = (TextView) convertView.findViewById(R.id.password_description);

        your_first_text_view.setText(mStringList.get(position));
        your_second_text_view.setText(mStringList.get(position));
        return convertView;
    }

最佳答案

您使用的是 POJO 类而不是字符串,因此在适配器和列表类型中进行所需的更改

用这个

ArrayList<PasswordItem> tasklist = new ArrayList<>(); 

代替

 ArrayList<String> tasklist = new ArrayList<>();

加上mAdapter应该是CustomAdapter类型所以

将其声明为 CustomAdapter mAdapter 并稍后将其初始化为

mAdapter = new CustomAdapter(this, R.layout.item_password ,tasklist);

在你的适配器中

public class CustomAdapter extends ArrayAdapter<PasswordItem>{
//                                              ^^^^^^^
    private Context mContext;
    private List<PasswordItem> mStringList;
    //            ^^^^^^^
    public CustomAdapter(Context context, int resource , List<PasswordItem> mStringList) {
        super(context, resource , mStringList);
        this.mContext = context;
        //this.mStringList = new ArrayList<>(); not required 
        this.mStringList = mStringList;
    }

    public updateList(List<PasswordItem> mStringList){
         this.mStringList = mStringList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_password, parent, false);

        TextView your_first_text_view = (TextView) convertView.findViewById(R.id.password);
        TextView your_second_text_view = (TextView) convertView.findViewById(R.id.password_description);

        your_first_text_view.setText(mStringList.get(position).password);
       //  use your getter or field names of POJO class
        your_second_text_view.setText(mStringList.get(position).description);
        return convertView;
    }

并替换这个 mAdapter.addAll(String.valueOf(tasklist)); 所以你的代码将是

if (mAdapter == null){
       mAdapter = new CustomAdapter(this,
               tasklist);
       mPasswordListView.setAdapter(mAdapter);
}else{
       mAdapter.clear();
       mAdapter.updateList(tasklist);
       // pass the new list to adapter
       mAdapter.notifyDataSetChanged();
   }

关于android - 无法应用 Arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42646878/

相关文章:

java - 将 ArrayList 刷新为 ClickListener

forms - 如何通过表单中的索引访问 session ArrayList?

java - "for"函数对 ArrayList 的操作和异常 java.lang.ArrayIndexOutOfBoundsException

java - 使用扫描仪将大文件读入内存时内存不足

java - 文件在python中加密在android中解密

android - 以编程方式单击或触摸 recyclerview 中的项目

java - HashMap vs ArrayList 性能我是正确的

java - 搜索字符串列表(100 000 个字符串): How to search for x character if there is a %

android - Instant app 不会直接启动,先显示一个屏幕

java - 如何从 AsyncTask 返回一个值,该值在 doInBackground 中进行另一个异步调用