Android 如何在SharedReference中使用Chat保存ListView

Android 如何在SharedReference中使用Chat保存ListView,android,Android,我正在与bubble进行聊天,其对话框保存在列表视图中,运行正常,现在我想将此聊天保存在内存中,当我重新打开应用程序时,我可以跟踪对话,类似于WhatsApp。有人能告诉我这是否可以通过SharePreference实现,以及如何实现吗 此处显示主活动代码: public class MainActivity extends AppCompatActivity { private ListView listView; private View btnSend; private EditText

我正在与bubble进行
聊天
,其对话框保存在列表视图中
,运行正常,现在我想
将此聊天保存在内存中
,当我重新打开应用程序时,我可以跟踪对话,
类似于WhatsApp
。有人能告诉我这是否可以通过
SharePreference
实现,以及如何实现吗

此处显示主活动代码:

public class MainActivity extends AppCompatActivity {
private ListView listView;
private View btnSend;
private EditText editText;
boolean myMessage = true;
private List<ChatBubble> ChatBubbles;
private ArrayAdapter<ChatBubble> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ChatBubbles = new ArrayList<>();

    listView = (ListView) findViewById(R.id.list_msg);
    btnSend = findViewById(R.id.btn_chat_send);
    editText = (EditText) findViewById(R.id.msg_type);
    adapter = new MessageAdapter(this, R.layout.left_chat_bubble, ChatBubbles);
    listView.setAdapter(adapter);

    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (editText.getText().toString().trim().equals("")) {
                Toast.makeText(MainActivity.this, "Please input some text...", Toast.LENGTH_SHORT).show();
            } else {
                //add message to list
                ChatBubble ChatBubble = new ChatBubble(editText.getText().toString(), myMessage);
                ChatBubbles.add(ChatBubble);
                adapter.notifyDataSetChanged();
                editText.setText("");
                if (myMessage) {
                    myMessage = false;
                } else {
                    myMessage = true;
                }
            }
        }
    });
  }
}
public类MainActivity扩展了AppCompatActivity{
私有列表视图列表视图;
私人视图btnSend;
私人编辑文本;
布尔值myMessage=true;
私人名单;
专用阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chatbubles=newarraylist();
listView=(listView)findViewById(R.id.list\u msg);
btnSend=findviewbyd(R.id.btn\u chat\u send);
editText=(editText)findViewById(R.id.msg_类型);
adapter=新消息适配器(this,R.layout.left\u chat\u bubble,chatbubles);
setAdapter(适配器);
btnSend.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(editText.getText().toString().trim().equals(“”){
Toast.makeText(MainActivity.this,“请输入一些文本…”),Toast.LENGTH_SHORT.show();
}否则{
//将消息添加到列表
chatbuble chatbuble=newchatbuble(editText.getText().toString(),myMessage);
添加(chatbuble);
adapter.notifyDataSetChanged();
editText.setText(“”);
如果(myMessage){
myMessage=false;
}否则{
myMessage=true;
}
}
}
});
}
}
下面是错误:

您可以使用
Gson
实现'com.google.code.Gson:Gson:2.8.5'

比如说

Gson gson = new Gson();
String json = gson.toJson(ChatBubble);
mSharedPref.putString("chatList", json);
mSharedPref.apply();
然后检索这个:

Gson gson = new Gson();
ArrayList<ChatBubble> mList = ArrayList<ChatBubble>();
String json = mSharedPref.getString("chatList", "");    
mList = gson.fromJson(json, ArrayList<ChatBubble>.class);
编辑 创建一个arrayList,如下所示

ArrayList<ChatBubble> mList = new ArrayList<ChatBubble>();
然后,每次在onCreate()方法中转到main活动时,您都会这样做

private void setUpChat(){
    SharedPreferences mSharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    Gson gson = new Gson();
    String json = mSharedPref.getString("chatList", ""); 
    ChatBubble[] mArray = gson.fromJson(json,ChatBubble[].class)   
    mList = new ArrayList<>(Arrays.asList(mArray));
    adapter = new MessageAdapter(this, R.layout.left_chat_bubble, mList);
    listView.setAdapter(adapter);
}
private void setUpChat(){
SharedPreferences mSharedPref=PreferenceManager.getDefaultSharedPreferences(此选项);
Gson Gson=新的Gson();
String json=mSharedPref.getString(“chatList”和“”);
chatbuble[]mArray=gson.fromJson(json,chatbuble[].class)
mList=newarraylist(Arrays.asList(mArray));
adapter=newmessageadapter(this,R.layout.left\u chat\u bubble,mList);
setAdapter(适配器);
}

skizoozᴉʞS
,你能给我一个更完整的例子吗?你更需要什么?实际上,这是如何创建
共享引用的,但我如何将其应用到我的代码中,以便每次打开应用程序并启动聊天时,都从内存中加载过去几天的其他聊天,我需要它与
ArrayList()
中的
WhatsApp消息的行为相同,参数必须放在括号内?我想是的,试试看第一个错误需要使用列表名的变量,第二个错误删除ArrayList并离开chatbuble.class
private void saveChat(){
SharedPreferences mSharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = mSharedPref.edit();
Gson gson = new Gson();
String json = gson.toJson(ChatBubble);
editor.putString("chatList", json);
editor.apply();
}
private void setUpChat(){
    SharedPreferences mSharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    Gson gson = new Gson();
    String json = mSharedPref.getString("chatList", ""); 
    ChatBubble[] mArray = gson.fromJson(json,ChatBubble[].class)   
    mList = new ArrayList<>(Arrays.asList(mArray));
    adapter = new MessageAdapter(this, R.layout.left_chat_bubble, mList);
    listView.setAdapter(adapter);
}