代码之家  ›  专栏  ›  技术社区  ›  Lucian

从锁定机制锁定UI线程更新片段中的ListView

  •  0
  • Lucian  · 技术社区  · 7 年前

    我有一个 列表视图 我想用来自蓝牙插座的信息更新。ListView是一个片段,这并不重要。
    当我想监听来自套接字的传入消息(这是一个单独线程上的锁定机制)并用接收到的消息更新ListView时,就会出现问题。
    fchat.java语言

    public class FChat extends Fragment {
        ArrayList<String> listItems=new ArrayList<String>();
        ArrayAdapter<String> itemsAdapter;
        ....
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
            //setup list view
            ListView messageContainer = (ListView) thisView.findViewById(R.id.btMessagesContainer);
            itemsAdapter = new ArrayAdapter<String>(thisView.getContext(), android.R.layout.simple_list_item_1, listItems);
            currentAct = getActivity();
            Thread test = new Thread() {
            public void run() {
                    try {
                        currentAct.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                listItems.add("other:" + String.valueOf(times));
                                try {
                                    String reply = bluetoothConnector.readSingleMessage();
                                    listItems.add("other:" + reply);
                                    itemsAdapter.notifyDataSetChanged();
                                }
                                catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
        };
        test.start();            
    
        }
    }
    

    所以,这感觉像是完全阻塞了UI线程,所以我猜runounithread是阻塞了UI线程。
    如果我把挡块拿出来 String reply = bluetoothConnector.readSingleMessage(); 换成 String reply = "test" 它工作得很好,用户界面得到了更新,看起来工作得很好。
    所以,我的问题是,如何从一个套接字读取数据并用它的内容更新ListView?
    谢谢你

    1 回复  |  直到 7 年前
        1
  •  1
  •   Andrey Danilov    7 年前

    UI thread

    Thread {
        //there is separate thread
        UiThread{
           //there is UI thread
           blockingOperation()
        }
    }
    

    String reply = "test"
    

    String reply = bluetoothConnector.readSingleMessage();
    

    Thread test = new Thread() {
        public void run() {
                try {
                    final String reply = bluetoothConnector.readSingleMessage();
                    currentAct.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            listItems.add("other:" + String.valueOf(times));
                            listItems.add("other:" + reply);
                            itemsAdapter.notifyDataSetChanged();
                        }
                    });
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
    };