(in this case I just tried to get the user name .. what happens is just load the name of a user, or just the last one in the listview)
EDIT .. AFTER SOME TESTS I HAVE NOTICED THAT, MY CODE ONLY UPDATES THE LAST ITEM OF LISTVIEW, BUT WITH THE VALUE THAT IT SHOULD GO TO THE FIRST ITEM ?? DOES NOT MEET ME
follow the codes
1.Tree 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. ListView Code
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;
}
}