我正在使用Realm.io来完成这个项目。我在片段中创建realm实例,并将RealmResults传递给我的适配器。每个人都在工作。酷豆。我有一个用于搜索的edittext,它也在我的片段中。它正在找到正确的域对象,我可以通过控制台上的日志消息看到它们。
我知道我的适配器正在获取正确的信息。
例如,
Object A: Comment=a!
Object B: Comment = b!
Object C: Comment = c
如果我搜索文本"!",它将在我的列表视图中显示对象A和B.如果我搜索文本" b",则只显示对象A.但是,搜索找到了正确的对象,因为我可以在我的控制台中看到它。而且,如果我搜索" c",则只显示对象A.
你们有没有想过为什么会发生这种情况?
这是Fragment W / ListView
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
realm = Realm.getInstance(getActivity());
RealmQuery<MyObject> query = realm.where(MyObject.class);
mItems = query.findAll();
realm.setAutoRefresh(true);
adapter = new MyObjectAdapter(getActivity(), mItems,false,
"fonts/Verdana.ttf");
realm.addChangeListener(new RealmChangeListener() {
@Override
public void onChange() {
}
});
realm.commitTransaction();
// initialize and set the list adapter
setListAdapter(adapter);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.log_entry_fragment, container,false);
editsearch = (EditText) view.findViewById(R.id.inputSearch);
editsearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
adapter.updateRealmResults(mItems);
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
/* Message message = buildMessage(WorkerHandler.SEARCH, s);
workerThread.workerHandler.sendMessage(message);
Log.d("CharSeq11111111", s.toString());*/
RealmQuery<MyObject> realmQuery= realm.where(MyObject.class);
realmQuery.contains("comment", s.toString());
mItems = realmQuery.findAll();
Log.d("SecondmItems", ""+mItems.toString());
}
});
return view;
}
这是适配器:
public class MyObjectAdapter extends RealmBaseAdapter<MyObject> implements ListAdapter{
RealmResults<MyObject> objects;
Typeface tf;
public Context context;
LayoutInflater inflater;
ViewHolder aViewHolder;
bViewHolder bViewHolder;
cViewHolder cViewHolder;
Realm realm;
public MyObjectAdapter(Context context, RealmResults<MyObject> realmResults,
boolean automaticUpdate, String FONT) {
super(context, realmResults, automaticUpdate);
this.context = context;
objects = realmResults;
tf = Typeface.createFromAsset(context.getAssets(), FONT);
}
@Override
public int getViewTypeCount()
{
return 1;
}
@Override
public int getItemViewType(int position)
{
return realmResults.get(position).getType();
}
@Override
public int getCount() {
return realmResults.size();
}
public void updateList(RealmResults<MyObject> newList){
this.realmResults = newList;
this.notifyDataSetChanged();
}
@Override
public MyObject getItem(int position)
{
return realmResults.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyObject listViewItem = realmResults.get(position);
int listViewItemType = listViewItem.getType();
aViewHolder = new ViewHolder();
bViewHolder = new bViewHolder();
cViewHolder = new cViewHolder();
if(convertView == null) {
inflater = LayoutInflater.from(context);
MyObject item = getItem(position);
convertView = inflater.inflate(R.layout.a_card_listview_item, parent, false);
/*Inflate the correct view for the type of object we're currently working with. Each
object has its own specific XML file, and hence needs to be inflated based on type.
*/
if(listViewItemType == 1){
convertView = inflater.inflate(R.layout.a_card_listview_item, parent, false);
}
else if(listViewItemType == 0){
convertView = inflater.inflate(R.layout.b_journal_listview_item, parent, false);
}
else if(listViewItemType == 2){
convertView = inflater.inflate(R.layout.c_journal_listview_item, parent, false);
}
//Set the background for the view. Every other object will alternate in colors.
if(position%2 == 0){
convertView.setBackgroundResource(R.drawable.largertailtesttwo);
}
else {
convertView.setBackgroundResource(R.drawable.largertailtest);
}
/* Set Views and View Content Here */
} else {
// recycle the already inflated view
//aViewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private static class ViewHolder {
TextView category;
TextView description;
TextView date;
TextView privacy;
TextView minutes;
TextView hours;
TextView client;
}
private static class bViewHolder {
TextView bCategory;
TextView privacy;
TextView bType;
TextView bJoinedBy;
RatingBar bStarLevel;
TextView bDescription;
TextView bDate;
TextView typeCategory;
}
private static class cViewHolder {
TextView cCategory;
TextView privacy;
TextView cType;
TextView cJoinedBy;
RatingBar cStarLevel;
TextView cDescription;
TextView cDate;
TextView cTypeCategory;
}
}
编辑:感谢用户@Rami!他指出使用&#34; If(ConvertView == null)&#34;我正在夸大我的观点的方式没有正常工作。我在那里删除了if / else,现在看起来工作得非常好!
真棒!