无法应用Arraylist

时间:2017-03-07 11:16:37

标签: android arraylist

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

enter image description here

不幸的是,我的部分时间(cursor.movetoNext())说我的字符串不能应用于arraylist。我正在尝试确定在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();

我正在尝试弄清楚如何将我的customadapter连接到我的更新列表方法。

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

2 个答案:

答案 0 :(得分:3)

您使用的是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();
   }

答案 1 :(得分:1)

将项添加到arraylist时,它们需要与定义的类型相同。您使用类型字符串定义了arraylist,但是为其添加了一个PasswordItem对象。 更改arraylist类型以保留PasswordItem:

ArrayList<PasswordItem> tasklist