Android - 更新回收站视图在Listener中运行不正常

时间:2016-05-21 04:46:04

标签: android android-recyclerview

我添加了像这样的列表方法

//db connection

global $conn;

$servername = "localhost";  //host name

$username = "username"; //username

$password = "password"; //password

$mysql_database = "dbname"; //database name

//mysqli prepared statement 

$conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());

mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");

$stmt = $conn->prepare("UPDATE news SET title=?,content=?,link=?,price=?,ppcode=? WHERE id =?");

            $stmt->bind_param('sssdii',$newsubject,$newdisc,$newlink,$newprice,$newppcode,$id);

 //   i corresponding variable has type integer
 //  d  corresponding variable has type double
//  s   corresponding variable has type string
//  b   corresponding variable is a blob and will be sent in packets

            $stmt->execute();

            $row_count= $stmt->affected_rows;
            $stmt->close();
             $conn->close();

我在这个方法中称之为

private void addMessage(String username, String message) {
  mMessages.add(msgObj);
  mAdapter.notifyItemInserted(mMessages.size()-1);
  Log.d("username", msgObj.getUsername());
  Log.d("message", msgObj.getMessage());
  mAdapter.notifyDataSetChanged();
  scrollToBottom();
}

此方法处于异步任务

private WebSocket connect() throws IOException, WebSocketException
    {
        return new WebSocketFactory()
            .setConnectionTimeout(TIMEOUT)
            .createSocket(SERVER)
            .addListener(new WebSocketAdapter() {
                // A text message arrived from the server.
                public void onTextMessage(WebSocket websocket, String message) {
                    Log.d("WebSocketFactory", message);
                    JSONObject json = null;
                    try {
                        json = new JSONObject(message);
                        if(json.getString("type").equals("msg")) {
                            JSONObject data = json.getJSONObject("data");
                            addMessage(data.getString("author"), data.getString("text"));
                        }
                    } catch(JSONException je) {
                        je.printStackTrace();
                    }
                }
            })
            .addExtension(WebSocketExtension.PERMESSAGE_DEFLATE)
            .connect();
    }

并且效果不佳。 显示addMessage方法中的调试日志。我可以看到日志 Log.d(" username",msgObj.getUsername()); Log.d(" message",msgObj.getMessage()); 但是,视图不会更新。但是当我滚动视图时,视图会更新。 我每次都要滚动视图才能看到消息。

BUT!当我在onCreate中调用addMessages方法时,这只能很好地工作。

我找不到有什么不同......

我该如何解决这个问题?我必须在onTextMessage Listner中调用addMessage

0 个答案:

没有答案