在ListView中单击图标时更新对话框的内容

时间:2011-06-29 09:56:34

标签: android listview

我在点击ListView内的图标时尝试打开一个对话框。对话框为AlertDialog,其中包含少量显示基于单击列表项的信息的TextViews。单击时,每个列表项将在对话框中显示不同的信息。

问题是当点击图标时我正在调用showDialog(INFO_DIALOG_ENYTY);并且对话框内的TextView仅在onCreateDialog中创建对话框时更新一次。

但我想每次点击List Item图标时更新TextView的对话框内的对话。

或者如果其他人有另一个选项而不是对话框那么请建议我的灵魂目的是在ListView中点击图标时显示信息

1 个答案:

答案 0 :(得分:0)

如果实例不为null,则单击事件检查alertbuilder实例,然后将文本设置为textview并使用showdialog(0)方法显示对话框。如果实例不为null,则创建对话框

参见示例

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    int counter = 0;

    Builder myAlertDialog;
    View entryView;
    TextView dialogTextview;

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

        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(listener);

    }

    public OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            ++counter;
            startDialog("Clicked=" + counter);

        }

    };

    private void startDialog(String message) {
        LayoutInflater factory = LayoutInflater.from(this);
        entryView = factory.inflate(R.layout.mytest, null);
        dialogTextview = (TextView) entryView.findViewById(R.id.textview1);
        if (myAlertDialog == null) {
            myAlertDialog = new AlertDialog.Builder(this);
            myAlertDialog.setView(entryView);

            myAlertDialog.setMessage("Do you want to exit the application?");
            myAlertDialog.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        // do something when the button is clicked
                        public void onClick(DialogInterface arg0, int arg1) {
                            System.out.println("...yes button is clicked..");
                            arg0.dismiss();

                        }
                    });

            myAlertDialog.setNegativeButton("NO",
                    new DialogInterface.OnClickListener() {

                        // do something when the button is clicked
                        public void onClick(DialogInterface arg0, int arg1) {
                            System.out.println("...clicked no...");
                            arg0.dismiss();
                        }
                    });
            AlertDialog alert = myAlertDialog.create();

            alert.getWindow().setLayout(600, 400);

            myAlertDialog.show();
        } else {
            dialogTextview.setText(message);
            System.out.println("...settext in dialog...");
            myAlertDialog.setView(entryView);
            myAlertDialog.show();
        }
    }

}