gpt4 book ai didi

java - android中 ListView 中的重复值

转载 作者:行者123 更新时间:2023-12-01 18:14:46 25 4
gpt4 key购买 nike

我将我的数据库值提取到自定义 ListView,但每当从我的 MainActivity 转到我的 ExistingPatient Activity 类时,它都会在 Listview 中创建我的数据的重复值。我的 MainActivity 仅包含一个按钮,在该按钮中,我使用 Intent 转到 ExistingPatient Activity 。

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<Patientinfo> {
private Context context;
ArrayList<Patientinfo> info;

public list_adapter(Context context, ArrayList<Patientinfo> 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;
}

}

存在病人.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 时有帮助,我认为这里有同样的问题

关于java - android中 ListView 中的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60397095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com