在我的自定义adadpter(RequestAdapter)中,我动态删除元素。我这样做是通过从我通过构造函数的对象的ArrayList中删除对象。一旦我在列表上使用remove(),我就调用notifyDataSetChanged()。
问题是这个 - 从原始列表中删除了项目,但除了已删除项目之外的所有项目都附加到listView上。 (如果我从列表[A B C]中删除A,最终结果是一个如下所示的列表 - [B C B C]。此外,如果我从这个列表中删除B,我得到这个 - [C B C C]
这是我使用的代码:
private HashMap<String,Object> tournamentStatus = new HashMap<>();
public RequestAdapter(Activity context, ArrayList<RequestDetails> requests){
super(context,0,requests);
this.mRequestsList = requests;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Log.d("Adapter getView()","Invoked");
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.request_list_item, parent, false);
}
final RequestDetails retrievedRequest = getItem(position);
final boolean isOCRequest;
final boolean isParticipantRequest;
final TextView tournamentName = (TextView) listItemView.findViewById(R.id.request_textview_tournament_name);
final TextView userName = (TextView) listItemView.findViewById(R.id.request_textview_user_name);
final TextView requestType = (TextView) listItemView.findViewById(R.id.request_textview_request_type);
final TextView sport = (TextView) listItemView.findViewById(R.id.request_textview_sport);
final TextView userFaculty = (TextView) listItemView.findViewById(R.id.request_textview_user_faculty);
final TextView userYear = (TextView) listItemView.findViewById(R.id.request_textview_user_year);
if(retrievedRequest.isParticipantRequest()) {
requestType.setText("Request Type: Participant");
sport.setText("Sport: "+retrievedRequest.getSport());
}
else {
requestType.setText("Request Type: Organizing Committee");
sport.setVisibility(View.GONE);
}
tournamentRef = FirebaseDatabase.getInstance().getReference().child("Tournaments").child(retrievedRequest.getTournamentId());
tournamentRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
TournamentInformation tournamentInformation = dataSnapshot.getValue(TournamentInformation.class);
tournamentName.setText(tournamentInformation.getTournamentName());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
userRef = FirebaseDatabase.getInstance().getReference().child("Users").child(retrievedRequest.getUserId());
userRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
UserInformation user = dataSnapshot.getValue(UserInformation.class);
userName.setText("User Name: "+user.getName());
userFaculty.setText("Faculty: "+user.getFaculty());
userYear.setText("Year: "+Integer.toString(user.getYear()));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Button mAcceptButton = (Button) listItemView.findViewById(R.id.accept_request_button);
Button mDenyButton = (Button) listItemView.findViewById(R.id.deny_request_button);
mAcceptButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(retrievedRequest.isParticipantRequest()){
tournamentStatus.put("isOrganizing",false);
tournamentStatus.put("isParticipating",true);
userRef.child("sports").child(retrievedRequest.getTournamentId()).child(retrievedRequest.getSport()).setValue(true);
}
else if(retrievedRequest.isOCRequest()){
tournamentStatus.put("isOrganizing",true);
tournamentStatus.put("isParticipating",false);
}
Toast.makeText(getContext(),"Requested Accepted",Toast.LENGTH_SHORT).show();
userRef.child("tournamentStatuses").child(retrievedRequest.getTournamentId()).updateChildren(tournamentStatus);
DatabaseReference requestsRef = FirebaseDatabase.getInstance().getReference().child("Requests");
requestsRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for(DataSnapshot requestLooper : dataSnapshot.getChildren()){
RequestDetails loopedRequest = requestLooper.getValue(RequestDetails.class);
if(loopedRequest.equals(retrievedRequest)){
requestLooper.getRef().removeValue();
Log.d("Removing at",requestLooper.getRef().toString());
Log.d("requestList",mRequestsList.toString());
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mRequestsList.remove(retrievedRequest);
notifyDataSetChanged();
}
});
mDenyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseReference requestsRef = FirebaseDatabase.getInstance().getReference().child("Requests");
requestsRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for(DataSnapshot requestLooper : dataSnapshot.getChildren()){
RequestDetails requestDetails = requestLooper.getValue(RequestDetails.class);
if(requestDetails.equals(retrievedRequest)){
requestLooper.getRef().removeValue();
Log.d("Removing at",requestLooper.getRef().toString());
Log.d("requestList",mRequestsList.toString());
Toast.makeText(getContext(),"Requested Denied",Toast.LENGTH_SHORT).show();
break;
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mRequestsList.remove(retrievedRequest);
notifyDataSetChanged();
}
});
return listItemView;
}
我做了哪些更改,以便列表不重复?
更新我添加了整个适配器代码,但大部分内容都涉及设置textViews。我只在Accept和Deny按钮的clickListeners中删除
答案 0 :(得分:0)
如果您不想在集合中重复,
List<String> al = new ArrayList<>();
//包括重复列表
private void removeDuplicate(ArrayList al)
{
HashSet<String> hs = new HashSet<>();
hs.addAll(al);
al.clear();
al.addAll(hs);
}
//在调用此方法后更改添加和删除列表
removeDuplicate(人);
答案 1 :(得分:0)
我无法正确理解您的适配器。但我可以给你一个建议,因为我遇到了这个问题,我找到了一条出路。不确定它是否适用于您的问题,但您可以尝试: 从ArrayList中删除项目后,您可以编写以下代码:
RequestAdapter.this.remove(的getItem(位置));
此代码将从listView中删除已删除的项目。希望它会奏效。感谢。