2015-06-19 80 views
0

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

我已经更新上的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; 
} 

}

+0

@MarkusRubey是有了新的消息应该移动到ListView顶部的线程,但它停留在它的情况是在创建线程原装现货。因此,如果线程上面有2个其他线程(Items),并且线程得到更新,我需要它在ListView的顶部(0位置)。这是一个messagnig应用程序。 –

回答

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的benifit是realmResults可以自动更新时COMMITED境界交易,这意味着你的情况,当你通过

thread_realm = Realm.getInstance(this); 
thread_realm.beginTransaction() 
// Change something in your Realm 
... 
thread_realm.commitTransaction() 

更新你的境界ZipListAdapter的notifyDataSetChanged将被自动调用,并且listView将被更新。

顺便说一句,你仍然可以留在自己的adpater中,只需使用RealmChangeListener更新即可。见example