0
Basically my project resembles a social network, using the firebase Restaltime database, but I came across a problem, I created a custom listview and my idea was... Since I had 2 children >Users and >Posts, in the Child post there is a user email node, that is, within the listview I would capture the user/avatar name (using the field/email I had in the Child post) ... but what happened was that.. it didn’t work, I tried several things but nothing worked, if someone could help me... I would be EXTREMELY happy
(in this case I simply tried to take the name of the user.. what happens is only carries the name of a user, or only the last one of the listview)
EDIT. AFTER SOME TESTS I NOTICED THAT, MY CODE ONLY UPDATES THE LAST ITEM OF LISTVIEW, BUT WITH THE VALUE THAT SHOULD GO TO THE FIRST ITEM? MAKES NO SENSE TO ME
follows the codes
1.Screen that calls the Listview
package com.gabriel.arhur.team.kindness02project;
public class TelaInicial extends AppCompatActivity {
FloatingActionButton floatButton;
ListView listV_dados;
private List<Post> listPost = new ArrayList<Post>();
private PostTI_ListAdapter arrayAdapterPost;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
Post postSelect = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tela_inicial);
getSupportActionBar().setTitle("Tela Inicial");
floatButton = findViewById(R.id.floatboton);
listV_dados = findViewById(R.id.List_posts);
iniciaFirebase();
eventoFirebase();
eventoonclick();
final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
mSwipeRefreshLayout.setColorSchemeColors(
Color.RED, Color.BLUE, Color.GREEN
);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
eventoFirebase();
mSwipeRefreshLayout.setRefreshing(false);
Snackbar snackbar = Snackbar
.make(mSwipeRefreshLayout, "Recarregado", Snackbar.LENGTH_LONG);
snackbar.show();
}
},3000);
}
});
}
private void iniciaFirebase() {
FirebaseApp.initializeApp(TelaInicial.this);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference();
}
private void eventoFirebase() {
databaseReference.child("Post").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listPost.clear();
for (DataSnapshot objSnapshot : dataSnapshot.getChildren()) {
Post p = objSnapshot.getValue(Post.class);
listPost.add(p);
}
arrayAdapterPost = new PostTI_ListAdapter(TelaInicial.this, R.layout.activity_post_ti__list_adapter, listPost);
listV_dados.setAdapter(arrayAdapterPost);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void eventoonclick() {
floatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(TelaInicial.this, NovosPosts.class);
startActivity(i);
}
});
listV_dados.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
postSelect = (Post) parent.getItemAtPosition(position);
alert(postSelect.getUid());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_pesquisa) {
alert("pesquisa");
} else if (id == R.id.menu_Perfil) {
alert("perfil");
Intent i = new Intent(TelaInicial.this, PerfilUser.class);
startActivity(i);
}
return true;
}
private void alert(String msg) {
Toast.makeText(TelaInicial.this, msg, Toast.LENGTH_LONG).show();
}
}
2.Code of Listview
public class PostTI_ListAdapter extends ArrayAdapter<Post> {
private Activity context;
private int resource;
private List<Post> list;
DatabaseReference databaseReference;
TextView textDescricao,textTitulopost,textLocalização,textnomeperfil;
ImageView PostImage;
String nome;
public PostTI_ListAdapter(@NonNull Activity context, @LayoutRes int resource, @NonNull List<Post> objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
list = objects;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View v = inflater.inflate(resource, null);
textDescricao= v.findViewById(R.id.textDescrição);
textTitulopost = v.findViewById(R.id.textTitulo);
textLocalização = v.findViewById(R.id.textlocal);
PostImage = v.findViewById(R.id.imgPost);
textnomeperfil = v.findViewById(R.id.textnomeperfil);
textDescricao.setText(list.get(position).getDescricao_post());
textTitulopost.setText(list.get(position).getTitulo());
Glide.with(context).load(list.get(position).getUrl_image()).into(PostImage);
textLocalização.setText(((list.get(position).getEstado()))+"-"+((list.get(position).getCidade())));
//Pesquisa... porem nao deu certo
String email= list.get(position).getCriador();
notifyDataSetChanged();
DatabaseReference raiz = FirebaseDatabase.getInstance().getReference();
Query query =raiz.child("Usuario").orderByChild("email").equalTo(email);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot objSnapshot:dataSnapshot.getChildren()){
textnomeperfil.setText(objSnapshot.child("nome").getValue().toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return v;
}
}
I believe the problem lies in the structure of your database, add in the question an example data json
– Costamilam
updated with a Screenshot of the database, and information in the "introduction" of the post
– Arthur Ramos