为什么listview在回到前台后没有刷新?

时间:2017-06-20 15:36:57

标签: java android listview arraylist notifydatasetchanged

我试图通过两项活动进行聊天应用。在第一个计划是一个包含每行中最后发送的消息的短列表。第二个活动包含所有对话。我使用socket.io,我的问题是,当我点击后退按钮然后我回到我的应用程序notifyDataSetChanged()停止工作。我的应用程序从一个与Android应用程序合作的网站接收来自对话框的消息。在控制台日志中,我看到收到了来自网站的消息,并且调用了onTaskComplete()方法,但是listview没有刷新。我读到一个适配器在重新启动一个活动后失去了对listview的引用,但是如何在我的代码中看到我在onResume()方法中创建了一个新的适配器。我不明白我做错了什么? 如果我将超出条件" mSocket.on(" message",onMessage);"然后在返回应用程序后刷新列表视图,但消息的次数是返回的次数。

下面是我的代码,请帮忙!

MainActivity:

package com.example.seadog.fb_dialog;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class MainActivity extends Activity implements MyListener {

    public ListView listView;
    public MyBaseAdapter adapter;

    public TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        /*
         * Get Socket.io Object
         */

        SocketIO socketIo = new SocketIO();

        Socket mSocket = socketIo.getSocket();  // get socket
        Integer id = socketIo.getId();          // get Website ID

        if(mSocket == null) {

            socketIo.Connection();
            mSocket = socketIo.getSocket();

            mSocket.on("message", onMessage);

        }

        listView = (ListView) findViewById(R.id.listView);


        /*
         * OnItemClickListener
         */

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(MainActivity.this, Conversation.class);
                intent.putExtra("item", position);
                startActivity(intent);

                TextView textView = (TextView) view.findViewById(R.id.descitem);
                textView.setTypeface(null, Typeface.NORMAL);

            }

        });

        textView = (TextView) findViewById(R.id.count);

    }

    private Emitter.Listener onMessage = new Emitter.Listener() {

            /*
             * Message Listener
             */

        @Override
        public void call(Object... args) {

            Boolean isset = false;

            try {

                JSONObject object = (JSONObject) args[0];


                String _id = object.getString("_id");
                String message = object.getString("message");

                JSONObject obj = new JSONObject();
                obj.put("direction", "fb-lt");
                obj.put("message", message);
                obj.put("date", "2017-05-29T12:15:49.245Z");


                for(int i = 0; i < arrayList.size(); i++){

                    ListData ld = (ListData) arrayList.get(i);
                    String id = ld.getId();

                    if(_id.equals(id)){

                        JSONArray Data = ld.getData();
                        Data.put(obj);
                        ld.setDescription(message);

                        arrayList.set(i, ld);

                        isset = true;

                        Log.d("LOG", message);
                    }

                }

                if(!isset) {

                    JSONArray jsonArray = new JSONArray();
                    jsonArray.put(obj);

                    ListData ld = new ListData();
                    ld.set_id(_id);
                    ld.setID(1);
                    ld.setTitle("Klient:");
                    ld.setDescription(message);
                    ld.setData(jsonArray);

                    arrayList.add(ld);

                }

                onTaskComplete();

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    };


    public void onResume() {

        super.onResume();

        ArrayList clone = (ArrayList) arrayList.clone();
        arrayList.clear();
        arrayList.addAll(clone);

        adapter = new MyBaseAdapter(this, arrayList);
        listView.setAdapter(adapter);

    }

    @Override
    public void onTaskComplete() {

        runOnUiThread(new Runnable() {

            @Override
            public void run () {
                Log.d("LOG:","refresh");
                adapter.notifyDataSetChanged();
            }

        });

    }

}

接口MyListener:

package com.example.seadog.fb_dialog;

import java.util.ArrayList;

public interface MyListener {

    ArrayList arrayList = new ArrayList();

    void onTaskComplete();

}

1 个答案:

答案 0 :(得分:0)

如果我记得很清楚,当你在android中打开一个新活动时,那就是与前一个活动分开的东西。 当您按下后退按钮时,它只会终止您所在的活动并显示之前的活动。

也许你可以尝试在按下背部时做你需要的东西:

@Override
public void onBackPressed()
{
    //Do your things here  
}

希望我帮助过。