如何在弹出窗口的listview项上显示popupwindow

时间:2014-01-09 05:13:46

标签: android android-listview

我有一个弹出窗口,里面有一个列表。每个列表项包含一个删除图像。现在我必须在用户点击图像时在删除图像上方打开一个popwindow。所以,基本上它就像popupwindow indside a弹出窗口。当我尝试这样做时,我收到以下错误: -

01-09 10:36:56.860: E/AndroidRuntime(8881): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@419d2ae0 is not valid; is your activity running?
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:705)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:355)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.view.Window$LocalWindowManager.addView(Window.java:559)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.widget.PopupWindow.invokePopup(PopupWindow.java:1013)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.widget.PopupWindow.showAtLocation(PopupWindow.java:856)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.widget.PopupWindow.showAtLocation(PopupWindow.java:820)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at com.sdei.presentationapp.util.PresentationListAdapter.displayRefreshPopUp(PresentationListA dapter.java:211)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at com.sdei.presentationapp.util.PresentationListAdapter$1.onClick(PresentationListAdapter.java:117)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.view.View.performClick(View.java:4232)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.view.View$PerformClick.run(View.java:17318)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.os.Handler.handleCallback(Handler.java:615)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.os.Looper.loop(Looper.java:137)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at android.app.ActivityThread.main(ActivityThread.java:4921)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at java.lang.reflect.Method.invoke(Method.java:511)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-09 10:36:56.860: E/AndroidRuntime(8881):     at dalvik.system.NativeStart.main(Native Method)

我想在最近3天做。请帮助我。

我的代码

 private void showMyPresentationPopup(final Context context, Rect loaction,final ArrayList<SelectedPresentationdata> mSelectedPresentation) {
    int popupWidth = convertDipToPixels(400);
    int popupHeight = convertDipToPixels(500);
    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final   View layout = layoutInflater.inflate(R.layout.presentation, null);
    displayPopUp(layout,popupWidth, popupHeight,myPresentation,location);
    PresentationListAdapter mAdapter = new PresentationListAdapter(DashboardActivity.this,mSelectedPresentation,mPosition,mPresentationList);
    ListView mListView =(ListView)layout.findViewById(R.id.presentationlistid); 
    mListView.setAdapter(mAdapter);
    mListView.setItemsCanFocus(false);
    mListView.setFocusableInTouchMode(false);

    mListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub
            Log.w("mSelectedPresentationList.get(position).get_dataSync()",mSelectedPresentationList.get(position).get_dataSync());
            if(mSelectedPresentationList.get(position).get_dataSync().equalsIgnoreCase("1")){
            PreferenceConnector.writeInteger(getApplicationContext(), PreferenceConnector.SELECTED_POSITION, position);
            Log.i("value to be write in preferences isssssss",position+">>>>>>>>>");
            PreferenceConnector.writeString(getApplicationContext(), PreferenceConnector.SELECTED_PRESSO_ID, mSelectedPresentationList.get(position).get_pId());
            PreferenceConnector.writeString(getApplicationContext(), PreferenceConnector.PRESENTATION_NAME, mSelectedPresentationList.get(position).get_pName());
            PreferenceConnector.writeString(getApplicationContext(), PreferenceConnector.SELECTED_PRESSO_MODE, mSelectedPresentationList.get(position).get_pMode());
            mPresentationName.setText(mSelectedPresentationList.get(position).get_pName());
            String present_id=mSelectedPresentation.get(position).get_pId();
            getSlideInfo(present_id);
            mPresentationList.dismiss();
            }

        }
            }); 


}
// Creating the PopupWindow
public void displayPopUp(View layout,int popupWidth,int popupHeight,View v, Rect location){
mPresentationList = new PopupWindow(mContext);
mPresentationList.setFocusable(true);
mPresentationList.setContentView(layout);
mPresentationList.setWidth(popupWidth);
mPresentationList.setHeight(popupHeight);
mPresentationList.setTouchable(true);
mPresentationList.setBackgroundDrawable(new BitmapDrawable());
mPresentationList.setOutsideTouchable(true);
mCancel=(Button)layout.findViewById(R.id.cancel);
mCancel.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         mPresentationList.dismiss();
    }
});

 mPresentationList.setTouchInterceptor(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
            mPresentationList.dismiss();
            return true;
        }
        return false;
    }

});
mPresentationList.showAsDropDown(v, -convertDipToPixels(20), 0);
}

