简体   繁体   中英

How to get a view in runtime when using socket.io in android?

I have an android application in frontend side and node js socket in backend.

There is a list of Chats in activity that I get them from socket.on event.

Every item in this list has a customview in it , I need to update this customview with different values when a socket event is received.

How can I do that?

Here is My Code When getting the list of chats:

final Handler mHandler04 = new Handler(Looper.getMainLooper());
            mHandler04.post(new Runnable() {
                @Override
                public void run() {
                    SocketManager.getInstance().getSocket().on("allchatres", new Emitter.Listener() {
                        @Override
                        public void call(final Object... args) {
                            g.context.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    JSONArray jsonArray = (JSONArray) args[0];
                                    Log.d(TAG, "run: " + jsonArray);
                                        try {
                                            for (int i = 0; i < jsonArray.length(); i++) {
                                                createView(jsonArray.getJSONObject(i).getString("title"), jsonArray.getJSONObject(i).getString("body"));
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                }
                            });
                        }
                    });
                }
            }); 

And Here is My Createview Code:

private void createView(final String title, final String body) {
    customViewChat = new customViewChat(g.context);
    layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    customViewChat.txtCsTitle.setText(title);
    customViewChat.txtCsBody.setText(body);
    LinearLayoutItemHolder.addView(customViewChat, layoutParams);
    customViewChat.btnJoin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(g.context, DetailActivity.class);
            i.putExtra("title", title);
            i.putExtra("body", body);
            startActivity(i);
        }
    });
}

But When i want to update customview in the list like this :

int count = LinearLayoutItemHolder.getChildCount() ;
Log.d(TAG,"child count : " + count) ;
for(int i = 0 ;i<count ; i++)
{
    View v = LinearLayoutItemHolder.getChildAt(i) ;

}

I see the below result in logcat:

child count : 0

How can i get every customview in LinearLayoutItemHolder ?

I Want to change customView Values in another socket.on event but I can't.

I searched a lot but haven't found anything useful.

Any suggestion will be helpful.

Finally, I found the best method to acheive my goal: using RecyclerView!

I had some dificulties using custom views so I used recycler view instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM