2016-04-14 40 views
0

我卡在一點。 我想添加彈出菜單到自定義列表視圖中的每個項目。 我已經試過,但只在最後一個位置或list.I的最後一個項目雨後春筍般冒出來我得到它之所以在最後的位置來,但沒有得到解決 我在這裏添加適配器類如何將彈出菜單添加到自定義列表視圖到每個項目android

public class InterestLevelAdapterEditable extends BaseAdapter { 

    private Activity activity; 
    private TextView level; 
    private InterestLevel m; 
    private LayoutInflater inflater; 
    private List<InterestLevel> interestLevelList; 

    public InterestLevelAdapterEditable(Activity activity, List<InterestLevel> interestLevelList) { 
     this.activity = activity; 
     this.interestLevelList = interestLevelList; 
    } 

    @Override 
    public int getCount() { 
     return interestLevelList.size(); 
    } 

    @Override 
    public Object getItem(int location) { 
     return interestLevelList.get(location); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     if (inflater == null) 
      inflater = (LayoutInflater) activity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) 
      convertView = inflater.inflate(R.layout.interest_level_editable, null); 


     TextView sports_name = (TextView) convertView.findViewById(R.id.sportsName); 
     level = (TextView) convertView.findViewById(R.id.level); 

     m = interestLevelList.get(position); 

     sports_name.setText(m.getSports_name()); 

     level.setText(m.getLevel()); 
     level.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //Creating the instance of PopupMenu 
       PopupMenu popup = new PopupMenu(activity, level); 
       //Inflating the Popup using xml file 
       popup.getMenuInflater().inflate(R.menu.popup_level, popup.getMenu()); 

       //registering popup with OnMenuItemClickListener 
       popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
        public boolean onMenuItemClick(MenuItem item) { 
         level.setText(item.getTitle()); 
         return true; 
        } 
       }); 

       popup.show();//showing popup menu 
      } 
     }); 

     return convertView; 
    } 

} 

我得到的理由是,我將不得不通過點擊監聽器的位置,但沒有得到我應該如何添加位置或(具有位置的InterestLevel的m對象)。

在此先感謝。 Screenshot

回答

0

創建PopupMenusetOnClickListener()show()onClick() -

  //Creating the instance of PopupMenu 
      final PopupMenu popup = new PopupMenu(activity, level); 
      //Inflating the Popup using xml file 
      popup.getMenuInflater().inflate(R.menu.popup_level, popup.getMenu()); 

      //registering popup with OnMenuItemClickListener 
      popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
       public boolean onMenuItemClick(MenuItem item) { 
        level.setText(item.getTitle()); 
        return true; 
       } 
      }); 

level.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) {   

      popup.show();//showing popup menu 
     } 
    }); 
+0

感謝Shadab的回答,但這種方法並沒有更新列表項的值到每個位置時,它僅更新最後列表項的值,因爲它因此我們需要通過在每個位置更新項目文本的位置。 – user3679536

0

我需要彈出菜單像列表視圖。我試試這個代碼,它是正確的(reference):

private static final String TITLE = "title"; 
private static final String ICON = "icon"; 

private List<HashMap<String, Object>> data = new ArrayList<HashMap<String, 
Object>>(); 

// Use this to add items to the list that the ListPopupWindow will use 
private void addItem(String title, int iconResourceId) { 
HashMap<String, Object> map = new HashMap<String, Object>(); 
map.put(TITLE, title); 
map.put(ICON, iconResourceId); 
data.add(map); 
} 

// Call this when you want to show the ListPopupWindow 
private void showListMenu(View anchor) { 
ListPopupWindow popupWindow = new ListPopupWindow(this); 

ListAdapter adapter = new SimpleAdapter(
     this, 
     data, 
     android.R.layout.activity_list_item, // You may want to use your own cool layout 
     new String[] {TITLE, ICON}, // These are just the keys that the data 
uses 
     new int[] {android.R.id.text1, android.R.id.icon}); // The view ids 
to map the data to 


popupWindow.setAnchorView(anchor); 
popupWindow.setAdapter(adapter); 
popupWindow.setWidth(400); // note: don't use pixels, use a dimen resource 
popupWindow.setOnItemClickListener(myListener); // the callback for when a list item is selected 
popupWindow.show(); 
} 

custom layout and work as listview

相關問題