android中listview中的重复值
问题描述
Am将我的数据库值提取到自定义ListView,但是每当从MainActivity转到我的ExistingPatient活动类时,它都会在Listview中创建我的数据的重复值。我的MainActivity仅包含一个按钮,在该按钮中,我曾打算去进行[[ExistingPatient活动。
list_adapter.javapackage com.example.voiceprescription;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class list_adapter extends ArrayAdapter
Exsistin Patient.java
package com.example.voiceprescription; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; public class Exsistinpatient extends AppCompatActivity { ListView list; DatabaseReference reff; FirebaseDatabase database; list_adapter adapter; Patientinfo patientinfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_exsistinpatient); Intent intent=new Intent(); list=findViewById(R.id.list); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Intent intent=new Intent(Exsistinpatient.this,Editinfo.class); intent.putExtra("index",position); startActivityForResult(intent,0); } }); patientinfo=new Patientinfo(); database=FirebaseDatabase.getInstance(); reff=database.getReference("Patients"); reff.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { for(DataSnapshot data:dataSnapshot.getChildren()){ patientinfo=data.getValue(Patientinfo.class); publicarray.info.add(patientinfo); } adapter=new list_adapter(Exsistinpatient.this,publicarray.info); list.setAdapter(adapter); } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==0){ adapter.notifyDataSetChanged(); } } }
思路:
尝试在每次启动时清除列表,当我使用RecyclerView时,它对我有帮助,我认为这里是同样的问题