简体   繁体   English

从EditText将输入的项添加到列表(使用SimpleAdapter)

[英]Adding an inputted item to a list(using SimpleAdapter) from a EditText

I am trying to enter something into two different EditTexts, then display them onto one item of a list. 我试图在两个不同的EditTexts中输入内容,然后将它们显示在列表的一个项目上。 My code looks like this, and compiles, but does not add the item. 我的代码看起来像这样,编译,但不添加项目。 Any help appreciated. 任何帮助赞赏。

package com.painLogger;

import java.util.ArrayList; ///ALL IMPORTS

public class PainLoggerActivity extends Activity implements OnClickListener, 
OnKeyListener {
/** Called when the activity is first created. */
EditText txtItem;
EditText txtItem2;
Button btnAdd;
ListView listItems; 
ArrayAdapter<String> aa;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    txtItem = (EditText)findViewById(R.id.txtItem);
    txtItem2 = (EditText)findViewById(R.id.txtItem2);

    btnAdd = (Button)findViewById(R.id.btnAdd);
    listItems = (ListView)findViewById(R.id.listItems);

    btnAdd.setOnClickListener(this);

    String[] from = new String[] {"row_1", "row_2"};
    int[] to = new int[] { R.id.row1, R.id.row2};

    SimpleAdapter adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout, 
    from, to);
    listItems.setAdapter(adapter);




}
private void addItem(){      
              HashMap<String,String>map = new HashMap<String, String>();
                    map.put("row_1",txtItem.getText().toString());
                    map.put("row_2",txtItem2.getText().toString());
                    painItems.add(map);

    }
@Override
public void onClick(View v) {
    if(v == this.btnAdd)
        addItem();

}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {

    if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == 
            KeyEvent.KEYCODE_DPAD_CENTER){
        this.addItem();
    }
    return false;

}

} }

Have you tried manually updating the display with 您是否尝试过使用手动更新显示

adapter.notifyDataSetChanged();

that may help or try to log exactly what is going in and out of your list. 这可能有助于或尝试准确记录列表中的内容。

` public class ChatServer { public class Message { String user; `public class ChatServer {public class Message {String user; String message; 字符串消息;

    public Message(String user, String message) {
        this.user = user;
        this.message = message;
    }
}

protected HashMap<String, ClientConnection> connections;

public synchronized void addUser(String user, ClientConnection connection) {
    connections.put(user, connection);
    System.out.println("Connection opened: " + user);       
}

public synchronized void removeUser(String user) {
    connections.remove(user);
    System.out.println("Connection closed: " + user);       
}

public synchronized void broadcastMessage(String user, String message) {
    System.out.println("Broadcast: " + user + " >> " + message);
    for(ClientConnection connection : connections.values()) {
        connection.sendMessage(user, message);
    }
}

public synchronized Collection<String> getUsers() {
    return connections.keySet();
}

public static void main(String[] args) {
    new ChatServer();
}

public ChatServer() {
    // set up data structures
    connections = new HashMap<String, ClientConnection>();

    try {
        // start server
        System.out.println("Starting Server");
        ServerSocket server = new ServerSocket(5050);

        // accept requests from clients to connect
        while (true) {
            // wait for client to connect
            Socket socket = server.accept();
            System.out.println("New Connection accepted");

            // create new communication thread per client
            ClientConnection connection = new ClientConnection(this, socket);

            // start communication with client
            connection.start();
        }
    } catch (IOException e) {
        // something went wrong!
        e.printStackTrace();
    }
}

} }

and another class 和另一堂课

   public class ClientConnection extends Thread {
private ChatServer server;
private Socket socket;
private DataInputStream in;
private DataOutputStream out;
private String user;

private static final String MESSAGE_COMMAND = "#MESSAGE";
private static final String USERS_COMMAND = "#USERS";
private static final String QUIT_COMMAND = "#QUIT";

public ClientConnection(ChatServer server, Socket socket) {
    // store references
    this.server = server;
    this.socket = socket;
}

public synchronized void sendMessage(String user, String message) {
    try {
        out.writeUTF(MESSAGE_COMMAND);
        out.writeUTF(user);
        out.writeUTF(message);
        out.flush();
    } catch (IOException e) {
        // oops!
        e.printStackTrace();
    }
}

public synchronized void sendUsers(Collection<String> users) {
    try {
        out.writeUTF(USERS_COMMAND);
        out.writeInt(users.size());
        for (String user : users) {
            out.writeUTF(user);
        }
        out.flush();
    } catch (IOException e) {
        // something went wrong
        e.printStackTrace();
    }
}

@Override
public void run() {
    try {
        // obtain i/o streams
        in = new DataInputStream(socket.getInputStream());
        out = new DataOutputStream(socket.getOutputStream());

        // obtain user name
        user = in.readUTF().trim();
        server.broadcastMessage("SERVER", user + " has joined.");

        // add to server structure so can start receiving messages
        server.addUser(user, this);

        // go into input stream polling loop
        String command = "";
        while (!command.equals(QUIT_COMMAND)) {
            command = in.readUTF();

            if (command.equals(MESSAGE_COMMAND)) {
                // handle message received from user, message expects a
                // parameter, i.e. the message that was sent, so read it
                String message = in.readUTF();

                // tell server to inform all users of the new message
                server.broadcastMessage(user, message);
            } else if (command.equals(USERS_COMMAND)) {
                // handle request to view users from user
                sendUsers(server.getUsers());
            } else if(command.equals(QUIT_COMMAND)) {
                server.broadcastMessage("SERVER", user + " has left.");                 
            }
        }

        // a QUIT_COMMAND was received - but is handled by the finally block

    } catch (IOException e) {
        // something went wrong, e.g. lost connection
        e.printStackTrace();
    } finally {
        // disconnect, start by closing streams
        try {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
            socket.close();
        } catch (IOException e) {
            // error closing connection
            e.printStackTrace();
        }

        // get server to remove from structures
        server.removeUser(user);
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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