为什么当我按后退按钮并返回活动时我的列表视图不刷新?

问题描述 投票:0回答:2

我已经尝试过我的代码。我正在编写一个聊天应用程序。我的应用程序包含两个活动。第一个活动有一个列表视图,其中每行包含为用户发送的最后一条消息,而第二个活动包含整个对话。在我的应用程序中,我使用了 Android 版的 socket.io。我的应用程序运行良好。

收到数据时列表视图会刷新,但是当我按后退按钮然后返回活动时,列表视图尚未刷新自身。在日志控制台中,我看到数据已收到并调用了“更改”方法,但列表视图未刷新。

下面的代码有什么问题?

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.net.URISyntaxException;
import java.util.ArrayList;

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

public class MainActivity extends Activity {

    public static ArrayList arrayList = new ArrayList();

    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", 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);

                        }

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

                    changed();

                }

            });

        }

        /*
         * Populate a listview
         */

        listView = (ListView) findViewById(R.id.listView);
        adapter = new MyBaseAdapter(this, arrayList);
        listView.setAdapter(adapter);

        /*
         * 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 void changed() {

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

    }

}

MyBaseAdapter 类:

package com.example.seadog.fb_dialog;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class MyBaseAdapter extends BaseAdapter {

    Context context;
    ArrayList<ListData> items = new ArrayList();
    LayoutInflater inflater;

    int id = 0;

    public MyBaseAdapter(Context context, ArrayList items) {
        this.context = context;
        this.items = items;
        inflater = LayoutInflater.from(this.context);
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public ListData getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder mViewHolder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item, parent, false);
            mViewHolder = new MyViewHolder(convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        ListData currentListData = getItem(position);

        id = position > 0 ? getItem(position - 1).getID() : 0;

        mViewHolder.Title.setText(currentListData.getTitle());
        mViewHolder.Desc.setText(currentListData.getDescription());

        if(1==1){

            mViewHolder.Title.setVisibility(View.GONE);

        }else {

            if (id != currentListData.getID()) {
                mViewHolder.Title.setVisibility(View.VISIBLE);
            } else {
                mViewHolder.Title.setVisibility(View.GONE);
            }

        }

        return convertView;
    }

    private class MyViewHolder {
        TextView Title, Desc;

        public MyViewHolder(View item) {
            Title = (TextView) item.findViewById(R.id.txtitem);
            Desc = (TextView) item.findViewById(R.id.descitem);

            Typeface title = Typeface.createFromAsset(context.getAssets(), "fonts/DroidSans.ttf");
            Title.setTypeface(title);

            Typeface desc = Typeface.createFromAsset(context.getAssets(), "fonts/DroidSans.ttf");
            Desc.setTypeface(desc, Typeface.BOLD);
        }
    }
}
android listview arraylist socket.io notifydatasetchanged
2个回答
1
投票

@覆盖 protected void onRestart() {

    Intent intentBack = new Intent(getApplicationContext(),MainActivity.class);
    startActivity(intentBack);
    super.onRestart();
}

0
投票

您返回此活动后没有任何回电。

您可以重写 onResume() 方法或 startActivityForResult 并在 onAcitvityForResult 方法中执行某些操作。

© www.soinside.com 2019 - 2024. All rights reserved.