filter java.lang.IllegalStateException:从错误的线程访问Realm。 Realm对象只能在创建它们的线程上访问

时间:2017-03-08 06:26:20

标签: android realm

我正在通过Realm db中的Searhview过滤listview项目。当我选择搜索视图时,我得到以下的删除

java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.

当我开始搜索时,listview被清除,它是空的。如果我关闭搜索视图列表视图没有回来。

适配器代码:

class DescAdapter extends BaseAdapter implements Filterable
{
 private seconds ds;
 private RealmResults<realdbTable> musers;
 private List<realdbTable> mOrig;
 Activity seconds;
 Realm realm;

DescAdapter(seconds ds, RealmResults<realdbTable> musers, com.statmob.seconds context)
{
 this.ds = ds;
 this.musers = musers;
 this.seconds = context;
}

@Override
public int getCount()
{
 return musers == null ? 0 : musers.size();
}

@Override
public Object getItem(int i)
{
  return musers == null ? null : musers.get(i);
}

@Override
public long getItemId(int i)
{
  return i;
}


@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
  realdbTable du = musers.get(i);
  String name = du.getName();
  String status = du.getStatus();
  long stime = du.getStime();
  Viewholder viewholder;
  return view;
}

@Override
public Filter getFilter()
{

 return new Filter()
 {
   @Override
  protected FilterResults performFiltering(final CharSequence constraint)
  {
      final FilterResults oReturn = new FilterResults();
      if (constraint == null || constraint.length() == 0) {
      oReturn.values = musers;
      oReturn.count = musers.size();
      } else {
      ds.runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  realm = Realm.getDefaultInstance();

                  final String lastToken = constraint.toString().toLowerCase();
                  RealmResults<realdbTable>  data = realm.where(realdbTable.class).equalTo("name", lastToken).findAll();
                  Log.e("Shiva","realm results--->"+data);
                  oReturn.values = data;
                  oReturn.count = data.size();
              }
          });
        //  Log.e("Shiva","Constraint not null");

         // final String lastToken = constraint.toString().toLowerCase();
        //  RealmResults results = null;

          //RealmQuery<realdbTable> query = realm.where(realdbTable.class);
          //query.contains("name", lastToken);
          //RealmResults<realdbTable> result = query.findAll();
          //  results.add();
        //  RealmResults  data = realm.where(realdbTable.class).equalTo("name", lastToken).findAll();


        //  oReturn.values = data;
         // oReturn.count = data.size();
      }
       return  oReturn;
    }
     RealmResults<realdbTable> filterResults(String text) {
         text = text == null ? null : text.toLowerCase().trim();
         if(text == null || "".equals(text)) {
             musers = realm.where(realdbTable.class).findAll();
         } else {
             musers = realm.where(realdbTable.class)
                     .contains("fieldToQueryBy", text, Case.INSENSITIVE) // TODO: change field
                     .findAll();
         }
         return musers;
     }

  @Override
  protected void publishResults(CharSequence constraint, FilterResults results)
  {
     if(constraint!=null && constraint.length()>0) {
    //mOrig = musers.where().contains("name",String.valueOf(constraint)).findAll();
         Log.e("Shiva","Publishing constraint not null"+results);

         musers= (RealmResults<realdbTable>) results.values;
               //  filterResults(constraint.toString());
    //  mOrig  =  performRealmFiltering(constraint,musers);
    notifyDataSetChanged();
  }
      }
 };
 }

public void notifyDataSetChanged()
{
  super.notifyDataSetChanged();
}

}

我在SO中搜索错误,我无法通过提供的解决方案解决它。请帮忙。

编辑:

class DescAdapter extends RealmBaseAdapter implements Filterable
{
private seconds ds;
private RealmResults<realdbTable> musers;
private RealmResults<realdbTable> mOrig;
OrderedRealmCollection<realdbTable> listreports;
Activity seconds;
Realm realm;
DescAdapter(seconds ds, RealmResults<realdbTable> musers, com.statmob.seconds context)
{
 super(musers);
 this.ds = ds;
 this.musers = musers;
 this.seconds = context;
}

@Override
public int getCount()
{
 return musers.size();
}

@Override
public RealmModel getItem(int i)
{
  return musers.get(i);
}

@Override
public long getItemId(int i)
{
  return i;
}



@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
  realdbTable du = musers.get(i);
  String name = du.getName();
  String status = du.getStatus();
  long stime = du.getStime();
  Viewholder viewholder;

    final long time1 = System.currentTimeMillis();

    long tdiff = time1 - stime;
    long Mins = (tdiff / (60 * 1000));
    long Hours = (Mins / 60);
    long Days = Hours / 24;
    long Months = Days / 30;
    long year = Months / 12;
    long secs = Mins * 60;
  if(view==null)
  {
    viewholder = new Viewholder();
    view = LayoutInflater.from(ds).inflate(R.layout.custom, null);
    viewholder.uname = (TextView) view.findViewById(R.id.name);
    viewholder.ustatus = (TextView) view.findViewById(R.id.status);
    viewholder.utime = (TextView) view.findViewById(R.id.stime);

    view.setTag(viewholder);
  }
  else
  {
    viewholder = (Viewholder) view.getTag();
  }
    viewholder.uname.setText(name);
    viewholder.ustatus.setText(status);

    return view;
}


public void filterResults(String text) {
    text = text == null ? null : text.toLowerCase().trim();

    if(text == null || "".equals(text) || text.isEmpty()) {
        listreports = realm.where(realdbTable.class).findAll();
    } else {
        listreports = realm.where(realdbTable.class)
                .contains("fieldToQueryBy", text, Case.INSENSITIVE) // TODO: change field
                .findAll();
    }
    updateData(listreports);

}

@Override
public Filter getFilter()
{
  StatFilter statFilter = new StatFilter(this);
  return statFilter;
}

private class StatFilter extends Filter {

    private final DescAdapter adapter;

    public StatFilter(DescAdapter adapter) {
         super();
        this.adapter = adapter;
    }

    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        return new FilterResults();
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
        if(charSequence == null || "".equals(charSequence) || charSequence.toString().isEmpty()) {
        adapter.filterResults(String.valueOf(charSequence)); }
    }
}

}

0 个答案:

没有答案