开发者

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. My code looks like this, and compiles, but does not add the item. Any help appreciated.

package com.painLogger;

import java.util.Arr开发者_开发问答ayList; ///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; 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);
    }
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