2016-07-30 68 views
0
public RecyclerView.Adapter mAdapter; 
    public RecyclerView mMessagesView; 

這是我的名單的Android recyclerView UPDATA所有列表數據仍與舊的數據

public List<MessageList> Message_List = new ArrayList<MessageList>(); 

合併,這是我的適配器

mAdapter = new MessageAdapter1(getApplicationContext(), Message_List); 
    mMessagesView = (RecyclerView) findViewById(R.id.messages); 
    mMessagesView.setLayoutManager(new LinearLayoutManager(this)); 
    mMessagesView.setAdapter(mAdapter); 

現在我加上它

一些數據
Message_List.add(messageList); 
    mAdapter.notifyItemInserted(Message_List.size() - 1); 

其工作沒有問題 現在我想改變這一切Message_List數據和recyvlerView 顯示在我這樣做

public void swap(List<MessageList> datas){ 
    Message_List.clear(); 
    Message_List.addAll(datas); 
    mAdapter.notifyDataSetChanged(); 

} 

在這裏,我代替Message_ListnotifyDataSetChanged所有數據現在它顯示的Message_List

Message_List展示新數據空白

但是當我嘗試添加新項目列表並通知適配器它顯示給我一些舊項目我刪除我牛逼從Message_ListLOG我看它沒有舊數據有

現在這是更新之前的圖像Message_List數據

enter image description here

改變現在Message_List數據,並通知

enter image description here

像我們看到空白,因爲沒有數據Message_List

現在嘗試新的項目添加到Message_List並通知適配器

Message_List.add(balabala data); 
mAdapter.notifyItemInserted(Message_List.size() - 1); 

和結果必須是沒有舊的數據 enter image description here

更多信息,這是我的MessageList

public class MessageList { 
public static final int TYPE_MESSAGE_RIGHT = 0; 
public static final int TYPE_MESSAGE_LEFT = 1; 
public static final int TYPE_ACTION = 2; 
public static final String TYPING = "typing"; 
private String message, thumbnailUrl; 
private String date; 
private int user_id; 
private boolean status = false; 
private String fname; 
private String direction; 
private ArrayList<String> messageList; 

public MessageList() { 
    setMessageStatus(status); 
} 

public MessageList(String name, String thumbnailUrl, String date, String direction, 
        ArrayList<String> messageList, String fname, int user_id) { 
    this.message = name; 
    this.thumbnailUrl = thumbnailUrl; 
    this.date = date; 
    this.direction = direction; 
    this.messageList = messageList; 
    this.fname = fname; 
    this.user_id = user_id; 


} 

public String getMessage() { 
    return message; 
} 
public String getFname() { 
    return fname; 
} 

public void setMessage(String message) { 
    this.message = message; 
} 

public String getThumbnailUrl() { 
    return thumbnailUrl; 
} 

public void setThumbnailUrl(String thumbnailUrl) { 
    this.thumbnailUrl = thumbnailUrl; 
} 

public String getDate() { 
    return date; 
} 


public boolean getMessageStatus() { 
    return status; 
} 

public void setMessageStatus(boolean status) { 
    this.status = status; 
} 


public int getUser_id() { 
    return user_id; 
} 

public void setUser_id(int user_id) { 
    this.user_id = user_id; 
} 

public void setDate(String date) { 
    this.date = date; 
} 

public void setFname(String fname) { 
    this.fname = fname; 
} 

public String getDir() { 
    return direction; 
} 

public void setDir(String direction) { 
    this.direction = direction; 
} 

public ArrayList<String> getMessageList() { 
    return messageList; 
} 

public void setMessageList(ArrayList<String> messageList) { 
    this.messageList = messageList; 
} 

} 

這是我的適配器

public class MessageAdapter1 extends RecyclerView.Adapter<MessageAdapter1.ViewHolder> { 

private List<MessageList> mMessages; 
private int[] mUsernameColors; 
private Context context; 

public MessageAdapter1(Context context, List<MessageList> messages) { 
    mMessages = messages; 
    this.context = context; 
} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    int layout = -1; 

