0

The app worked fine when I used Spinner. But when I tried AutoComplete Textview instead of Spinner, nothing is displayed when I type on it. Any help is appreciated.

Logcat Warn:

08-23 14:01:13.485 9542-9542/com.vyshnav.realmexample W/art: Failed to find OatDexFile for DexFile /data/data/com.vyshnav.realmexample/files/instant-run/dex/slice-slice_9-classes.dex ( canonical path /data/data/com.vyshnav.realmexample/files/instant-run/dex/slice-slice_9-classes.dex) with checksum 0x0486ffcc in OatFile /data/data/com.vyshnav.realmexample/cache/slice-slice_9-classes.dex
08-23 14:01:14.237 9542-9542/com.vyshnav.realmexample W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-23 14:01:14.769 9542-9595/com.vyshnav.realmexample E/GED: Failed to get GED Log Buf, err(0)
08-23 14:01:14.839 9542-9595/com.vyshnav.realmexample W/MALI: glDrawArrays:714: [MALI] glDrawArrays takes more than 5ms here. Total elapse time(us): 12482
08-23 14:01:20.502 9542-9668/com.vyshnav.realmexample W/Filter: An exception occured during performFiltering()!
                                                            java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
                                                                at io.realm.BaseRealm.checkIfValid(BaseRealm.java:449)
                                                                at io.realm.RealmResults.isLoaded(RealmResults.java:872)
                                                                at io.realm.RealmResults.size(RealmResults.java:372)
                                                                at java.util.AbstractCollection.toArrayList(AbstractCollection.java:348)
                                                                at java.util.AbstractCollection.toArray(AbstractCollection.java:339)
                                                                at java.util.ArrayList.<init>(ArrayList.java:97)
                                                                at android.widget.ArrayAdapter$ArrayFilter.performFiltering(ArrayAdapter.java:456)
                                                                at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
                                                                at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                at android.os.Looper.loop(Looper.java:194)
                                                                at android.os.HandlerThread.run(HandlerThread.java:61)
08-23 14:01:35.351 9542-9811/com.vyshnav.realmexample W/Filter: An exception occured during performFiltering()!
                                                            java.lang.NullPointerException: collection == null
                                                                at java.util.ArrayList.<init>(ArrayList.java:94)
                                                                at android.widget.ArrayAdapter$ArrayFilter.performFiltering(ArrayAdapter.java:456)
                                                                at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
                                                                at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                at android.os.Looper.loop(Looper.java:194)
                                                                at android.os.HandlerThread.run(HandlerThread.java:61)
08-23 14:01:44.516 9542-9899/com.vyshnav.realmexample W/Filter: An exception occured during performFiltering()!
                                                            java.lang.NullPointerException: collection == null
                                                                at java.util.ArrayList.<init>(ArrayList.java:94)
                                                                at android.widget.ArrayAdapter$ArrayFilter.performFiltering(ArrayAdapter.java:456)
                                                                at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
                                                                at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                at android.os.Looper.loop(Looper.java:194)
                                                                at android.os.HandlerThread.run(HandlerThread.java:61)

MinActivity.java:

package com.vyshnav.realmexample;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;

import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;
public class MainActivity extends AppCompatActivity {

private Realm realm;
private RealmConfiguration realmConfig;
private String[] listOfStationNames;
ArrayAdapter adapter;
AutoCompleteTextView autoCompleteTextView1;

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

    //toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //fab
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    listOfStationNames = new String[] {"vvv", "rrr"};
    autoCompleteTextView1= (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

    // field variable
    RealmResults<Station> stations;
    // field variable
    RealmChangeListener<RealmResults<Station>> changeListener = new RealmChangeListener<RealmResults<Station>>() {
        @Override
        public void onChange(RealmResults<Station> results) {
            // handle onSuccess()
        }
    };


    // Create the Realm configuration                   // In-Memory realm
    realmConfig = new RealmConfiguration.Builder(this).name("myrealm.realm").inMemory().build();
    // Open the Realm for the UI thread.
    realm = Realm.getInstance(realmConfig);

    // Call basicWrite
    basicWrite(realm);

    // Read
    stations = realm.where(Station.class).findAll();
    stations.addChangeListener(changeListener);

    adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,stations);
    autoCompleteTextView1.setAdapter(adapter);


}


