Reputation: 133
I have the codes bellow which shows results from an array list ,but , it shows only the test result, I attached the same results to a text view to test and found that the results that I got from the query method does contain more than one and they had appeared in the text view, but the listview shows only first one . please find the codes below along with the picture attached.
//searchactivity.class//
package com.example.boc.search;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.example.boc.Interface.IMainActivity;
import com.example.boc.R;
import com.example.boc.main.phone_nombers_Activity;
import com.example.boc.models.Note;
import com.example.boc.models.Search;
import com.google.android.gms.tasks.OnCompleteListener;
import android.widget.ArrayAdapter;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
public class searchActivity extends phone_nombers_Activity implements
View.OnClickListener,
IMainActivity
{
private DocumentSnapshot documentSnapshot;
ListView listView;
public TextView resultTxt , userinput ;
private ArrayList<Search> mSearch = new ArrayList<>();
public FirebaseFirestore db = FirebaseFirestore.getInstance();
public Note note ;
public LinearLayout layout ;
private ArrayList<Note> mNotes = new ArrayList<>();
private DocumentReference noteRef = db.collection("notes").document();
private CollectionReference notesCollectionRef = db.collection("notes");
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_search );
final ListView listView = findViewById( R.id.listview4 );
final EditText userinput = findViewById( R.id.userInputtxt );
final Button findbutton = findViewById( R.id.findBtn );
final TextView resultTxt = findViewById( R.id.resultTxt );
mRecyclerView = findViewById(R.id.recycler_view);
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference notesCollectionRef = db
.collection("notes");
Query notesQuery = null;
if(documentSnapshot != null){
notesQuery = notesCollectionRef;
}
else{
notesQuery = notesCollectionRef
.orderBy("timestamp", Query.Direction.ASCENDING);
}
notesQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
String data = "";
for(final QueryDocumentSnapshot document: task.getResult()){
Note note = document.toObject(Note.class);
mNotes.add(note);
if( userinput !=null ) {
findbutton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
final String userinputString = userinput.getText().toString();
Note match = null;
String matcheddata ="";
for (Note note : mNotes) {
if (note.getTitle().contains(userinputString)) {
match = note;
String matchedtitle = match.getTitle();
String matchedcontent = match.getContent();
matcheddata += "هاتف:" + matchedcontent + "\nالاسم:" + matchedtitle + "\n\n";
}
if (match != null) {
ArrayList<String> namesList = new ArrayList<>();
resultTxt.setText( matcheddata );
namesList.clear();
ArrayAdapter<String>adapter = new ArrayAdapter<>( getApplicationContext(), android.R.layout.simple_selectable_list_item, namesList );
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
namesList.add(matcheddata);
// Found a match
//previewResultTextview.setOnClickListener( new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// String content = previewResultTextview.getText().toString();
// Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", contenttoparse, null));
// startActivity(intent);
// }
// } );
}
}
}
} );
}
else {
userinput.setError( "اسم الملف مطلوب" );
}
}
}
}
});
}
@Override
public void onStart () {
super.onStart();
}
}
//searchactivity.xml//
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".search.searchActivity">
<EditText
android:id="@+id/userInputtxt2"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:paddingTop="50dp"
android:textColor="@color/Black"
android:textSize="18sp" />
<EditText
android:id="@+id/userInputtxt"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:paddingTop="50dp"
android:textColor="@color/Black"
android:textSize="18sp" />
<Button
android:id="@+id/findBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="320dp"
android:layout_marginEnd="3dp"
android:text="find"
android:textSize="10sp" />
<ListView
android:id="@+id/listview4"
android:layout_width="398dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="55dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp">
</ListView>
<TextView
android:id="@+id/resultTxt"
android:layout_width="match_parent"
android:layout_height="203dp"
android:layout_marginTop="450dp"
android:background="@color/transparentGrey"
android:text="TextView" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Upvotes: 0
Views: 246
Reputation: 98
if (match != null) {
ArrayList<String> namesList = new ArrayList<>();
resultTxt.setText( matcheddata );
// namesList.clear(); // You don't need to clear it as you just created it above
namesList.add(matcheddata);
ArrayAdapter<String> adapter = new ArrayAdapter<>( getApplicationContext(), android.R.layout.simple_selectable_list_item, namesList );
listView.setAdapter(adapter);
}
Hi Amer Anajjem, can you try prepare the namesList before creating the adapter and use the latest namesList to create the adapter.
Edit:
I found that the problem is: matcheddata
is a string. When you call namesList.add(matcheddata);
, you added only 1 item in to the list. As the result, you only got 1 item in the list view. To prove that, you can try below:
if (match != null) {
ArrayList<String> namesList = new ArrayList<>();
resultTxt.setText( matcheddata );
namesList.clear();
namesList.add(matcheddata);
namesList.add(matcheddata);
ArrayAdapter<String> adapter = new ArrayAdapter<>( getApplicationContext(), android.R.layout.simple_selectable_list_item, namesList );
listView.setAdapter(adapter);
}
You should see 2 duplicated item in the listview.
Edit 2:
You can try this, but I did not test it.
package com.example.boc.search;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.example.boc.Interface.IMainActivity;
import com.example.boc.R;
import com.example.boc.main.phone_nombers_Activity;
import com.example.boc.models.Note;
import com.example.boc.models.Search;
import com.google.android.gms.tasks.OnCompleteListener;
import android.widget.ArrayAdapter;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
public class searchActivity extends phone_nombers_Activity implements
View.OnClickListener,
IMainActivity
{
private DocumentSnapshot documentSnapshot;
ListView listView;
public TextView resultTxt , userinput ;
private ArrayList<Search> mSearch = new ArrayList<>();
public FirebaseFirestore db = FirebaseFirestore.getInstance();
public Note note ;
public LinearLayout layout ;
private ArrayList<Note> mNotes = new ArrayList<>();
private DocumentReference noteRef = db.collection("notes").document();
private CollectionReference notesCollectionRef = db.collection("notes");
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_search );
final ListView listView = findViewById( R.id.listview4 );
final EditText userinput = findViewById( R.id.userInputtxt );
final Button findbutton = findViewById( R.id.findBtn );
final TextView resultTxt = findViewById( R.id.resultTxt );
mRecyclerView = findViewById(R.id.recycler_view);
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference notesCollectionRef = db
.collection("notes");
Query notesQuery = null;
if(documentSnapshot != null){
notesQuery = notesCollectionRef;
}
else{
notesQuery = notesCollectionRef
.orderBy("timestamp", Query.Direction.ASCENDING);
}
notesQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
String data = "";
for(final QueryDocumentSnapshot document: task.getResult()){
Note note = document.toObject(Note.class);
mNotes.add(note);
if( userinput !=null ) {
findbutton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
final String userinputString = userinput.getText().toString();
ArrayList<String> namesList = new ArrayList<>();
for (Note note : mNotes) {
if (note.getTitle().contains(userinputString)) {
String matchedtitle = note.getTitle();
String matchedcontent = note.getContent();
String matcheddata += "هاتف:" + matchedcontent + "\nالاسم:" + matchedtitle + "\n\n";
namesList.add(matcheddata);
}
ArrayAdapter<String> adapter = new ArrayAdapter<>( getApplicationContext(), android.R.layout.simple_selectable_list_item, namesList );
listView.setAdapter(adapter);
}
} );
}
else {
userinput.setError( "اسم الملف مطلوب" );
}
}
}
}
});
}
@Override
public void onStart () {
super.onStart();
}
}
Upvotes: 2
Reputation: 690
I think it's adapter notifies issue.
You are giving 0 size list to the adapter and immediately adapter is notified.
I think you should be notified the adapter after adding in the list. or You can add your data to your list before calling adapter class.
Example
notesQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
String data = "";
for(final QueryDocumentSnapshot document: task.getResult()){
Note note = document.toObject(Note.class);
mNotes.add(note);
if( userinput !=null ) {
findbutton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
final String userinputString = userinput.getText().toString();
Note match = null;
String matcheddata ="";
for (Note note : mNotes) {
if (note.getTitle().contains(userinputString)) {
match = note;
String matchedtitle = match.getTitle();
String matchedcontent = match.getContent();
matcheddata += "هاتف:" + matchedcontent + "\nالاسم:" + matchedtitle + "\n\n";
}
}
if (match != null) {
ArrayList<String> namesList = new ArrayList<>();
resultTxt.setText( matcheddata );
namesList.clear();
namesList.add(matcheddata);
ArrayAdapter<String>adapter = new ArrayAdapter<>( getApplicationContext(), android.R.layout.simple_selectable_list_item, namesList );
listView.setAdapter(adapter);
}
}
} );
}
else {
userinput.setError( "اسم الملف مطلوب" );
}
}
}
}
});
Upvotes: 0