2012-04-26 27 views
2

如何使ListView中的BaseAdapter中的按鈕顯示alertDialog,我試過,但它停止工作意外(運行時錯誤)我的代碼如下所示。如何使BaseAdapter顯示AlertDialog android應用程序

任何建議

在此先感謝

Monerah

====更新=====================

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

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

} 
+0

您是否得到了其解答? – KickingLettuce 2013-01-08 17:53:31

回答

0

您必須使用AlertDialog.Builder才能構建AlertDialog

+0

AlertDialog alertDialog = new AlertDialog.Builder(context);我這樣做,有一個語法錯誤 – Monerah 2012-04-26 12:56:03

+0

看看示例[AlertDialogSamples.java](http://developer.android。com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlertDialogSamples.html) – Rajesh 2012-04-26 12:59:42

1

alertDialog甚至沒有初始化的第一件事..所以.. NPE,當你的適配器內創建確保使用activity context and not ApplciatioContext

AlertDialog alertDialog = new AlertDialog.Builder(YourACtivity.this); 

上述行不應該在一流水平..它應該在裏面getView()方法...並使用您的活動實例作爲上下文..類似nameOfYourActivity.this

+0

AlertDialog alertDialog = new AlertDialog.Builder(context);我這樣做,有一個語法錯誤! – Monerah 2012-04-26 12:55:52

+0

@Monerah ..顯示代碼太..並接受一些答案.. – ngesh 2012-04-26 12:57:06

+0

好生病請看看我的更新plz – Monerah 2012-04-26 12:57:43

8

當你通過「this」作爲參數創建你的適配器,你傳遞活動?

一個很好的檢查方法是修改這樣的程序:

添加活動參數適配器

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

創建這樣的警告對話框...

AlertDialog alertDialog = new AlertDialog.Builder(parentActivity); 

最後,調用適配器的構造函數像這樣...

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

說明:您可能不需要在活動和上下文傳遞到你的基地適配器,但我這樣做只是讓你可以保留一切作爲是暫時。我不確定當你實例化你的適配器時,「this」實際上是一個活動。我在構造函數中將第三個參數定義爲「Activity」來強制你傳入一個Activity。如果你嘗試傳遞一些不是和活動的東西,你會得到編譯錯誤,所以它應該會幫助你。

此外,我剛剛注意到,但問題可能是您的更新代碼仍在嘗試使用MyCasesListAdapter.this創建AlertDialog作爲上下文,而不是活動。

1
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getRootView().getContext()); 
    alertDialogBuilder.setMessage("Are you sure you want to delete?"); 
    alertDialogBuilder.setPositiveButton("Ok", 
      new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface arg0, int arg1) { 
        deleteExp(tid); 
       } 
      }); 

    alertDialogBuilder.setNegativeButton("cancel", 
      new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface arg0, int arg1) { 

       } 
      }); 

    AlertDialog alertDialog = alertDialogBuilder.create(); 
    alertDialog.show(); 
相關問題