In an android application I am trying to delete a ListView item which involves the use of SharedPreferences. The problem is that on Press and hold a list item and after confirm deletion through Dialog box, every time last item seems to be deleted though selected item gets removed from the list. Because after coming back to the list fragment, appropriate list is shown.
Please help, I have been trying to fix it since months without success. Many Thanks!
Adpater Class:
public class DuasListAdapter extends ArrayAdapter<String>{
// class variables
private Context context;// to save context
private List<String> duas;// to save list of stores
LayoutInflater inflater;// so save layout inflater for the view
public DuasListAdapter(Context ctx, List<String> duasList) {
super(ctx, R.layout.adapter_list_duas, duasList);
context = ctx;// save context
duas = duasList;// save list of stores
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// save inflater layout
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
// if view is not loaded
if (!(convertView instanceof View)) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.adapter_list_duas, parent, false);
// load the view from holder class
holder.background = (LinearLayout) convertView.findViewById(R.id.ll_duas_list);
holder.iv_duatype = (ImageView) convertView.findViewById(R.id.iv_duatype);
holder.tv_dua_arabic = (TextView) convertView.findViewById(R.id.tv_dua_arabic);
holder.tv_dua_no = (TextView) convertView.findViewById(R.id.dua_no);
//holder = new ViewHolder(convertView);
// set the tag for future use
convertView.setTag(holder);
}
// if view is loaded
else{
// get view from tag
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
final View view = v;
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:{
ViewHolder holder = (ViewHolder) view.getTag();
int index = holder.duaIndex;
((MainActivity) context).removeItemFromList(index);
// ((MainActivity) context).loadDuasListFragment();
break;
}
case DialogInterface.BUTTON_NEGATIVE:
// No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Delete Dua from Favorites?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
return false;
}
});
return convertView;
}
static class ViewHolder {
int duaIndex;
LinearLayout background;// background to display color for read and unread messages
ImageView iv_duatype;
TextView tv_dua_arabic;// title of message
//TextView tv_dua_ref;// message by and message created on
TextView tv_dua_no;
}
}
Fragment Class:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
context = inflater.getContext();
View view = inflater.inflate(R.layout.fragment_list_duas, container, false);
// TODO Auto-generated method stub
return view;
}
/** method removing the accurate item form list, checked by debugging**/
public void removeItemFromList(int index) {
String verses = new SharedPreferencesSupplication().read(SingletonClass.keyListOfVerses, "a1");
String[] versesList = verses.split(",");
String identifier = versesList[index];
new FavoriteDuas().removeDua(identifier);
updatedData();
}
public void updatedData() {
boolean keyIsFavSel = new SharedPreferencesSupplication().read(SingletonClass.keyIsFavSelected, false);
if (keyIsFavSel)
new SharedPreferencesSupplication().save(SingletonClass.keyListOfVerses, new SharedPreferencesSupplication().read(SingletonClass.keyFavVerses, "a1"));
String verses = new SharedPreferencesSupplication().read(SingletonClass.keyListOfVerses, "a1");
String[] versesList = verses.split(",");
duas = new ArrayList<String>();
for (int i = 0; i < versesList.length; i++) {
if (versesList[i].length() > 0)
duas.add(versesList[i]);
}
duasAdapter.clear();
if (duas.size() > 0) {
for (String object : duas) {
duasAdapter.insert(object, duasAdapter.getCount());
}
}
duasAdapter.notifyDataSetChanged();
}
0 comments:
Post a Comment