列表视图仅显示数组列表的第一个结果

我有下面的代码显示数组列表的结果,但是,它只显示测试结果,我将相同的结果附加到文本视图进行测试,发现我从查询方法得到的结果确实包含多个他们出现在文本视图中,但列表视图只显示第一个。请在下面找到代码以及所附图片。

<?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>

http://img3.mukewang.com/6481a2340001c29d06541138.jpg

函数式编程
浏览 127回答 2
2回答

翻过高山走不出你

if (match != null) {&nbsp; &nbsp; ArrayList<String> namesList = new ArrayList<>();&nbsp; &nbsp; resultTxt.setText( matcheddata );&nbsp; &nbsp; // namesList.clear(); // You don't need to clear it as you just created it above&nbsp; &nbsp; namesList.add(matcheddata);&nbsp; &nbsp; ArrayAdapter<String> adapter = new ArrayAdapter<>( getApplicationContext(), android.R.layout.simple_selectable_list_item, namesList );&nbsp; &nbsp; listView.setAdapter(adapter);}嗨 Amer Anajjem,您可以尝试在创建适配器之前准备 namesList 并使用最新的 namesList 来创建适配器。编辑:我发现问题是:matcheddata是一个字符串。当您致电时namesList.add(matcheddata);,您仅将 1 项添加到列表中。结果,您在列表视图中只有 1 个项目。为了证明这一点,您可以尝试以下操作:if (match != null) {&nbsp; &nbsp; ArrayList<String> namesList = new ArrayList<>();&nbsp; &nbsp; resultTxt.setText( matcheddata );&nbsp; &nbsp; namesList.clear();&nbsp; &nbsp; namesList.add(matcheddata);&nbsp; &nbsp; namesList.add(matcheddata);&nbsp; &nbsp; ArrayAdapter<String> adapter = new ArrayAdapter<>( getApplicationContext(), android.R.layout.simple_selectable_list_item, namesList );&nbsp; &nbsp; listView.setAdapter(adapter);}您应该在列表视图中看到 2 个重复的项目。编辑 2:你可以试试这个,但我没有测试过。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&nbsp; &nbsp; &nbsp; &nbsp; View.OnClickListener,&nbsp; &nbsp; &nbsp; &nbsp; IMainActivity{&nbsp; &nbsp; private DocumentSnapshot documentSnapshot;&nbsp; &nbsp; ListView listView;&nbsp; &nbsp; public TextView resultTxt , userinput ;&nbsp; &nbsp; private ArrayList<Search> mSearch = new ArrayList<>();&nbsp; &nbsp; public FirebaseFirestore db = FirebaseFirestore.getInstance();&nbsp; &nbsp; public Note note ;&nbsp; &nbsp; public LinearLayout layout ;&nbsp; &nbsp; private ArrayList<Note> mNotes = new ArrayList<>();&nbsp; &nbsp; private DocumentReference noteRef = db.collection("notes").document();&nbsp; &nbsp; private CollectionReference notesCollectionRef = db.collection("notes");&nbsp; &nbsp; private RecyclerView mRecyclerView;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate( savedInstanceState );&nbsp; &nbsp; &nbsp; &nbsp; setContentView( R.layout.activity_search );&nbsp; &nbsp; &nbsp; &nbsp; final ListView listView = findViewById( R.id.listview4 );&nbsp; &nbsp; &nbsp; &nbsp; final EditText userinput = findViewById( R.id.userInputtxt );&nbsp; &nbsp; &nbsp; &nbsp; final Button&nbsp; &nbsp;findbutton = findViewById( R.id.findBtn );&nbsp; &nbsp; &nbsp; &nbsp; final TextView resultTxt = findViewById( R.id.resultTxt );&nbsp; &nbsp; &nbsp; &nbsp; mRecyclerView = findViewById(R.id.recycler_view);&nbsp; &nbsp; &nbsp; &nbsp; FirebaseFirestore db = FirebaseFirestore.getInstance();&nbsp; &nbsp; &nbsp; &nbsp; CollectionReference notesCollectionRef = db&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collection("notes");&nbsp; &nbsp; &nbsp; &nbsp; Query notesQuery = null;&nbsp; &nbsp; &nbsp; &nbsp; if(documentSnapshot != null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notesQuery = notesCollectionRef;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notesQuery = notesCollectionRef&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orderBy("timestamp", Query.Direction.ASCENDING);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; notesQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<QuerySnapshot> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(task.isSuccessful()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String data = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(final QueryDocumentSnapshot document: task.getResult()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Note note = document.toObject(Note.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mNotes.add(note);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( userinput !=null ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findbutton.setOnClickListener( new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String userinputString = userinput.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> namesList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Note note : mNotes) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (note.getTitle().contains(userinputString)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String matchedtitle = note.getTitle();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String matchedcontent = note.getContent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String matcheddata += "هاتف:" + matchedcontent + "\nالاسم:" + matchedtitle + "\n\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; namesList.add(matcheddata);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter<String> adapter = new ArrayAdapter<>( getApplicationContext(), android.R.layout.simple_selectable_list_item, namesList );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listView.setAdapter(adapter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userinput.setError( "اسم الملف مطلوب" );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onStart () {&nbsp; &nbsp; &nbsp; &nbsp; super.onStart();&nbsp; &nbsp; }}

扬帆大鱼

我认为这是适配器通知问题。您正在向适配器提供 0 大小列表,并立即通知适配器。我认为您应该在添加到列表后通知适配器。或者您可以在调用适配器类之前将数据添加到列表中。例子notesQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<QuerySnapshot> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(task.isSuccessful()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String data = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(final QueryDocumentSnapshot document: task.getResult()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Note note = document.toObject(Note.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mNotes.add(note);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( userinput !=null ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findbutton.setOnClickListener( new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String userinputString = userinput.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Note match = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String matcheddata ="";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Note note : mNotes) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (note.getTitle().contains(userinputString)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; match = note;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String matchedtitle = match.getTitle();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String matchedcontent = match.getContent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matcheddata += "هاتف:" + matchedcontent + "\nالاسم:" + matchedtitle + "\n\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (match != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> namesList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultTxt.setText( matcheddata );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; namesList.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; namesList.add(matcheddata);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter<String>adapter = new ArrayAdapter<>( getApplicationContext(), android.R.layout.simple_selectable_list_item, namesList );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listView.setAdapter(adapter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userinput.setError( "اسم الملف مطلوب" );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });只需更改上面的功能并尝试。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java