I have a 'ListView' with one button. When the user clicks on the button I added the selected position value in arraylist.But second time when user click's on the same button i want to remove this button entry from arraylist how to do this.This is my code snipet
@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;
}
Guessing by your question, I did solution like below.
Initialize ArrayList :
ArrayList<String> selectedString = new ArrayList<>();
Now onClick :
if(selectedString.contains(al.get(position))){
selectedString.remove(al.get(position));
}else {
selectedString.add(al.get(position));
}
Hope this will make sense.
You can use.
boolean pressedFlag = true;
ArrayList<String> list = new ArrayList<String>();
Now inside onClick
if(pressedflag){
list.add(arrayList.get(position));
pressedFlag = false;
}else{
list.remove(arrayList.get(position));
pressedFlag = true;
}
Hope this will help...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.