android中listview中的重复值

来源:爱站网时间:2021-09-16编辑:网友分享

问题描述


Am将我的数据库值提取到自定义ListView,但是每当从MainActivity转到我的ExistingPatient活动类时,它都会在Listview中创建我的数据的重复值。我的MainActivity仅包含一个按钮,在该按钮中,我曾打算去进行[[ExistingPatient活动。

list_adapter.java

package 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 { private Context context; ArrayList info; public list_adapter(Context context, ArrayList info) { super(context,R.layout.cutomlist,info); this.context = context; this.info = info; } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.cutomlist, parent, false); TextView tvname = convertView.findViewById(R.id.tv_Cname); TextView tvphone = convertView.findViewById(R.id.tv_Cphone); tvname.setText(info.get(position).getName()); tvphone.setText(info.get(position).getPhone()); return convertView; } }

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时,它对我有帮助,我认为这里是同样的问题

上一篇:TESTNG |从可运行的jar内的主要方法调用testng.xml文件

下一篇:一般异常:使用restTemplate时连接超时

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载