Created
January 3, 2019 08:47
-
-
Save doilio/fdb88b3ab0bfb411045ab4d5298c6d4f to your computer and use it in GitHub Desktop.
Error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A simple {@link Fragment} subclass. | |
*/ | |
public class ConversasFragment extends Fragment { | |
private RecyclerView recyclerViewConversas; | |
private List<Conversa> listaConversas = new ArrayList<>(); | |
private ConversasAdapter adapter; | |
private DatabaseReference database; | |
private DatabaseReference conversasRef; | |
private ChildEventListener childEventListenerConversas; | |
//private ValueEventListener valueEventListenerConversas; | |
public ConversasFragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
View view = inflater.inflate(R.layout.fragment_conversas, container, false); | |
//Configuracoes Iniciais | |
recyclerViewConversas = view.findViewById(R.id.conversas_recyclerview); | |
String identificadorUsuario = UsuarioFirebase.getIdentificadorUsuario(); | |
database = ConfiguracaoFirebase.getFirebaseDatabase(); | |
conversasRef = database.child("conversas") | |
.child(identificadorUsuario); | |
// Adapter | |
adapter = new ConversasAdapter(listaConversas, getActivity()); | |
//RecyclerView | |
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity()); | |
recyclerViewConversas.setLayoutManager(layoutManager); | |
recyclerViewConversas.setHasFixedSize(true); | |
recyclerViewConversas.setAdapter(adapter); | |
// Configurar Cliques | |
recyclerViewConversas.addOnItemTouchListener( | |
new RecyclerItemClickListener( | |
getActivity(), | |
recyclerViewConversas, | |
new RecyclerItemClickListener.OnItemClickListener() { | |
@Override | |
public void onItemClick(View view, int position) { | |
Conversa conversaSelecionada = listaConversas.get(position); | |
Intent intent = new Intent(getActivity(), ChatActivity.class); | |
intent.putExtra("chatContacto", conversaSelecionada.getUsuarioExibicao()); | |
startActivity(intent); | |
} | |
@Override | |
public void onLongItemClick(View view, int position) { | |
} | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
} | |
})); | |
return view; | |
} | |
@Override | |
public void onStart() { | |
super.onStart(); | |
Log.d("tamanho Start Antes", String.valueOf(listaConversas.size())); | |
listaConversas.clear(); | |
recuperarConversas(); | |
Log.d("tamanho Start Dep.", String.valueOf(listaConversas.size())); | |
} | |
@Override | |
public void onStop() { | |
super.onStop(); | |
//conversasRef.removeEventListener(childEventListenerConversas); | |
conversasRef.removeEventListener(childEventListenerConversas); | |
} | |
public void pesquisarConversa(String texto) { | |
List<Conversa> listaConversaBusca = new ArrayList<>(); | |
for (Conversa conversa : listaConversas) { | |
Log.d("tamanho pesq.", String.valueOf(listaConversas.size())); | |
String nome = conversa.getUsuarioExibicao().getNome(); | |
String ultimaMsg = conversa.getUltimaMensagem(); | |
if (nome.contains(texto) || ultimaMsg.contains(texto)) { | |
listaConversaBusca.add(conversa); | |
} | |
} | |
adapter = new ConversasAdapter(listaConversaBusca, getActivity()); | |
recyclerViewConversas.setAdapter(adapter); | |
adapter.notifyDataSetChanged(); | |
} | |
public void recuperarConversas() { | |
childEventListenerConversas = conversasRef.addChildEventListener(new ChildEventListener() { | |
@Override | |
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { | |
//Recuperar conversas | |
Conversa conversa = dataSnapshot.getValue(Conversa.class); | |
listaConversas.add(conversa); | |
adapter.notifyDataSetChanged(); | |
Log.d("tamanho da lista", String.valueOf(listaConversas.size())); | |
} | |
@Override | |
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { | |
} | |
@Override | |
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) { | |
} | |
@Override | |
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { | |
} | |
@Override | |
public void onCancelled(@NonNull DatabaseError databaseError) { | |
} | |
}); | |
/* valueEventListenerConversas = conversasRef.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(@NonNull DataSnapshot dataSnapshot) { | |
for(DataSnapshot dados: dataSnapshot.getChildren()){ | |
Conversa conversa = dados.getValue(Conversa.class); | |
listaConversas.add(conversa); | |
} | |
adapter.notifyDataSetChanged(); | |
Log.d("tamanho recuperaV", String.valueOf(listaConversas.size())); | |
} | |
@Override | |
public void onCancelled(@NonNull DatabaseError databaseError) { | |
} | |
});*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference at com.example.dowy.chat.fragment.ConversasFragment.pesquisarConversa(ConversasFragment.java:133) at com.example.dowy.chat.activity.MainActivity$2.onQueryTextChange(MainActivity.java:82)