开发者

update dialog box's content when icon is click inside ListView

I am trying open a dialog when an icon inside ListView is clicked. The dialog is a AlertDialog which contains few TextViews that shows information based on the List Item clicked. Each List It开发者_开发百科em will show different information inside dialog box when clicked.

the problem is when an icon is clicked i am calling showDialog(INFO_DIALOG_ENYTY); and TextView inside dialog are updated only once as dialog is created inside onCreateDialog.

But I want to update TextView's contect which is inside dialog every time i click on List Item icon.

or if anyone else has another option to use instead of dialog then please suggest as my soul purpose is to show information when icon is clicked inside ListView


on click event check for the alertbuilder instance if the instance is not null then set text to the textview and show the dialog using showdialog(0) method. if the instance is not null then create the dialog

See the sample example

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();
        }
    }

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