2015-04-01 121 views
0

我想表明POPUPMENU onLongClick在Android的,所以我寫:如何將popupMenu添加到Android的listView項目中?

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    listView = (ListView) findViewById(R.id.listView); 
    adapter = new MyAdapter(getApplicationContext()); 

} 

@Override 
protected void onResume() { 
    super.onResume(); 
    connectToDatabase(); 
} 

private void connectToDatabase() { 
    //Create database 
     listView.setAdapter(adapter); 
    } 

我的適配器:

 @Override 
    public View getView(final int position, View convertView, final ViewGroup parent) { 
     final MyHolder holder; 
     if (convertView == null) { 
      convertView = layoutInflater.inflate(R.layout.list_row, null); 
      holder = new MyHolder(); 
      holder.vocabs = (TextView) convertView.findViewById(R.id.textViewVocabs); 
      holder.points = (TextView) convertView.findViewById(R.id.textViewPoints); 
      holder.tick = (ImageButton) convertView.findViewById(R.id.tick); 
      holder.cross = (ImageButton) convertView.findViewById(R.id.cross); 
      convertView.setTag(holder); 
     } else { 
      holder = (MyHolder) convertView.getTag(); 
     } 

     holder.id = id.get(position); 
     holder.vocabs.setText(vocabs.get(position)); 
     holder.points.setText(points.get(position) + ""); 

     holder.tick.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //Toast.makeText(parent.getContext(), "Yes... :D ... You learn it :D", Toast.LENGTH_SHORT).show(); 
       points.set(position, points.get(position) + 1); 
       database.execSQL("UPDATE VOCABS SET POINTS=" + points.get(position) + " WHERE ID=" + id.get(position) + ";"); 
       notifyDataSetChanged(); 
      } 
     }); 

     holder.cross.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //Toast.makeText(parent.getContext(), "Yes... :D ... You learn it :D", Toast.LENGTH_SHORT).show(); 
       if (points.get(position) != 0) { 
        points.set(position, points.get(position) - 1); 
        database.execSQL("UPDATE VOCABS SET POINTS=" + points.get(position) + " WHERE ID=" + id.get(position) + ";"); 
       } 
       notifyDataSetChanged(); 
      } 
     }); 

     convertView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(parent.getContext(), means.get(position), Toast.LENGTH_LONG).show(); 
      } 
     }); 

     final View finalConvertView = convertView; 
     convertView.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       //popup-menu for delete item. 
       PopupMenu popupMenu = new PopupMenu(parent.getContext(),finalConvertView); 
       popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu()); 
       popupMenu.show(); 

       popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
        @Override 
        public boolean onMenuItemClick(MenuItem item) { 
         if (item.getTitle().equals("Delete")) { 
          database.execSQL("DELETE FROM VOCABS WHERE ID=" + holder.id + ";"); 
          id.remove(position); 
          vocabs.remove(position); 
          means.remove(position); 
          points.remove(position); 
          notifyDataSetChanged(); 
         } 
         return false; 
        } 
       }); 
       //Toast.makeText(parent.getContext(), "onLongClickFunction! " + position, Toast.LENGTH_LONG).show(); 
       return false; 
      } 
     }); 
     return convertView; 
    } 

我Itellij IDEA 13.1.4工作。我添加appcompat以支持比11歲以前的API更高的popup_Menu。現在,當我運行應用程序時,Everythings都可以。但是當長按一下listView中的一個項目時得到下面的錯誤:

04-01 15:50:29.749 17078-17078/net.motameni.apps.vocabs_box E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: net.motameni.apps.vocabs_box, PID: 17078 
java.lang.RuntimeException: Failed to resolve attribute at index 6 
     at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:603) 
     at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6423) 
     at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6591) 
     at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:735) 
     at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:679) 
     at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:62) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
     at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:370) 
     at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:219) 
     at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:153) 
     at android.support.v7.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:125) 
     at android.support.v7.widget.PopupMenu.show(PopupMenu.java:193) 
     at net.motameni.apps.vocabs_box.MainActivity$MyAdapter$4.onLongClick(MainActivity.java:154) 
     at android.view.View.performLongClick(View.java:4795) 
     at android.view.View$CheckForLongPress.run(View.java:19723) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5221) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

什麼是錯誤?

+0

Peales發佈popup_menu xml文件。 – 2015-04-02 21:48:38

回答

1

我懷疑調用PopupMenu構造函數時沒有獲得正確的視圖。

更改代碼:

PopupMenu popupMenu = new PopupMenu(parent.getContext(),finalConvertView); 

TO:

PopupMenu popupMenu = new PopupMenu(parent.getContext(), v); 

我承認也許是相同的觀點,但是這取決於佈局設計。 TO下的建議代碼更準確。 View參數v指向ListView中的某個行項目。

請張貼您的菜單xml R.menu.popup_menu也。

+0

@Ali,您也可以通過點擊答案旁邊的向上箭頭圖標,將發佈的答案投票爲「有用」。祝你有美好的一天... – 2015-04-04 16:54:18

相關問題