private void basicWrite(Realm realm) {

    //(incase of using Callback) watch Slidenerd's approach 'AsyncTask transaction = realm.execute.....' and then closing transaction
    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            Station station = new Station();
            for(String stationName : listOfStationNames) {
                station.setName(stationName);
                realm.insert(station);
            }
        }
    });}



@Override
protected void onDestroy() {
    super.onDestroy();
    realm.close(); // Remember to close Realm when done.
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Station.java:

package com.vyshnav.realmexample;

import io.realm.RealmObject;

public class Station extends RealmObject {
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String toString(){//overriding the toString() method
    return name;
}
}
V1 Kr
  • 177
  • 1
  • 3
  • 18

1 Answers1

1

Evaluate the filter in publishResults() instead of performFiltering(), because Realm queries should be executed on the thread where you want to access the results.

You'll need to override the performFiltering method of ArrayAdapter, kinda like this guy did.

(Also, now that I think about it, it might probably be a good idea for you to change your ArrayAdapter into a RealmBaseAdapter ~ source, or at least handle automatic updates with a RealmChangeListener manually.)

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • k..let me have a check – V1 Kr Aug 23 '16 at 09:02
  • https://github.com/realm/realm-java/issues/2887 How about this one? I think its yours – V1 Kr Aug 23 '16 at 12:00
  • That works well too, but it uses `RecyclerView` so it's kinda a DIY solution instead of spinner/autocompletetextview (and yes, I wrote that a while ago off the top of my head :P) – EpicPandaForce Aug 23 '16 at 12:11
  • I tried that. Removed some bugs(mostly spelling mistakes and semicolons) and ran it. App runs fine but no items are displayed. I could only type in EditText. But no Reyclerview is displayed – V1 Kr Aug 24 '16 at 08:51
  • Also I included the row layout into the recyclerview which u missed thinking if that was the prob..but still same. – V1 Kr Aug 24 '16 at 08:53
  • I updated the code there slightly, it's a shame the user never replied saying "pls halp your code doesn't handle if the input text is shorter than 2 and should have a default state" – EpicPandaForce Aug 24 '16 at 09:01
  • Try bigger words, the initial version should work for word length > 3, but I edited it now to make it better – EpicPandaForce Aug 24 '16 at 09:56
  • New version mostly shows a textview while searching..not recyclerview..for eg: if i have 4 words "appleA", "appleaAB", "appleABC" and "appleABCD", without typing anything as well as while typing 'a' or 'appleA' it shows the full list. If typed 'appleAB', only "appleAB" is shown, and if 'appleABC' is typed, it shows only "appleABC". Why is it like that? – V1 Kr Aug 24 '16 at 11:12
  • I'd have to see the code at this point. Considering it's a completely different setup that doesn't use `Filterable`, it should be its own question. – EpicPandaForce Aug 24 '16 at 11:17
  • Its okey.. I apperciate yr answers. Now lemme dive deep into Realm.. Tnx lot – V1 Kr Aug 24 '16 at 14:27
  • hey, is this a correct way of doing: https://gist.github.com/sdex/83b75ce9c2f2e2654bec Though no errors were shown compile time, app crashes while searching – V1 Kr Aug 26 '16 at 09:57
  • Well it opens a new Realm instance each time it filters something so that's pretty bad, and you have to change false to Case.INSENSITIVE – EpicPandaForce Aug 26 '16 at 16:41
  • Ohkey, dats the error too i guess..new realm instance is not closed in dat.. I saw a new one.. https://github.com/realm/realm-java/issues/646 this one has the answer i think. But I couldn't instantiate the abstract class in my mainActivity since they both already extends another. Do you know how to do that? – V1 Kr Aug 27 '16 at 04:41
  • I have posted it as a new question: http://stackoverflow.com/questions/39179576/android-realm-abstract-class-instantiate – V1 Kr Aug 27 '16 at 09:53