0

It's a shame... I'm trying to implement a SearView to filter a ListView and unfortunately, I never reached my goal, even by following simple tutorials (yes, with an S). Each time, I end up with a List with strange string instead of what I was expected.

This time, I was checking this one and one more time, my app is displaying strange listview : enter image description here

So, as it's not the first time, I think I made a mistake somewhere and I ask your help to understand the solution once and for all.

The shame. Going to stackoverflow to understand a tutorial...

By the way, thank's for your help.

The MainActivity.class

package com.soundbox.djbam.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


ArrayList<Spacecraft> spacecrafts = new ArrayList<>();
ArrayAdapter<Spacecraft> adapter;
ListView lv;
SearchView sv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //VIEW REFERENCES
    lv = findViewById(R.id.lv);
    sv = findViewById(R.id.sv);

    //POPULATE DATA
    fillData();

    //ADAPTER
    //final SoundAdapter adapter = new SoundAdapter(Sounds_Organisation.this, sounds);
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, spacecrafts);
    lv.setAdapter(adapter);

    //EVENTS
    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            //FILTER
            adapter.getFilter().filter(newText);

            return false;
        }
    });
}

private void fillData() {
    Spacecraft s = new Spacecraft("Voyager");
    spacecrafts.add(s);

    s = new Spacecraft("Rosetter");
    spacecrafts.add(s);
    s = new Spacecraft("Toucan");
    spacecrafts.add(s);
    s = new Spacecraft("Babouchka");
    spacecrafts.add(s);
    s = new Spacecraft("Biloute");
    spacecrafts.add(s);
    s = new Spacecraft("Test");
    spacecrafts.add(s);
}
}

The Spacecraft.class package com.soundbox.djbam.myapplication;

public class Spacecraft {
String name;

public Spacecraft(String name) {
    this.name = name;
}

public String getName() {
    return name;
}
}

The activity_main layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <SearchView
        android:id="@+id/sv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </SearchView>

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ListView>

</LinearLayout>


</android.support.constraint.ConstraintLayout>
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Ahriman
  • 115
  • 8
  • 1
    You need to override `toString()` in `Spacecraft`. – Andy Turner Jun 25 '18 at 20:16
  • Aaaaannnnnd it's working. Thank's to help a beginner. I learn one more thing. When it return strange row like com.app@number I have to override the string to delete the class name and make appear the name of the object and not the hashcode of it. – Ahriman Jun 26 '18 at 17:17

0 Answers0