用新的SMS更新Recyclerview



我正在研究一个几乎完成的短信应用程序,但我有问题更新新的短信的Recyclerview,新的短信没有显示在列表上,直到我关闭并打开我的应用程序我已经尝试了很多解决方案,但我不能使它工作,请帮助提前感谢我的converationdetailsitemadapter .java类,我正在扩展recyverview。适配器

public class ConverstaionDetailsItemAdapter extends 
RecyclerView.Adapter<ConversationDetailsItemViewHolder> {
Conversation conversation;
List<Message> messages;
Context mContext;
public List<Message> getMessages() {
return messages;
}
public void setMessages(List<Message> messages) {
this.messages = messages;
}
public ConverstaionDetailsItemAdapter(List<Message> pMessages, Context context, Conversation 
pConversation) {
this.messages = pMessages;
mContext = context;
this.conversation = pConversation;
}
@Override
public ConversationDetailsItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ConversationDetailsItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_conversations_details_item, parent, false), mContext,conversation);
}
@Override
public void onBindViewHolder(final ConversationDetailsItemViewHolder holder, final int position) {
Message message = messages.get(position);
if (message != null) {
holder.setConversationItem(message, position);
}
}
@Override
public int getItemCount() {
return (messages == null) ? 0 : messages.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
}

我的ViewHolder类

public class ConversationDetailsItemViewHolder extends RecyclerView.ViewHolder {
Context mContext;
Conversation conversation;
ImageView imageView;
public static TextView txtContactName, txtMessage, txtMessageTime;
private RelativeLayout rltRoot;

public ConversationDetailsItemViewHolder(View view, Context context, Conversation pConversation) {
super(view);
mContext = context;
conversation = pConversation;
rltRoot = view.findViewById(R.id.rlt_conversation_details_item_root);
txtContactName = view.findViewById(R.id.txt_contact_name);
txtMessageTime = view.findViewById(R.id.text_message_time);
txtMessage = view.findViewById(R.id.text_message);
}
public void setConversationItem(Message item, final int position) {
if(item.getType().equals("1")) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
txtMessage.setLayoutParams(params);
txtMessage.setBackgroundResource(R.drawable.rounded_rectangle_incoming);

} else {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
txtMessage.setLayoutParams(params);
txtMessage.setBackgroundResource(R.drawable.rounded_rectangle);
}
txtMessage.setText(item.getBody()+"n");
}
}

我有类似的项目,查看我的代码

package com.example.crimerigestration;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.MenuView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class CrimeListFragment extends Fragment {

private boolean mSubtitleVisible;
private RecyclerView mCrimeRecyclerView;
private CrimeAdapter crimeAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment_list_crime,container,false);
mCrimeRecyclerView=(RecyclerView)v.findViewById(R.id.crime_recycler_view);
mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
setHasOptionsMenu(true);
updateUI();
return v;
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_crime_list,menu);
MenuItem menuItem=menu.findItem(R.id.show_subtitle);
if(mSubtitleVisible){
menuItem.setTitle(R.string.hide_subtitle);
}
else {
menuItem.setTitle(R.string.show_subtitle);
}
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
//return super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.new_crime:
Crime crime=new Crime();
CrimeLab.get(getActivity()).addCrime(crime);
Intent intent=CrimePagerActivity.newIntent(getActivity(),crime.getmID());
startActivity(intent);
return true;
case R.id.show_subtitle:
mSubtitleVisible=!mSubtitleVisible;
getActivity().invalidateOptionsMenu();
updateSubtitle();
return true;
default:
return super.onOptionsItemSelected(item);

}
}