我的用于popupwindoe Listitem的Adapter类: -

   public class PresentationListAdapter extends BaseAdapter implements BgTaskListener<String>{

Context context;
ArrayList<SelectedPresentationdata> mSelectedPresentation;
private Rect location;
CommonMethod mCommonMethod;
private PopupWindow mRefreshPopUp;
ArrayList<String> mPresentationIdList;
private String mPosition;
private String syncStaus;
private String mUserId;
private PopupWindow mPopUpList;
private String mSelectedPressoId;

public PresentationListAdapter(Context  context,ArrayList<SelectedPresentationdata> mSelectedPresentation,String mPosition,PopupWindow mWindow) {
    // TODO Auto-generated constructor stub
    this.context=context;
    this.mSelectedPresentation=mSelectedPresentation;
    this.mPosition=mPosition;

    Log.d("vie wissssss",mPopUpList+">>>>>>>");
    this.mPopUpList=mWindow;
    mCommonMethod=new CommonMethod(context);
    mUserId=PreferenceConnector.readString(context, PreferenceConnector.USER_ID, "");

}   

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

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return mSelectedPresentation.get(arg0);
}

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

@Override
public View getView(final int pos,  View convertview, final ViewGroup arg2) {
    // TODO Auto-generated method stub
    final Viewholder holder;

    if(convertview==null){  
    LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    convertview=layoutInflater.inflate(R.layout.presentationitem, null,false);
    holder=new Viewholder();

    holder.Title=(TextView)convertview.findViewById(R.id.presentationtitle);
    holder.mLayout=(LinearLayout)convertview.findViewById(R.id.parentLayout);
    holder.mRefresh=(Button)convertview.findViewById(R.id.refreshid);
    holder.mDelete=(Button)convertview.findViewById(R.id.deleteid);
    holder.mTickView=(ImageView)convertview.findViewById(R.id.iv_icon);
    holder.mDelete.setFocusable(false);
    holder.mRefresh.setFocusable(false);
    holder.mDelete.setFocusableInTouchMode(false);
    holder.mRefresh.setFocusableInTouchMode(false);
    convertview.setTag(holder);
    }
    else{
        holder=(Viewholder)convertview.getTag();
    }
        mSelectedPressoId=mSelectedPresentation.get(pos).get_pId();
        holder.Title.setText(mSelectedPresentation.get(pos).get_pName());
        syncStaus=mSelectedPresentation.get(pos).get_dataSync();
        Log.i("syncing tag value issssssssss",mSelectedPresentation.get(pos).get_dataSync()+">>>>>>>>>>>>>>>>>.");
        Log.e("position  isssssss",mPosition+">>>>>>>>>"+pos);

        holder.mDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                LayoutInflater layoutInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final   View layout = layoutInflater.inflate(R.layout.delete_dialog, null);

                 displayRefreshPopUp(layout, 200, 200, v, location);
            }
        });


        if(syncStaus.equalsIgnoreCase("0")){
         holder.mRefresh.setVisibility(View.VISIBLE);
         holder.mRefresh.setBackgroundResource(R.drawable.refresh_icon);
         Log.v("size of popup isssss",mPopUpList.getHeight()+">>>>>>>>");
         holder.mRefresh.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Log.i("position clickjed of refresh icon and pressso is to be synced isssssss",pos+">>>>>"+mSelectedPresentation.get(pos).get_pId());
                    new SendNotificationStatus(context, mSelectedPresentation.get(pos).get_pId(), mUserId, "1", PresentationListAdapter.this).execute();

                    //new GetPresentationData(context, mUserId, mSelectedPresentation.get(pos).get_pId(), PresentationListAdapter.this).execute();

                    mPopUpList.dismiss();

                }
            });
          }else{
            holder.mRefresh.setBackgroundResource(R.drawable.refresh_black_icon);

        }
        if(mPosition.equalsIgnoreCase(mSelectedPressoId)){
            Log.i("position inside if isssssss",mPosition+">>>>>>>>>");
             holder.Title.setTextColor(Color.BLACK);
             holder.mTickView.setVisibility(View.VISIBLE);
             holder.mLayout.setBackgroundColor(R.color.dark_grey);
             holder.mRefresh.setVisibility(View.VISIBLE);
             holder.mDelete.setVisibility(View.VISIBLE);
        }else{
            if(syncStaus.equalsIgnoreCase("0")){
                 holder.mRefresh.setVisibility(View.VISIBLE);
                  holder.mRefresh.setBackgroundResource(R.drawable.refresh_icon);
             }else{
                 holder.mRefresh.setVisibility(View.GONE); 
             }
            holder.Title.setTextColor(R.color.grey);
             holder.mTickView.setVisibility(View.GONE);
             holder.mLayout.setBackgroundColor(0x00000000);

             holder.mDelete.setVisibility(View.GONE);
        }
        //convertview.setTag(R.string.close, mSelectedPresentation.get(pos));
    return convertview;
}

