如何使BaseAdapter显示AlertDialog Android应用BaseAdapter、AlertDialog、Android

2023-09-05 03:30:12 作者:撩妹狂魔

我怎样才能使BaseAdapter按钮ListView中显示alertDialog,我试过了,但它停止工作意外(运行时错误)我的code如下图所示。

任何建议

在此先感谢

Monerah

====更新后=====================

 进口的java.util.List;

进口android.app.AlertDialog;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.Button;
进口android.widget.TextView;
进口android.widget.Toast;

公共类MyCasesListAdapter扩展了BaseAdapter {
    私人上下文的背景下;

    私人列表< MyCaseClass> listOfCases;

    // TODO删除它不是小鬼。
    公共MyCasesListAdapter(){

        超();

    }

    公共MyCasesListAdapter(上下文的背景下,名单,其中,MyCaseClass> listPhonebook){
        this.context =背景;
        this.listOfCases = listPhonebook;
    }

    公众诠释getCount将(){
        返回listOfCases.size();
    }

    公共对象的getItem(INT位置){
        返回listOfCases.get(位置);
    }

    众长getItemId(INT位置){
        返回的位置;
    }

    公共查看getView(INT位置,查看convertView,ViewGroup中的ViewGroup){
        MyCaseClass进入= listOfCases.get(位置);

        如果(convertView == NULL){

            LayoutInflater充气=(LayoutInflater)上下文
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.mypage_row,NULL);


        }

        //这是行项目..
        //设置onClick的监听器此按钮
        按钮ConfExpandRegion =(按钮)convertView.findViewById(R.id.expand);
        按钮Cancelb =(按钮)convertView.findViewById(R.id.cancelCase);
        TextView的tvCase =(TextView中)convertView.findViewById(R.id.mypage_name);

        //要成为一个可点击的按钮
        ConfExpandRegion.setFocusableInTouchMode(假);
        ConfExpandRegion.setFocusable(假);
       //对于对话
        AlertDialog alertDialog =新AlertDialog.Builder(MyCasesListAdapter.this);
       alertDialog.setTitle(构象);
       alertDialog.setMessage(你确定你想干什么???);
        ConfExpandRegion.setOnClickListener(新View.OnClickListener(){

            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                    alertDialog.setButton(是,新DialogInterface.OnClickListener(){
                   公共无效的onClick(DialogInterface对话,诠释它){
                      //有些code

                       //ConfExpandRegion.setEnabled(false);
                   }

                });

                alertDialog.setButton2(否,新DialogInterface.OnClickListener(){
                   公共无效的onClick(DialogInterface对话,诠释它){
                    //在这里你可以添加功能
                    // 没做什么




                   }
                });

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




        }});

       //要成为一个可点击的按钮
        Cancelb.setFocusableInTouchMode(假);
        Cancelb.setFocusable(假);
        Cancelb.setOnClickListener(新View.OnClickListener(){

            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                MyCaseClass条目=(MyCaseClass)v.getTag();
                listOfCases.remove(入口);
                // listPhonebook.remove(view.getId());
                notifyDataSetChanged();
            }
        });

        //设置条目,让你可以捕捉到哪些项目被点击,
        //然后将其删除
        //作为一种替代方法,您可以使用该项目的ID /位置捕获
        //项目
        //被点击。
        ConfExpandRegion.setTag(入口);
        Cancelb.setTag(入口);


        // btnRemove.setId(位置);


        返回convertView;
    }

    公共无效的onClick(视图查看){
        MyCaseClass条目=(MyCaseClass)view.getTag();
        listOfCases.remove(入口);
        // listPhonebook.remove(view.getId());
        notifyDataSetChanged();

    }

    私人无效的ShowDialog(MyCaseClass进入){
        //创建和显示你的对话
        //根据对话框按钮单击删除或什么都不做
    }

    公共无效添加(MyCaseClass myCaseClass){
        // TODO自动生成方法存根
        listOfCases.add(myCaseClass);
    }






}
 

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

 进口的java.util.ArrayList;
进口的java.util.List;

进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.Intent;
进口android.os.Bundle;
进口android.telephony.SmsManager;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.Button;
进口android.widget.ListView;
进口android.widget.TextView;

