java - 从 BaseAdapter 内的 Activity 调用函数

标签 java android dialog

谁能告诉我如何在扩展BaseAdapter的类中调用Activity的函数?

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


}

//======我的基础适配器==========

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



}

我想在 BaseaAdapter 中调用 ShowingDialog,因为 BaseAdapter 不属于 AlertDialog!!

最佳答案

只需更改您的:

private Context context;

private MyPage myPage;

然后在showDialog中调用

myPage.ShowingDialog();

关于java - 从 BaseAdapter 内的 Activity 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10336050/

相关文章:

java - InvokeLater 使用事件排序

java - 为什么 Intellij Idea 建议在使用循环将数组转换为 Set 时创建中间列表?

android - 印度的 Google 结帐替代方案

Bash 对话框显示未知文本

android - 带有 ListView 和按钮的自定义对话框始终全屏显示

java - 将数组对象中的信息打印到单独的字段中

java - 尝试记住代码可视化工具的名称

java - Tomcat JDBC连接池(释放连接)

java - 从 html 文件中收集文本数据

android - 我可以让仪器测试杀死并重新启动应用程序进程吗?