static class Viewholder{

    TextView Title;
    Button mRefresh;
    Button mDelete;
    LinearLayout mLayout;
    ImageView mTickView;


}


@Override
public void OnTaskComplete(String t) {
    // TODO Auto-generated method stub

}

@Override
public void OnTaskFailure(String t) {
    // TODO Auto-generated method stub

}
// Creating the PopupWindow
public void displayRefreshPopUp(View layout,int popupWidth,int popupHeight,View v, Rect location){
    mRefreshPopUp = new PopupWindow(context);
    mRefreshPopUp.setContentView(layout);
    mRefreshPopUp.setWidth(popupWidth);
    mRefreshPopUp.setHeight(popupHeight);
    mRefreshPopUp.setTouchable(true);
    mRefreshPopUp.setFocusable(true);
    mRefreshPopUp.setBackgroundDrawable(new BitmapDrawable());
    mRefreshPopUp.setOutsideTouchable(true);
    mRefreshPopUp.setTouchInterceptor(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
            mRefreshPopUp.dismiss();
            return true;
        }
        return false;
    }
});
    //mRefreshPopUp.showAsDropDown(v, -convertDipToPixels(20), 0);
    mRefreshPopUp.showAtLocation(v, Gravity.TOP, 0, 0);
   }


  }

1 个答案:

答案 0 :(得分:0)

声明活动

 Activity activity;

在构造函数中传递活动

 public PresentationListAdapter(Activity activity,Context  context,ArrayList<SelectedPresentationdata> mSelectedPresentation,String mPosition,PopupWindow mWindow) {
// TODO Auto-generated constructor stub
 this.activity=activity;
this.context=context;
this.mSelectedPresentation=mSelectedPresentation;
this.mPosition=mPosition;

Log.d("vie wissssss",mPopUpList+">>>>>>>");
this.mPopUpList=mWindow;
mCommonMethod=new CommonMethod(context);
mUserId=PreferenceConnector.readString(context, PreferenceConnector.USER_ID, "");

 }  

您的PopupWindow代码

 public void displayRefreshPopUp(View layout,int popupWidth,int popupHeight,View v, Rect location){
mRefreshPopUp = new PopupWindow(activity);
mRefreshPopUp.setContentView(layout);
mRefreshPopUp.setWidth(popupWidth);
mRefreshPopUp.setHeight(popupHeight);
mRefreshPopUp.setTouchable(true);
mRefreshPopUp.setFocusable(true);
mRefreshPopUp.setBackgroundDrawable(new BitmapDrawable());
mRefreshPopUp.setOutsideTouchable(true);
mRefreshPopUp.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
        mRefreshPopUp.dismiss();
        return true;
    }
    return false;
}
  });
 //mRefreshPopUp.showAsDropDown(v, -convertDipToPixels(20), 0);
   mRefreshPopUp.showAtLocation(v, Gravity.TOP, 0, 0);
    }