簡體   English   中英

我如何使用arraylist填充listview

[英]How can I populate listview using arraylist

這是我的MainActivity 如何將按鈕內的值傳輸到ListView 我已經搜索過了,這不好。

public class MainActivity extends Activity {

 static final int SocketServerPORT = 8080;

 TextView infoIp, infoPort, chatMsg;
 Button chatmsg;
 String msgLog = "";

 List<ChatClient> user;    
 ServerSocket serverSocket;

 ListView lv;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  infoIp = (TextView) findViewById(R.id.infoip);
  infoIp.setText(getIpAddress());
  infoPort = (TextView) findViewById(R.id.infoport);
  chatMsg = (Button) findViewById(R.id.button1);

  lv = (ListView)findViewById(R.id.lv);      
  user = new ArrayList<ChatClient>();

  ChatServerThread chatServerThread = new ChatServerThread();
   chatServerThread.start();
 }
 }

 private class ChatServerThread extends Thread {

  @Override
  public void run() {
   Socket socket = null;

   try {
    serverSocket = new ServerSocket(SocketServerPORT);
    MainActivity.this.runOnUiThread(new Runnable() {

     @Override
     public void run() {
      infoPort.setText("I'm waiting here: "
        + serverSocket.getLocalPort());
     }
    });

    while (true) {
     socket = serverSocket.accept();
     ChatClient client = new ChatClient();
     user.add(client);
     ConnectThread connectThread = new ConnectThread(client, socket);
     connectThread.start();
    }

   } catch (IOException e) {
    e.printStackTrace();
   } finally {
    if (socket != null) {
     try {
      socket.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }    
  }    
 }

連接螺紋mmm

 private class ConnectThread extends Thread {

  Socket socket;
  ChatClient connectClient;
  String msgToSend = "";

  ConnectThread(ChatClient client, Socket socket){
   connectClient = client;
   this.socket= socket;
   client.socket = socket;
   client.chatThread = this;
  }

  @Override
  public void run() {
   DataInputStream dataInputStream = null;
   DataOutputStream dataOutputStream = null;

   try {
    dataInputStream = new DataInputStream(socket.getInputStream());
    dataOutputStream = new DataOutputStream(socket.getOutputStream());

    String n = dataInputStream.readUTF();        
    connectClient.name = n;

    MainActivity.this.runOnUiThread(new Runnable() {

     @Override
     public void run() {
     chatMsg.setText(msgLog + "\n");
     }      
    });

    dataOutputStream.flush();    

    while (true) {
     if (dataInputStream.available() > 0) {
      String newMsg = dataInputStream.readUTF();          

      msgLog += newMsg;
      MainActivity.this.runOnUiThread(new Runnable() {

       @Override
       public void run() {
        chatMsg.setText(msgLog);
       }
      });

      broadcastMsg(n + ": " + newMsg);
     }

     if(!msgToSend.equals("")){
      dataOutputStream.writeUTF(msgToSend);
      dataOutputStream.flush();
      msgToSend = "";
     }         
    }

   } catch (IOException e) {
    e.printStackTrace();
   } finally {
    if (dataInputStream != null) {
     try {
      dataInputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }

    if (dataOutputStream != null) {
     try {
      dataOutputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }

    user.remove(connectClient);
    MainActivity.this.runOnUiThread(new Runnable() {

     @Override
     public void run() {
      Toast.makeText(MainActivity.this, 
       connectClient.name + " removed.", Toast.LENGTH_LONG).show();

      msgLog += "-- " + connectClient.name + " leaved\n";
      MainActivity.this.runOnUiThread(new Runnable() {

       @Override
       public void run() {
       //chatMsg.setText(msgLog);
       }
      });

      broadcastMsg("-- " + connectClient.name + " leaved\n");
     }
    });
   }       
  }

  private void sendMsg(String msg){
   msgToSend = msg;
  }      
 }

 private void broadcastMsg(String msg){
  for(int i=0; i<user.size(); i++){
   user.get(i).chatThread.sendMsg(msg);     
  }

  MainActivity.this.runOnUiThread(new Runnable() {

   @Override
   public void run() {
   chatMsg.setText(msgLog +"\n");

   }
  });
 }

 class ChatClient {

String name;
  Socket socket;
  ConnectThread chatThread;   
    }
}

我認為此鏈接應該可以幫助您。

簡而言之,您需要使用ArrayAdapter從列表元素到布局的各個部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM