2013-05-12 30 views
1

我有一個自定義適配器,用於其單元包含刪除按鈕的listview。 我想顯示一條消息「真的刪除此條目嗎?」當按下刪除時。從自定義適配器打開對話框

我從自定義適配器的內部處理刪除按鈕事件,問題是,爲了創建一個對話框,我需要對當前活動的引用......我該如何解決這個問題? 只是使用上下文導致我空指針異常。

+1

沒有你寄你的活動範圍內的適配器構造? – 2013-05-12 11:38:04

回答

2

這應該是你的適配器,使活動能夠直傳上下文適配器骨架..

public class ImagePrepare extends BaseAdapter { 

    private Context ctx; 

     public ImagePrepare(Context ctx, String[] icons,DisplayMetrics m) { 
     // TODO Auto-generated constructor stub 
     this.ctx=ctx; 

    } 
    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return lengthofdata; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return data[position]; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public int getItemViewType(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

    } 
    } 
3

將這個代碼...

AlertDialog.Builder alertDialog = new AlertDialog.Builder((Activity) v.getContext()); 

      alertDialog.setTitle("Delete this item?"); 
      alertDialog.setMessage("Are you sure you want to delete this?"); 
      alertDialog.setIcon(R.drawable.icon); 

      alertDialog.setPositiveButton(
       "Delete", 
       new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          // Do the stuff.. 
         } 
        } 
       ); 


      } 

      alertDialog.show(); 

你的列表視圖點擊收聽:

@Override 
    public void onListItemClick(ListView lv, View v, int position, long id) { 

} 

是你要找的東西?

1

你可以這樣使用。

lv1.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 

       LayoutInflater layout = (LayoutInflater) getBaseContext() 
         .getSystemService(LAYOUT_INFLATER_SERVICE); 
       View popupView = layout.inflate(R.layout.popup1, null); 
       Display display = getWindowManager().getDefaultDisplay(); 
       int height = display.getHeight(); 
       int width = display.getWidth(); 
       final PopupWindow popupWindaow = new PopupWindow(popupView, 
         (int) (width/1.4), (int) (height/2.5)); 
       popupWindaow.showAtLocation(popupView, Gravity.CENTER, 0, 0); 

       TextView tv1 = (TextView) popupView 
         .findViewById(R.id.textView1);         

       tv1.setText("Really delete this entry?"); 

       Button No = (Button) popupView.findViewById(R.id.No); 
          Button Yes = (Button) popupView.findViewById(R.id.Yes); 
       Yes.setOnClickListener(new OnClickListener() { 

        public void onClick(View arg0) { 
         //code to delete 

        } 
       }); 

       No.setOnClickListener(new OnClickListener() { 

        public void onClick(View arg0) { 
         popupWindaow.dismiss(); 

        } 
       }); 

      } 
     }); 

我希望這會幫助你。

相關問題