    int type = -1; 
    if(mMessages.get(viewType).getDir().equals("left"))   type = 1; 
    else if(mMessages.get(viewType).getDir().equals("right")) type = 0; 
    else if(mMessages.get(viewType).getDir().equals("typing")) type = 2; 


    switch (type) { 
    case MessageList.TYPE_MESSAGE_RIGHT: 
     layout = R.layout.right_message; 
     break; 
    case MessageList.TYPE_MESSAGE_LEFT: 
     layout = R.layout.left_message; 
     break; 
    case MessageList.TYPE_ACTION: 
     layout = R.layout.message_left; 
     break; 
    } 

    View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false); 

    return new ViewHolder(v); 
} 

@Override 
public void onBindViewHolder(ViewHolder viewHolder, int position) { 
    MessageList message = mMessages.get(position); 


    viewHolder.setGroupMessage(message); 
} 

@Override 
public int getItemCount() { 
    return mMessages.size(); 
} 



@Override 
public int getItemViewType(int position) { 

    return position; 
} 




public class ViewHolder extends RecyclerView.ViewHolder { 

    private LinearLayout groupMessage; 
    //private ImageView typing; 
    public ViewHolder(View itemView) { 
     super(itemView); 

     groupMessage = (LinearLayout)itemView.findViewById(R.id.messages); 

    } 

    public void setGroupMessage(MessageList m) { 


     if (null == groupMessage) return; 

     int i = 0; 

     if(m.getMessageStatus() == false){ 
      m.setMessageStatus(true); 
      for (String message : m.getMessageList()) { 

       //TextView text = new TextView(activity); 
       TextView text = new MyTextView(context); 
       LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
       p.setMargins(0, 0, 0, 2); 

       if (m.getDir().equals("left")) { 

        text.setTextColor(Color.BLACK); 
        p.gravity = Gravity.LEFT; 
        if(m.getMessageList().size() == 1){ 
         text.setBackgroundResource(R.drawable.message_left_default); 
        } 
        else if (i == 0) { 
         text.setBackgroundResource(R.drawable.message_left_first); 
        } else if (i + 1 == m.getMessageList().size()) { 
         text.setBackgroundResource(R.drawable.message_left_last); 
        } else { 
         text.setBackgroundResource(R.drawable.message_left); 
        } 


       } else{ 

        p.gravity = Gravity.RIGHT; 
        text.setTextColor(Color.WHITE); 

        if(m.getMessageList().size() == 1){ 
         text.setBackgroundResource(R.drawable.message_right_default); 
        } 
        else if (i == 0) { 
         text.setBackgroundResource(R.drawable.message_right_first); 
        } else if (i + 1 == m.getMessageList().size()) { 
         text.setBackgroundResource(R.drawable.message_right_last); 
        } else { 
         text.setBackgroundResource(R.drawable.message_right); 
        } 

       } 


       text.setLayoutParams(p); 
       text.setText(message); 
       text.setPadding(8, 8, 8, 8); 

       text.setTextSize(18f); 



       //text.setTextAppearance(context, android.R.style.TextAppearance_Large); 
       groupMessage.addView(text); 
       i++; 
      } 

     } 




    } 



} 
} 
+0

顯示您getItemCount()方法也。 – uguboz

+0

@UğurB'@Override public int getItemCount(){ return mMessages.size(); }' – medo

+0

不是Message_List?什麼是mMessages? – uguboz

回答

0

問題是mMessages和Message_List之間沒有連接。爲了達到這個目的,把swap()放在適配器類中,並從activity或fragment中調用它。

 public class MessageAdapter1 extends RecyclerView.Adapter<MessageAdapter1.ViewHolder> { 

      ... 

       public void swap(List<MessageList> datas){ 
        mMessages.clear(); 
        mMessages.addAll(datas); 
        notifyDataSetChanged(); 
       } 


       public void add(MessageList data){ 
         mMessages.add(data); 
         notifyItemInserted(mMessages.size() - 1);  
       } 

     } 

,並從片段或活動:

((MessageAdapter1) rv.getAdapter).add(data); 

((MessageAdapter1) rv.getAdapter).swap(Message_List); 
+0

我將函數添加到適配器但我不能訪問它! 'adapter.swap(Message_List);' – medo

+0

同樣的問題仍然存在舊數據:/ – medo