2017-03-07 61 views
1

我想獲取密碼描述文本輸入來填充我的應用程序的數組視圖。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; 
    } 

回答

3

您使用POJO類不串這樣做適配器所需的更改,以及在你的列表類型

使用本

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

而不是

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

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

Pavneet,幾乎在那裏。我在updatelist方法中添加了if和else語句。我已更新我的帖子以顯示他們。 arrayadapter說明不能推斷參數。你知道這是什麼原因嗎? –

+0

它仍然說arrayadapter不能應用。 –

+0

等我拿到了,讓我更新 –

1

將項目添加到數組列表時,它們需要與定義的類型相同。您使用字符串類型定義了ArrayList,但添加了一個PasswordItem對象。 更改的ArrayList類持有PasswordItem代替:

ArrayList<PasswordItem> tasklist