Reputation: 27
I'm developing an app that allows a user to add properties (as in real houses) to their profile in a realtime database and then display those same houses back in the form of a ListView. Ignore any other property details such as size, price etc. I'm only focusing on putting its name into the listview.
Here is the code:
public class agent_home extends AppCompatActivity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agent_home);
loadProperties();
lv = (ListView) findViewById(R.id.lvProperties);
// ListView Item Click Listener
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
//int itemPosition = position;
// ListView Clicked item value
//String itemValue = (String) lv.getItemAtPosition(position);
//EditText tvCategory = (EditText) findViewById(R.id.tvCategory);
//tvCategory.setText(itemValue);
//InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
});
}
public void loadProps(View view) {
loadProperties();
}
private void loadProperties() {
Toast.makeText(getApplicationContext(),"loadProp run",Toast.LENGTH_SHORT).show();
List<String> propList = new ArrayList<String>();
lv = (ListView) findViewById(R.id.lvProperties);
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference propRef = rootRef.child("Properties");
propRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String answer = ds.getKey();
DatabaseReference agentRef = propRef.child(answer).child("agent");
agentRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (!task.isSuccessful()) {
Log.e("firebase", "Error getting data", task.getException());
}
else {
if (task.getResult().getValue().equals(globals.loggedUser)) {
propList.add(answer);
}
else {
Toast.makeText(getApplicationContext(),"Did not populate list",Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
else {
Toast.makeText(getApplicationContext(),"Doesn't exist",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, propList);
lv.setAdapter(arrayAdapter);
}
public void addProperty(View view) {
Intent intent = new Intent(this, add_property.class);
startActivity(intent);
}
public void createCategory(View view) {
Intent intent = new Intent(this, add_category.class);
startActivity(intent);
}
public void goToLogin(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public void goToGoals(View view) {
Intent intent = new Intent(this, goals.class);
startActivity(intent);
}
}
Here is the database structure:
The issue that I'm having as that for some reason the data doesn't load into the listview on the first instance of the Activity. The only way I can get the ListView to show the data is if I logout of the app and login in again after which the ListView shows the correct output. The method and code clearly works as I do get the correct output after the second login, however, I ca't seem to figure out why it wont load the first time. My first instinct is that it was a performance issue as I'm using the Android Virtual Device.
Upvotes: 0
Views: 46
Reputation: 3747
Try to initialize adapter before loading data:
List<String> propList = new ArrayList<String>();
lv = (ListView) findViewById(R.id.lvProperties);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, propList);
lv.setAdapter(arrayAdapter);
After that, when you load data add it to adapter instead of list:
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (!task.isSuccessful()) {
Log.e("firebase", "Error getting data", task.getException());
} else {
if (task.getResult().getValue().equals(globals.loggedUser)) {
arrayAdapter.add(answer);
} else {
Toast.makeText(getApplicationContext(),"Did not populate
list",Toast.LENGTH_SHORT).show();
}
}
ArrayAdapter handles notifying list by himself.
Or you can add data to list, and then call arrayAdapter.notifyDataSetChanged()
after every insert/delete
Upvotes: 1