1

我想实现我自己的ListView,但我有一些问题。我有以下代码:

private ArrayList<ItemChat> itemsChat;
private ListView lv;
......

public void show (View v){
     lv = (ListView)findViewById(R.id.listView);
     itemsChat = new ArrayList<ItemChat>();
     itemsChat.add(new ItemChat(1, "Patatas", "Tuberculo", "drawable/patatas"));
     ItemChatAdapter adapter = new ItemChatAdapter(this, itemsChat);
     lv.setAdapter(adapter);
}

onClick在布局中,我有一个事件是“显示”的按钮。ItemChat.java它工作正常,并显示我在类中定义的适配器。

但是,当我尝试调用另一种方法的相同代码时,它根本不起作用。

public void show2 (String c){
     lv = (ListView)findViewById(R.id.listView);
     itemsChat = new ArrayList<ItemChat>();
     itemsChat.add(new ItemChat(1, "Patatas", c , "drawable/patatas"));
     ItemChatAdapter adapter = new ItemChatAdapter(this, itemsChat);
     lv.setAdapter(adapter);
}

两者的区别在于论点。首先,由于该onClick事件,我通过 View V,其次,我尝试在适配器中显示String c而不是“Tuberculo”,但它不起作用。有没有人有任何想法?

PS:onClick event inshow()来自 Eclipse 图形布局(继承自View.onClick

好的,这是我的完整代码。

我尝试实现一个 xmpp 客户端,它工作正常,连接到 gtalk 服务器,登录用户,并收到来自其他用户的消息。我可以在日志中看到收到的消息,所以我想在自定义列表视图中接收 mje(类似于 Wahtsapp 中的聊天)。

首先,我创建了一个自定义列表视图并实现了一个调用“显示”的按钮来测试它是否工作正常。

当我按下显示按钮时,我的自定义列表视图显示:图片、标题、描述和其他图片。它工作完美。

然后,我想在我的列表视图中更改描述,而不是它,放置收到的消息。所以我使用了一个方法 show(incoming message)

我的 main.xml 文件有 2 个按钮。Conectec 和 Show。

和列表视图,id = listView

有2个文件java

ItemChat.java ItemChatAdapter.java

和我的主要活动

公共类 MiCliente2 扩展 Activity {

private final static String SERVER_HOST = "talk.google.com";
private final static int SERVER_PORT = 5222;
private final static String SERVICE_NAME = "gmail.com"; 
private final static String LOGIN = "xxxxxxxx@gmail.com";
private final static String PASSWORD = "xxxxxxx";
private XMPPConnection conn2;

private MessageListener messageListener;

private ArrayList<ItemChat> itemsChat;
private ListView lv;

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

}


//when press conect button, implement this.
//it´s works perfect
public void conect (View v){
    try 
    {
         initConnection();
    Log.i("XMPPClient","Server conected");
    }
    catch(XMPPException ex){
        Log.e("XMPPClient","Error conecting to server");    
    }
}

private void initConnection() throws XMPPException {

    ConnectionConfiguration config =
    new ConnectionConfiguration(SERVER_HOST, SERVER_PORT, SERVICE_NAME);
    conn2 = new XMPPConnection(config);
    config.setReconnectionAllowed(true);

    try {
        conn2.connect();

        Log.i("XMPPClient", " Connected to " + conn2.getHost());

    } catch (XMPPException ex) {
         Log.e("XMPPClient", "Failed to connect to " + conn2.getHost());
         Log.e("XMPPClient", ex.toString());

        }
    try {
        conn2.login(LOGIN, PASSWORD);
        Log.i("XMPPClient", "Logged in as " + conn2.getUser());

        Presence presence = new Presence(Presence.Type.available);
        presence.setStatus("On line");
        conn2.sendPacket(presence);

        // register listeners
        ChatManager chatmanager = conn2.getChatManager();
        conn2.getChatManager().addChatListener(new ChatManagerListener()
        {
            public void chatCreated(final Chat chat, final boolean createdLocally)
            {
                chat.addMessageListener(new MessageListener()
            {
          public void processMessage(Chat chat, Message message)
            {
              Log.i("XMPPClient","Received message: " 
              + (message != null ? message.getBody() : "NULL"));
        // I want to call show() passing message´s body               
              show(message.getBody());               
          }
        });
      }
    });

    } catch (XMPPException ex) {
        Log.e("XMPPClient", " Failed to log in as " + LOGIN);
        Log.e("XMPPClient", ex.toString());
    }
}

//Not work when call show("something")
public void show(String v) {
    Log.i("XMPPClient",v); // in log can see Received message
    lv = (ListView)findViewById(R.id.listView);

    itemsChat = new ArrayList<ItemChat>();            
    itemsChat.add(new ItemChat(1, "Patatas", "Tuberculo", "drawable/patatas"));

    ItemChatAdapter adapter = new ItemChatAdapter(this, itemsChat);

    Log.i("XMPPClient"," In show() before lv.setAdapter");
    lv.setAdapter(adapter);

    //this log never happend, only can see before msje

    Log.i("XMPPClient","In show() after lvsetAdapter");
 }

//pressing "Show" button
public void show2 (View v){

     lv = (ListView)findViewById(R.id.listView);
     itemsChat = new ArrayList<ItemChat>();
     itemsChat.add(new ItemChat(1, "Patatas", "Tuberculo", "drawable/patatas"));
     ItemChatAdapter adapter = new ItemChatAdapter(this, itemsChat);
     lv.setAdapter(adapter);
}

}

当传入消息和 show(meesage.getbody) 不显示任何内容时,在 logcat 中我只看到: Log.i("XMPPClient"," In show() before lv.setAdapter"); 不是列表视图。

4

1 回答 1

0

首先,什么是“c”?如果你用 onClick 属性在你的 xml 中设置它,那么你不能将任何字符串值传递给这个方法......那么现在“c”是什么?如果您从任何其他地方调用此方法,那么它应该可以工作......但是如果您在 xml 上设置按钮的 onClick 属性,那么您应该将其用作

public void show(View v)

您可能想在 show(View c) 中调用 show(String c) 方法?因为那最终会被调用...

于 2012-11-09T22:44:53.037 回答