gpt4 book ai didi

android - 无法应用 Arraylist

转载 作者:行者123 更新时间:2023-11-29 15:39:48 26 4
gpt4 key购买 nike

我正在尝试获取密码描述文本输入以填充我的应用程序的数组 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com