【问题标题】:Why listview is not refresh after come back to the foreground?为什么返回前台后listview不刷新?
【发布时间】:2017-11-23 06:06:24
【问题描述】:

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

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

主活动:

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();

}

【问题讨论】:

  • 您想在使用时刷新列表视图再次返回您的应用吗?
  • 是的,因为当应用程序在后台运行时我会收到一条新消息时,列表视图无法刷新。 ArrayList 已更新,但 listview 尚未更新。所以在来到前台时,我想刷新一个列表视图。

标签: java android listview arraylist notifydatasetchanged


【解决方案1】:

如果我没记错的话,当你在 android 中打开一个新 Activity 时,它与前一个是不同的。 当您按下后退按钮时,它只会终止您所在的活动并显示上一个活动。

也许你可以尝试在按下返回时做你需要做的事情:

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

希望我能帮上忙。

【讨论】:

  • 你是对的,但是当我从第一个活动(隐藏应用程序)按下返回按钮然后我打开应用程序时,我的列表视图没有刷新。只有当我点击后退按钮然后我回来时它才会停止工作,但是当我按下主页按钮或购物车视图然后我回来时它就可以工作了。
  • 应用程序仍在后台运行,并没有真正被杀死。尝试手动杀死它,然后查看您的列表视图是否已刷新。尝试添加finish();在 onBackPressed() 函数中。但是您总是需要在每次开始时重新加载所有内容。我不是专家,所以我可能是错的:/
  • 我希望我的应用在后台运行。它必须像 messenger/viber 等一样工作。当我收到消息时,声音会播放,我会再次打开我的应用程序。每时每刻都有一个列表视图必须刷新。
  • 看看这个,也许对你有帮助。您可以在 onResume() 函数中刷新列表视图。 stackoverflow.com/questions/4414171/…
  • 我在我的项目中使用了 onResume() 方法,但不起作用。如何创建对 arraylist 的新引用?也许它是一个问题?看...当我第一次打开我的应用程序时,它的效果很好。例如,我从网站发送消息,应用程序接收这些消息。接下来,当我按下后退按钮(应用程序是后台)然后在这一刻返回到前台时,onResume() 方法会刷新列表视图,但是接下来当我尝试从控制台日志中的网站发送消息时,我看到消息已收到因为 onTaskComplete() 方法被调用但列表视图没有刷新。为什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 2016-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多