0

Can anyone tell me how to call function of an Activity inside a class that extends BaseAdapter?

//My activity

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MyPage extends Activity {

    private Activity mContext;
    Button createForm;
    Button ConfExpandRegion, Cancelb;
    String ExpandMsg, CancelMsg;
    boolean b;
    MyCaseClass mycase;
    TextView tvCase;
    AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mypage);


        // Moving to anther activity
        createForm = (Button) findViewById(R.id.creat_new_formbtn);
        createForm.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent j = new Intent(MyPage.this, CreateNewForm.class);
                startActivity(j);

            }
        });

        // ============================================================================================
        // for list


            ListView list = (ListView) findViewById(R.id.mypage_list);
            list.setClickable(true);

            final List<MyCaseClass> listOfPhonebook = new ArrayList<MyCaseClass>();

            MyCasesListAdapter adapter = new MyCasesListAdapter(this, listOfPhonebook);

            for (MyCaseClass m : All_Static.getMyCaseList())
                adapter.add(new MyCaseClass(m));

            // after fill the adapter.. assign the list to the adapter
            list.setAdapter(adapter);

            list.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View view, int position, long index) {
                    System.out.println("sadsfsf");
                ;
                }
            });
            list.setAdapter(adapter);
        // ========================================================================================

    }



    public void sendSMS(String number, String msg) throws Exception {
        if (!b) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(number, null, msg, null, null);
        }
        b = true;
    }

    // ========================================================================

    public void ShowingDialog(){
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Conformation");
        alertDialog.setMessage("Are you sure you want to Expand Report Region?");
        alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                  //some codes

                    }
                   ConfExpandRegion.setEnabled(false);
               }

            });

            alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                // here you can add functions
                // Do nothing 




               }
            });

            alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialog.show();
    }


}

//====== my baseadapter ===========

import java.util.List;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class MyCasesListAdapter extends BaseAdapter {
    private Context context;

    private List<MyCaseClass> listOfCases;

    // TODO delete it not imp.
    public MyCasesListAdapter() {

        super();

    }

    public MyCasesListAdapter(Context context, List<MyCaseClass> listPhonebook) {
        this.context = context;
        this.listOfCases = listPhonebook;
    }

    public int getCount() {
        return listOfCases.size();
    }

    public Object getItem(int position) {
        return listOfCases.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup viewGroup) {
        MyCaseClass entry = listOfCases.get(position);

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.mypage_row, null);

        }

        // this is row items..
        // Set the onClick Listener on this button
        Button ConfExpandRegion = (Button) convertView
                .findViewById(R.id.expand);
        Button Cancelb = (Button) convertView.findViewById(R.id.cancelCase);
        TextView tvCase = (TextView) convertView.findViewById(R.id.mypage_name);

        // To be a clickable button
        ConfExpandRegion.setFocusableInTouchMode(false);
        ConfExpandRegion.setFocusable(false);
        // For Dialog

        ConfExpandRegion.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        // To be a clickable button
        Cancelb.setFocusableInTouchMode(false);
        Cancelb.setFocusable(false);
        Cancelb.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                MyCaseClass entry = (MyCaseClass) v.getTag();
                listOfCases.remove(entry);
                // listPhonebook.remove(view.getId());
                notifyDataSetChanged();
            }
        });

        // Set the entry, so that you can capture which item was clicked and
        // then remove it
        // As an alternative, you can use the id/position of the item to capture
        // the item
        // that was clicked.
        ConfExpandRegion.setTag(entry);
        Cancelb.setTag(entry);

        // btnRemove.setId(position);

        return convertView;
    }

    public void onClick(View view) {
        MyCaseClass entry = (MyCaseClass) view.getTag();
        listOfCases.remove(entry);
        // listPhonebook.remove(view.getId());
        notifyDataSetChanged();

    }

    private void showDialog(MyCaseClass entry) {
        // Create and show your dialog
        // Depending on the Dialogs button clicks delete it or do nothing
    }

    public void add(MyCaseClass myCaseClass) {
        // TODO Auto-generated method stub
        listOfCases.add(myCaseClass);
    }



}

I want to call ShowingDialog in BaseaAdapter since the BaseAdapter doesn't belongs AlertDialog!!

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Monerah
  • 215
  • 1
  • 5
  • 17

3 Answers3

2

Just change your:

private Context context;

to

private MyPage myPage;

Then in showDialog call

myPage.ShowingDialog();
dbyoung
  • 276
  • 2
  • 7
  • This is the simplest way of doing this. Make this change in your adapter and you should be fine. The Context is a reference to your activity. – Bobbake4 Apr 26 '12 at 15:24
0

According to your question AlertDialog doesn't belong to BaseAdapter. Include your method (its Java not C so is not called function) in the BaseAdapter class. To use any class/method/variable from other nested classes you could put the name of the class and use this : MyCasesListAdapter.this.ShowingDialog(). However, you are calling a method that doesn't depend of any variable unless your button. Declare it public static and the use it MyCasesListAdapter.ConfExpandRegion. Also you can create a class MyDialog, for example, and implement the method there, like this:

public MyDialog(Context context, String title,  String message) {
    super(context);
    this.title = title;
    this.message = message;

}
    public void showMessage( String title, String message) {
    AlertDialog.Builder dialogBox = new AlertDialog.Builder(
            super.getContext());

    dialogBox.setTitle("Hello world");


    dialogBox.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {


            dialog.cancel();


        }
    });
    dialogBox.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

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

        }
    });

    dialogBox.show();

}
}

And you can pass anything as parameter on the constructor or in the method, any Object.

Call it on your BaseAdapter

new MyDialog(title, message).showmessage(title, message);

or

Mydialog mDialog =  new new MyDialog(title, message);
mDialog.showmessage(title, message);

and if you are concerned to enter your entry object pass it as parameter of the method.

your question isn't very clear. Hope this helps anyway,

Sulabh Gajjar
  • 496
  • 4
  • 16
  • Java has functions and methods. See this answer for more details about the difference in Java: http://stackoverflow.com/a/155655/180538 – WarrenFaith Apr 26 '12 at 16:02
  • @WarrenFaith: well AFAIK java doesn't have functions. check [this](http://java.sun.com/docs/white/langenv/Simple.doc2.html) in section 2.2.4 and i quote **Java has no functions. Object-oriented programming supersedes functional and procedural styles. ** – kokumajutsu Apr 27 '12 at 10:48
  • Still the definition of "function" remains the same, a function uses only the data that was passed to it and nothing from the class/object it is in. Again possible terminology issues but I will go with it... – WarrenFaith Apr 28 '12 at 08:55
0

yes you can try context.function_name() where "context" is the context which you have passed from your activity to base adapter. You can several things like broadcast, starting activity etc from that...

Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29