我有一个&#39; ListView&#39;只需一个按钮。当用户点击按钮时,我在arraylist中添加了所选的位置值。但是当用户点击同一按钮时,我想从arraylist中删除此按钮条目,如何执行此操作。这是我的代码snipet < / p>
@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;
}
答案 0 :(得分:2)
根据你的问题猜测,我做了如下的解决方案。
初始化 ArrayList :
ArrayList<String> selectedString = new ArrayList<>();
现在 onClick :
if(selectedString.contains(al.get(position))){
selectedString.remove(al.get(position));
}else {
selectedString.add(al.get(position));
}
希望这是有道理的。
答案 1 :(得分:0)
你可以使用。
C
2
42
3
2
71
现在在onClick
boolean pressedFlag = true;
ArrayList<String> list = new ArrayList<String>();
希望这会有所帮助......