从列表视图中删除旧项目,并在按钮单击时使用新项目进行更新

时间:2017-02-02 22:47:27

标签: android listview firebase android-edittext firebase-realtime-database

我正在从firebase中检索数据,并根据EditText中的值显示在列表视图中。但是当我更改EditText中的值时,它必须从listView中删除以前的值并显示新值。

我尝试了很多方法删除以前的值但是显示了两次新值。我不希望它只需要显示一次值。

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
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;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AnonymousBloodBank extends AppCompatActivity {

private ListView lv;
AlertDialogManager alert = new AlertDialogManager();
private ProgressDialog progressDialog;

EditText editTextBloodGroup;
//private AutoCompleteTextView acTextView;
Button btnSearch;
final int[] count = {0};
List<String> list = new ArrayList<String>();
final ArrayList<String> nameData = new ArrayList<>();
final ArrayList<String> bloodInfo = new ArrayList<>();
final ArrayList<String> contactInfo = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_anonymous_blood_bank);

    editTextBloodGroup = (EditText) findViewById(R.id.editBloodGroup);

    lv = (ListView) findViewById(R.id.lvBlood);
    btnSearch = (Button) findViewById(R.id.btnSearch);


    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String contact = contactInfo.get(position);
            try {
                String uri = "tel:" + contact;
                Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
                startActivity(callIntent);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });


    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference myRef = database.getReference().child("clients");
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(final DataSnapshot dataSnapshot) {
            btnSearch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    collectionInfo((Map<String, Object>) dataSnapshot.getValue());
                }
            });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

    });
}

//Retrieve location from database
public void collectionInfo(Map<String, Object> users)
{
    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
    for(int i=0; i<arrayAdapter.getCount(); i++)
    {
        list.remove(i);

    }
    arrayAdapter.notifyDataSetChanged();
    arrayAdapter.notifyDataSetInvalidated();

    for (Map.Entry<String, Object> entry : users.entrySet()) {
        //Get user map
        Map singleUser = (Map) entry.getValue();
        //Get phone field and append to list
        bloodInfo.add((String) singleUser.get("bloodgroup"));
        contactInfo.add((String) singleUser.get("contact"));
        nameData.add((String) singleUser.get("name"));
        count[0]++;
    }

    for (int i = 0; i < count[0]; i++) {
        if(bloodInfo.get(i).equals(editTextBloodGroup.getText().toString()))
        {
            try{
                list.add(nameData.get(i));
            }catch (Exception e) {
                alert.showAlertDialog(AnonymousBloodBank.this, "No blood group",
                        "Please enter blood group", false);
            }
        }

    }
    lv.setAdapter(arrayAdapter);

   }
 }

The output screenshot is given. When I click on search button the values for eg: theakshay naik must be deleted and new value must instead of it

1 个答案:

答案 0 :(得分:0)

您没有清除其他列表:nameData,bloodInfo,contactInfo。而count [0]总是在增加。没有任何部分可以将其值初始化为零或清除它们。尝试在collectionInfo方法的开头执行此操作:

nameData.clear();
bloodInfo.clear();
contactInfor.clear();
count[0] = 0;