在我的android应用程序中,我使用扩展ArrayAdapter的适配器在片段中有一个列表。每当您持有行项目很长时间,您可以通过在警报对话框上单击是来删除该行。但是,当您单击是时,似乎删除了最后一行。当您关闭该片段并返回该片段时,视图就可以删除正确的行。经过这么多天的调试后,我无法解决这个问题。任何人都可以搞清楚什么是错的? 提前谢谢。
AdapterClass
public class DuasListAdapter extends ArrayAdapter<String> {
// class variables
private final Context context;// to save context
private final 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
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// if view is not loaded
if (!(convertView instanceof View)) {
convertView = inflater.inflate(R.layout.adapter_list_duas, parent, false);
// load the view from holder class
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();
holder.duaIndex = position;
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);
Toast.makeText(view.getContext(), "Deleted", Toast.LENGTH_LONG).show();
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;
}
ListFragment
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();
}
@Override
public void onResume() {
lvDuaas.setAdapter(duasAdapter);
updatedData();
super.onResume();
}
@Override
public void onPause() {
// TODO Auto-generated method stub
lvDuaas.setAdapter(duasAdapter);
updatedData();
super.onPause();
}
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(",");
ArrayList<String> 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();
}
删除-方法
public void removeDua(String duaIdentifier) {
String verses = new SharedPreferencesSupplication().read(SingletonClass.keyFavVerses, "");
String[] versesList = verses.split(",");
int indexFound = -1;
for (int i = 0; i < versesList.length; i++) {
if (versesList[i].equals(duaIdentifier)) {
indexFound = i;
break;
}
}
if (indexFound >= 0) {
verses = "";
for (int i = 0; i < versesList.length; i++) {
if (indexFound != i) {
if (verses.isEmpty())
verses = versesList[i];
else
verses = verses + "," + versesList[i];
}
}
new SharedPreferencesSupplication().save(SingletonClass.keyFavVerses, verses);
}
}
答案 0 :(得分:0)
您可以尝试重新实例化duasAdapter
duasAdapter = new DuasListAdapter(your_context_here, duas);
而不是
duasAdapter.clear();
if (duas.size() > 0) {
for (String object : duas) {
duasAdapter.insert(object, duasAdapter.getCount());
}
}
duasAdapter.notifyDataSetChanged();
答案 1 :(得分:0)
您在视图上使用setOnLongClickListener(),而是使用setOnItemLongClickListener()。