无法实现setAdapter

时间:2012-10-18 22:12:56

标签: android adapter

我想实现自己的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事件为“show”的按钮。它运行正常,并显示我在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中的show()事件来自Eclipse图形布局(继承自View.onClick

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

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

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

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

然后,我希望在listview中更改描述,而不是它,放入Received消息。所以我使用方法show(传入消息)

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

和listviews,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”,“在lv.setAdapter之前的show()中”; 不是listview。

1 个答案:

答案 0 :(得分:0)

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

public void show(View v)

您可能想在节目中调用show(String c)方法(查看c)?因为最终会被称为......