Activity calling BaseAdapter which sets OnClick() for each item in row

Without showing too much code:

I have an Activity "A". This Activity populates a ListView via a BaseAdapter, the BaseAdapter's getView() sets an onClickListener() for an item in every row.

I want to show an AlertDialog in Activity "A" when the rows' item is clicked.

Answers


I don't see where you encounter problems. If you have your BaseAdapter version in the Activity A then you could simply call(in the OnCLickListener for that row's item that you set in the getView method of the BaseAdapter) a private method that you create in the A Activity:

private void showADialog(int position) {
        new AlertDialog.Builder(this)
                .setMessage("The clicked row is " + position)
                .setPositiveButton("Ok?",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }

                        }).show();
    }

If your custom BaseAdapter isn't in the same file as Activity A then you could use the Context that you passed to the BaseAdapter(if the code for your adapter is the one from your previous question, if not you should pass it in the constructor). You could then cast that Context to your activity and call the previous method showADialog(). Also when you set the listener you should pass the position as a tag so you can retrieve it in the OnCLickListener(otherwise you could get the wrong position as the views + listener get recycled):

//...
item.setTag(new Integer(position)); // or if you use a ViewHolder: holder.item bla.....
item.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Integer realPosition = (Integer) v.getTag();
    // Here show the dialog using one of the methods above.You either:        
    // showADialog(realPosition); // when the code for your custom BaseAdapter in in the same class as Activity A
    //((CallingAlertDialogBaseAdapter) context).showADialog(realPosition); // when the code for the custom BaseAdapter is in a separate file then Activity A
    //create directly an AlertDialog. For this you'll require a valid Context(from an  Activity(getApplicationCOntext() will NOT work). 
   //You have this context from the parameter that you pass in the constructor of your custom BaseAdapter(as you ussualy pass this in the Activity where you instantiate the adapter)
   }
});
//...

Need Your Help

How to deserialize this string into a PHP array of key => value pairs?

php arrays

I'm calling the script at: http://phat-reaction.com/googlefonts.php?format=php

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.