公共类我的页面扩展了活动{

    按钮的CreateForm;
    按钮ConfExpandRegion,Cancelb;
    字符串ExpandMsg,CancelMsg;
    布尔B:
    MyCaseClass mycase;
    TextView的tvCase;
    AlertDialog alertDialog;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.mypage);


        //移动到花药活动
        的CreateForm =(按钮)findViewById(R.id.creat_new_formbtn);
        createForm.setOnClickListener(新View.OnClickListener(){

            公共无效的onClick(视图v){

                意图J =新的意图(MyPage.this,CreateNewForm.class);
                startActivity(J);

            }
        });

        // ================================================ ============================================
        //为列表


            ListView控件列表=(ListView控件)findViewById(R.id.mypage_list);
            list.setClickable(真正的);

            最后的名单,其中,MyCaseClass> listOfPhonebook =新的ArrayList< MyCaseClass>();

            MyCasesListAdapter适配器=新MyCasesListAdapter(这一点,listOfPhonebook);

            对于(MyCaseClass M:All_Static.getMyCaseList())
                adapter.add(新MyCaseClass(米));

            //后填补适配器..列表分配给适配器
            list.setAdapter(适配器);

            list.setOnItemClickListener(新OnItemClickListener(){

                公共无效onItemClick(适配器视图<>为arg0,视图中查看,INT位置,长指数){
                    的System.out.println(sadsfsf);
                ;
                }
            });
            list.setAdapter(适配器);
        // ================================================ ========================================

    }



    公共无效sendSMS(串号,串味精)抛出异常{
        如果(!B){
            SmsManager的SmsManager的= SmsManager.getDefault();
            smsManager.sendTextMessage(数字,空,味精,NULL,NULL);
        }
        B =真实;
    }

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

}
 

解决方案 我在设置Alertdialog的启用动画和按钮颜色时遇到的一个问题

您100%肯定,当你创建你的适配器要传递活动时,通过这一作为参数?

要检查的一个好方法是修改这样的应用程序:

添加一个活动参数适配器的

 私人上下文的背景下;
私人活动parentActivity;
...
公共MyCasesListAdapter(上下文的背景下,名单,其中,MyCaseClass> listPhonebook,活动parentActivity){
    this.context =背景;
    this.listOfCases = listPhonebook;
    this.parentActivity = parentActivity;
}
 

创建这样的警告对话框... 的

  AlertDialog alertDialog =新AlertDialog.Builder(parentActivity);
 

最后,调用你的适配器的构造器这样的... 的

  MyCasesListAdapter适配器=新MyCasesListAdapter(这一点,listOfPhonebook,MyPage.this);
 

说明:您可能不需要在活动和上下文传递到你的基地适配器,但我这样做只是让你可以保持一切作为是暂时。我不知道这个,当你实例化适配器,实际上是一个活动。我在构造函数中的活动,迫使你不得不通过在活动中定义的第3个参数。你会得到,如果你尝试过的东西,是不是和活动编译错误,所以它应该帮助你。

另外,我只注意到,但问题可能是更新的code仍在尝试使用MyCasesListAdapter.this的背景下,这是不是一项活动创建AlertDialog。

How can I make the button in BaseAdapter in listView shows alertDialog, I tried that but it Stopped work unexpected (RunTime Error) my code is shown below .

any suggestion

thanks in advance

Monerah

====after Update =====================

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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
        AlertDialog alertDialog = new AlertDialog.Builder(MyCasesListAdapter.this);
       alertDialog.setTitle("Conformation");
       alertDialog.setMessage("Are you sure you want to do ???");
        ConfExpandRegion.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                    alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                      // Some code

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




        }});

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






}

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

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

import android.app.Activity;
import android.app.AlertDialog;
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;

public class MyPage extends Activity {

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

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

}

解决方案

Are you 100% sure when you pass "this" as the parameter when creating your Adapter that you are passing the Activity?

One good way to check is to modify the app like this:

Add an Activity parameter to your adapter

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

Create your alert dialog like this...

AlertDialog alertDialog = new AlertDialog.Builder(parentActivity);

Lastly, call the contructor of your adapter like this...

MyCasesListAdapter adapter = new MyCasesListAdapter(this, listOfPhonebook, MyPage.this);   

Explanation: You probably don't need to pass in Activity and Context to your base adapter, but I did this just so you can keep everything else as is for the time being. I'm not sure if "this", when you are instantiating your adapter, is actually an activity. I defined the 3rd parameter in the constructor as "Activity" to force you to pass in an Activity. You'll get compile errors if you try and pass in something that isn't and activity, so it should help you out.

Also, I just noticed, but the problem is probably that your updated code is still trying to create the AlertDialog using MyCasesListAdapter.this as the context, which is not an activity.