private void updateSubtitle() {
String subtitle=null;
if(mSubtitleVisible){
CrimeLab crimeLab=CrimeLab.get(getActivity());
int count=crimeLab.getsCrimes().size();
subtitle=getString(R.string.subtitle_formate,count);
}
AppCompatActivity appCompatActivity=(AppCompatActivity)getActivity();
appCompatActivity.getSupportActionBar().setSubtitle(subtitle);
}
@Override
public void onResume() {
super.onResume();
updateUI();
}
private void updateUI() {
CrimeLab crimeLab=CrimeLab.get(getActivity());
ArrayList<Crime> crimes=crimeLab.getsCrimes();
if (crimeAdapter==null){
crimeAdapter =new CrimeAdapter(crimes);
mCrimeRecyclerView.setAdapter(crimeAdapter);
}
else {
crimeAdapter.setCrimes(crimes);
crimeAdapter.notifyDataSetChanged();
}
}

private class CrimeHolder extends RecyclerView.ViewHolder implements 
View.OnClickListener {
private TextView mTitleTextView, mDateTextView;
private ImageView mSolvedImageView;
private Crime mCrime;
public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item_crime,parent,false));
mTitleTextView=(TextView) itemView.findViewById(R.id.crime_title);
mDateTextView=(TextView)itemView.findViewById(R.id.crime_date);
mSolvedImageView=(ImageView)itemView.findViewById(R.id.imageView);
itemView.setOnClickListener(this);
}
public void bind(Crime crime){
this.mCrime=crime;
mTitleTextView.setText(mCrime.getmTitle());
mDateTextView.setText(mCrime.getmDate().toString());
mSolvedImageView.setVisibility(mCrime.ismSolved()?View.VISIBLE:View.GONE);
}
@Override
public void onClick(View v) {
Intent intent=CrimePagerActivity.newIntent(getActivity(),mCrime.getmID());
startActivity(intent);
}
}

private class RequireCrimeHolder extends RecyclerView.ViewHolder implements 
View.OnClickListener {
private TextView mTitleTextView, mDateTextView;
private ImageView mSolvedImageView;
private Button mRequireButton;
private Crime mCrime;
public RequireCrimeHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.requried_to_police,parent,false));
mTitleTextView=(TextView) itemView.findViewById(R.id.crime_title);
mDateTextView=(TextView)itemView.findViewById(R.id.crime_date);
mSolvedImageView=(ImageView)itemView.findViewById(R.id.imageView);
mRequireButton=(Button)itemView.findViewById(R.id.button);
Factory();
itemView.setOnClickListener(this);
}
protected void Factory(){
mRequireButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"No functionality 
available",Toast.LENGTH_LONG).show();
}
});
}
public void bind(Crime crime){
this.mCrime=crime;
mTitleTextView.setText(mCrime.getmTitle());
mDateTextView.setText(mCrime.getmDate().toString());
mSolvedImageView.setVisibility(mCrime.ismSolved()?View.VISIBLE:View.GONE);
}
@Override
public void onClick(View v) {
Intent intent=CrimePagerActivity.newIntent(getActivity(),mCrime.getmID());
startActivity(intent);        }
}

private class CrimeAdapter extends RecyclerView.Adapter{
private ArrayList<Crime> mCrime;
public CrimeAdapter(ArrayList<Crime> mCrime) {
this.mCrime = mCrime;
}
@Override
public int getItemViewType(int position) {
if (mCrime.get(position).ismRequire() ){
return 0;
}
else{
return 1;
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(getActivity());
if (viewType==0){
return new RequireCrimeHolder(layoutInflater,parent);
}
else{
return new CrimeHolder(layoutInflater,parent);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int 
position) {
Crime crime=mCrime.get(position);
if (crime.ismRequire() ){
RequireCrimeHolder requireCrimeHolder= (RequireCrimeHolder) holder;
requireCrimeHolder.bind(crime);
}
else{
CrimeHolder crimeHolder= (CrimeHolder) holder;
crimeHolder.bind(crime);
}
}
@Override
public int getItemCount() {
return mCrime.size();
}
public void setCrimes(ArrayList<Crime> crime){
this.mCrime=crime;
}
}
}

您应该调用notifyItemInsert(0)(或size - 1,而不是0,取决于排序asc/desc)。如果它不工作,尝试notifyDataSetChanged(),但不要保留它,因为它会降低性能。

相关内容

  • 没有找到相关文章

最新更新