我有一个孩子eventListener
课程。这是由一些不同的活动召集的。但出于某种原因,当我离开一个活动,然后去下一个活动。我的listView
保留了侦听器中的先前数据,并添加了新的。
我试图在我的活动背面添加removeListener
,但结果仍然相同。
这是我的听众课程
public class aimeEnentListener {
private ListView mlv;
private static ArrayList<String> AppNameList = new ArrayList<>();
private ArrayList<String> urlList = new ArrayList<>();
private static ArrayList<String> ApkList = new ArrayList<>();
private static ArrayList<String> Aboutapp = new ArrayList<>();
private static ArrayList<String> appImage = new ArrayList<>();
private static ArrayList<String> Downloadscount = new ArrayList<>();
private static ArrayList<String> DOWNLOADS = new ArrayList<>();
private Context mContext;
private dataListAdapter apkData;
public static String mCurrentPhotoPath;
private static String apkNames;
private Firebase mrootRef;
private String appname;
private String Apptype;
private String counter,url,apkname,about,appimg,tagname;
private com.firebase.client.ChildEventListener eventListener;
String tag;
public aimeEnentListener (ListView mlv,Context context,String AppType,Firebase mrootRef){
this.mContext = context;
this.mlv = mlv;
this.Apptype = AppType;
this.mrootRef = mrootRef;
}
public void setList() {
eventListener = new com.firebase.client.ChildEventListener() {
@Override
public void onChildAdded(com.firebase.client.DataSnapshot dataSnapshot, String s) {
appname = dataSnapshot.child("name").getValue(String.class);
counter = dataSnapshot.child("downloads").getValue(String.class);
url = dataSnapshot.child("url").getValue(String.class);
apkname = dataSnapshot.child("apk").getValue(String.class);
about = dataSnapshot.child("about").getValue(String.class);
appimg = dataSnapshot.child("image").getValue(String.class);
tagname = dataSnapshot.child("tag").getValue(String.class);
DOWNLOADS.add(tagname);
Downloadscount.add(counter);
AppNameList.add(appname);
urlList.add(url);
ApkList.add(apkname);
Aboutapp.add(about);
appImage.add(appimg);
final String[] arr = AppNameList.toArray(new String[AppNameList.size()]);
String[] arr1 = Aboutapp.toArray(new String[Aboutapp.size()]);
String[] arr2 = appImage.toArray(new String[appImage.size()]);
final String[] arr3 = Downloadscount.toArray(new String[Downloadscount.size()]);
apkData = new dataListAdapter(mContext, arr, arr1, arr2, arr3, mrootRef, Apptype);
mlv.setAdapter(apkData);
apkData.notifyDataSetChanged();
mlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
final Firebase ref = mrootRef.getRef();
tag = DOWNLOADS.get(i).toString();
apkNames = AppNameList.get(i).toString();
DownloadFileFromURL dl = new DownloadFileFromURL(mContext, apkNames, mCurrentPhotoPath);
dl.execute(urlList.get(i));
Count_System count = new Count_System(ref);
count.count();
App_DownLoadCounter(tag);
}
});
}
@Override
public void onChildChanged(com.firebase.client.DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.exists()) {
counter = dataSnapshot.child("downloads").getValue(String.class);
appname = dataSnapshot.child("name").getValue(String.class);
counter = dataSnapshot.child("downloads").getValue(String.class);
url = dataSnapshot.child("url").getValue(String.class);
apkname = dataSnapshot.child("apk").getValue(String.class);
about = dataSnapshot.child("about").getValue(String.class);
appimg = dataSnapshot.child("image").getValue(String.class);
tagname = dataSnapshot.child("tag").getValue(String.class);
apkData.notifyDataSetChanged();
}
}
@Override
public void onChildRemoved(com.firebase.client.DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(com.firebase.client.DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
};
mrootRef.child(Apptype).addChildEventListener(eventListener);
}
public void App_DownLoadCounter(String tag) {
this.tag = tag;
Firebase ref = mrootRef.getRef();
ref.child(Apptype).child(tag).child("downloads").runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
if (mutableData.getValue() == null) {
mutableData.setValue(0);
} else {
int count = mutableData.getValue(Integer.class);
mutableData.setValue(count + 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(FirebaseError firebaseError, boolean b, com.firebase.client.DataSnapshot dataSnapshot) {
}
});
}
public void removeListener(){
mrootRef.child(Apptype).removeEventListener(eventListener);
}
}
然后我使用
从每个活动中调用它 mlv = (ListView) findViewById(R.id.lvCastingapps);
listener = new aimeEnentListener(mlv, Casting_Apps.this, Apptype, mrootRef);
listener.setList();
我甚至尝试将removeListener
添加到onBackpress
@Override
public void onBackPressed(){
super.onBackPressed();
Intent i=new Intent(Intent.ACTION_MAIN);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
listener.removeListener();
}
编辑****
如果我制作一个侦听器类的精确副本并在其中使用它,那么在下一个上面使用上面的侦听器它正常工作。但我真的只想使用一个听众