gpt4 book ai didi

java - 如何在数组列表中添加选定的值并从数组列表中删除未选定的值

转载 作者:行者123 更新时间:2023-12-02 03:56:48 25 4
gpt4 key购买 nike

我有一个带有一个按钮的“ListView”。当用户单击按钮时,我在数组列表中添加了选定的位置值。但是当用户第二次单击同一个按钮时,我想从数组列表中删除此按钮条目如何执行此操作。这是我的代码 fragment

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.single_row, null);


}
TextView msg = (TextView) convertView.findViewById(R.id.tv_wishdata);
msg.setText(al.get(position));
ArrayList arrayList = new ArrayList();

final ArrayList list = new ArrayList<String>();
final ImageView iv = (ImageView) convertView.findViewById(R.id.iv_myImageview);
final ListView lis = (ListView) convertView.findViewById(R.id.listView);
Boolean click = true;
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<String> listOfFavoritePhrases = new ArrayList<String>();
//check boolean value
if (click) {
iv.setBackgroundResource(R.drawable.thirdwish);
/*TinyDB tinyDB = new TinyDB(context);
tinyDB.putString("msg", al .get(position));
*/ // al.add(al.get(position))

temp.add(al.get(position));

Log.d("ARRAY DATA", "" + temp);
Log.d("ARRAY SIZE", "" + temp.size());

/*ContentValues contentValues = new ContentValues();
contentValues.put("item",al.get(position));
Log.d("Contentvalues",""+contentValues.toString());
db.insert("favorite",null,contentValues);*/

sp = context.getSharedPreferences("mypref",Context.MODE_APPEND);
SharedPreferences.Editor editor = sp.edit();
editor.putString("phrases", TextUtils.join(",", temp.toArray()));
Log.d("Shared Data", "" + sp.getString("phrases", ""));
editor.commit();
click = false;

}
else {
click = true;
temp.remove(al.get(position));
iv.setBackgroundResource(R.drawable.onewish);
// db.rawQuery("delete from favorite where item like'%" + al.get(position) + "%'", null);
Log.d("QUERY", "" + al.get(position));
// al.remove(position);
notifyDataSetChanged();
}
// Toast.makeText(context, "imageview clicked", Toast.LENGTH_LONG).show();
}
});
return convertView;
}

最佳答案

根据你的问题猜测,我做了如下的解决方案。

初始化ArrayList:

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

现在onClick:

if(selectedString.contains(al.get(position))){
selectedString.remove(al.get(position));
}else {
selectedString.add(al.get(position));
}

希望这有意义。

关于java - 如何在数组列表中添加选定的值并从数组列表中删除未选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35369392/

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