Listview不会从领域更新

时间:2015-06-19 18:04:44

标签: android listview android-listview realm

创建或接收消息后,发送消息然后将其保存到域。之后我需要在线程页面上更新我的线程ListView并将最新的消息带到顶部。我已经拥有它,所以线程列表显示更新的预览和更新日期,但它保持在它的初始ListView位置。我尝试重新查询域以获取所有信息并按lastUpdated时间重新排序,但它似乎不起作用。我是否需要擦除旧的线程列表然后重新填充它以使其更新?

我在onResume()

上触发了更新
    @Override
protected void onResume() {
    super.onResume();
    updateListview = true;
    updateList();
}

这是我的更新

    @UiThread
public void updateList() {
    try {

        if (updateListview) {
            thread_realm = Realm.getInstance(this);
            results = thread_realm.where(ZipListModel.class).findAllSorted("zipupdated", RealmResults.SORT_ORDER_DESCENDING);
            adapter = new ZipListAdapter(this, results);
            threadsListView.setAdapter(adapter);

            adapter.notifyDataSetChanged();
            if (results.size()==0){
                createZipHint.setVisibility(View.VISIBLE);
            } else {
                createZipHint.setVisibility(View.INVISIBLE);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

适配器

package com.admin.zipline.adapters;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.admin.zipline.R;
import com.admin.zipline.activities.ZipListPage;
import com.admin.zipline.model.ZipListModel;

import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class ZipListAdapter extends ArrayAdapter<ZipListModel>


  {
    List<ZipListModel> items;
 Context context;
 Typeface semiBold;
 Typeface light;
 Typeface regular;
 String[] months={};
 public ZipListModel ziplist;
 ArrayList<String> ziplistNames,ziplistParticipantsaids;
 public ZipListAdapter(Context context, List<ZipListModel> threadslist) {
     super(context,R.layout.zip_adapter_view,threadslist);
    this.context = context;
    this.items=threadslist;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
    ViewHolder holder ;
    if (view == null) {
        holder =new ViewHolder();
        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.zip_adapter_view, parent, false);
        light = Typeface.createFromAsset(context.getAssets(), "ProximaNova-Light.otf");
        regular = Typeface.createFromAsset(context.getAssets(), "ProximaNova-Regular.otf");
        semiBold = Typeface.createFromAsset(context.getAssets(),"ProximaNova-Semibold.otf");
        //TODO
        /*for showing the author image
        * */
        //holder.thread_image = (ImageView)view.findViewById(R.id.author_avatar);
        holder.thread_text = (TextView) view.findViewById(R.id.threadtext);
        holder.thread_name = (TextView) view.findViewById(R.id.threadname);
        holder.last_updated = (TextView) view.findViewById(R.id.lastupdated);
        holder.zip_members=(TextView)view.findViewById(R.id.ziplist_members);
        holder.thread_text.setTypeface(light);
        holder.thread_name.setTypeface(semiBold);
        holder.zip_members.setTypeface(regular);
        view.setTag(holder);
    }else{
        holder =(ViewHolder)view.getTag();
    }
    try{
        ziplist = items.get(position);
        ziplistNames = new ArrayList<String>();
        ziplistParticipantsaids=new ArrayList<>();

    if (ziplist != null) {
       if (ziplist.getMessagesListsmodel().first().getText()!=null){
           holder.thread_text.setText(ziplist.getMessagesListsmodel().first().getText());
       }
       if (ziplist.getMessagesListsmodel().first().getCreatedAt()!=null){
            holder.last_updated.setText(getDate(ziplist.getMessagesListsmodel().first().getCreatedAt()));
        }


        for (int i = 0; i < ziplist.getParticipantsmodel().size(); i++) {
            ziplistNames.add(ziplist.getParticipantsmodel().get(i).getName());
            ziplistParticipantsaids.add(ziplist.getParticipantsmodel().get(i).getParticipantId());
        }

        String members="";
        for (int i=0;i<ziplistNames.size();i++){
            members+=ziplist.getParticipantsmodel().get(i).getFirstName()+", ";
        }
       if (members.length() > 3){
           members=members.substring(0,members.length()-2);
       }

        holder.zip_members.setText(members);


        if(ziplist.getZipname().isEmpty()){
            holder.thread_name.setText(members);
        } else {
            holder.thread_name.setText(ziplist.getZipname());
        }
    }
    }
    catch (Exception e){
        e.printStackTrace();
    }
 view.setBackgroundColor(Color.parseColor(ziplist.getZipColor()));

    return view;
}
String getDate(Date date) {
           try {
                Date d = date;
               months=context.getResources().getStringArray(R.array.months);
               return months[d.getMonth()] + " " + (d.getDate());
           } catch (Exception e) {
               e.printStackTrace();
               return "";
    }
}
public class ViewHolder{
    ImageView thread_image;
    TextView thread_text,thread_name,last_updated,zip_members;
   // CardView cardView;
}

}

2 个答案:

答案 0 :(得分:1)

这个问题对我来说并不十分清楚,但我可以建议一些常见的方法来做到这一点。

假设您的ZipListModel定义如下:

public class ZipListModel extends RealmObject {
    private String title;
    private Date date;
    private String Author;

    ... getters and setters...
}

在ListView中显示模型的最简单方法是使用RealmBaseAdapter。您可以找到文档here。并example

例如:

public class ZipListAdapter extends RealmBaseAdapter<ZipListModel> implements ListAdapter {
    public ZipListAdapter(Context context, int resId,
                     RealmResults<ZipListModel> realmResults,
                     boolean automaticUpdate) {
        super(context, realmResults, automaticUpdate);
    }

    ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       // Update your views with the realmResults
       ...
       ZipListModel zlm = realmResults.get(position);
       authorTextView.setText(zlm.getAuthor);
       ...
    }
    ...
}

使用RealmBaseAdapter的好处是RealmResults可以在Realm事务提交时自动更新,这意味着在您的情况下,每当您通过

更新您的Realm时
thread_realm = Realm.getInstance(this);
thread_realm.beginTransaction()
// Change something in your Realm
... 
thread_realm.commitTransaction()

将自动调用ZipListAdapter的notifyDataSetChanged,并更新listView。

顺便说一下,你仍然可以使用自己的adpater,只需使用RealmChangeListener进行更新即可。请参阅example

答案 1 :(得分:0)

尝试使用RealmBaseAdapter.UpdateData(RealmResult<T> result)。我的项目有类似的要求,它适用于我

https://realm.io/docs/java/latest/api/io/realm/RealmBaseAdapter.html