【问题标题】:onItemClick stop working after List update列表更新后 onItemClick 停止工作
【发布时间】:2013-08-02 23:28:08
【问题描述】:

我正在开发需要 listView 的应用程序。当我运行该应用程序时,它运行良好,我使用以下代码:

listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(MainActivity.this);
adapterListView = new AdapterListView(MainActivity.this, itemList);
listView.setAdapter(adapterListView);
listView.setCacheColorHint(Color.TRANSPARENT);

Main Activity 实现了 OnItemClickListener,其中我有:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
    ItemListView item = adapterListView.getItem(arg2);
    for(ConsultaAndroid ca : lista_consulta) {
        if(item.getTexto().contains(ca.getNome().toString()) && item.getTexto().contains(ca.getHorario())) {
            Intent intent = new Intent();
            intent.putExtra("nome", ca.getNome());
            intent.putExtra("rg", ca.getRg());
            intent.putExtra("cpf", ca.getCpf());
            intent.putExtra("datanasc", ca.getDatanasc());
            intent.putExtra("telefone", ca.getTelefone());
            intent.putExtra("celular", ca.getCelular());
            intent.putExtra("email", ca.getEmail());
            intent.putExtra("endereco", ca.getEndereco()+", "+ca.getNumero()+" - "+ca.getBairro()+" - "+ca.getCidade());
            intent.setClass(MainActivity.this,PacienteActivity.class);
            startActivity(intent);
        }
    }
}

之后,如果我单击 DatePicker,当我设置新日期时,列表应该更新,它确实更新了,但 onItemClick 停止工作。 我在 DatePicker 的 OnDateSetListener() 中使用了相同的代码。

任何想法为什么会发生?

XML:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFFFFFFF">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="393dp"
        android:divider="#ECECEC"
        android:dividerHeight="1sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.28"
            android:text="Data" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.28"
            android:text="Sair" />

    </LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: java android listview layout onitemclicklistener


    【解决方案1】:

    我不知道到底发生了什么,但是你可以在 startActivity 之后添加 break 或 return 来停止循环。但这不是重点)

    布局 xml 是干净的。你能显示完整的 MainActivity 代码吗?

    【讨论】:

    • 是的!)我知道,但没有足够的声誉来添加评论。声誉讽刺......
    • The first pice of code is in onCreate or onResume method? 对问题的贡献是否足够重要?此外,在回复某人时,请添加@username,以便他们了解您的回复。
    • @wikram 是的。你是对的 onCreate/onResume。没有完整的代码,很难给出建议。
    • 抱歉,我无法显示完整的代码..如果有特定的部分或我应该放在这里的一些额外信息,请告诉我。
    猜你喜欢
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多