0

동적으로 팝업 메뉴를 만들면 항목이 웹 서비스에서 채워집니다.BaseAdapter 클래스에서 Activity Java 클래스 내부의 List <> 배열에 액세스하는 방법

상황에 따라 구문 분석은 MainActivity.java에 있었지만 팝업 메뉴는 BaseAdapter.java 클래스 안에 있습니다. 내가 listMenuItems.add(strMenuItemNames)으로 List<String> listMenuItems 내부에 항목을 추가하고

try { 
     JSONArray jsonArray = new JSONArray(menuItemsResponse.toString()); 
     for (int i = 0; i < jsonArray.length(); i++){ 
     JSONObject object = jsonArray.getJSONObject(i); 
     String strMenuItemNames = object.getString("Name"); 

     listMenuItems.add(strMenuItemNames); 
       } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

: 나는 아래의 코드를 참조 호야 MainActivity.java 내부 배열 내에서 모든 메뉴 항목을 추가하고있다.

이제 BaseAdapter 클래스에서 listMenuitems에 액세스하려고합니다. 솔루션

PopupMenu popupMenu = new PopupMenu(activity, imgDropDown); 
popupMenu.getMenu().add() // array to be added here 


popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener({ 
    // on click events for each item 
}); 

누구나 대응하시기 바랍니다 : 아래 BaseAdapter 클래스의 getView() 방법 내부 팝업 메뉴에 대한 내 코드입니다.

미리 감사드립니다.

+0

getItem (int position)이라는 메서드가 있는데이 메서드에서 동일한 인스턴스를 반환하고 adapter.getItem ()을 호출 할 수 있습니다. –

답변

0

1. 구성 방법에 사용해야하는 목록 정보를 추가하십시오. 코드에서 그 구석 구석

List<String> listMenuItems; 
public MyAdapter(List<String> listMenuItems, Context context) { 
    ... 
} 

.

popupMenu.getMenu().add(listMenuItems.get(position)); 

시도해보십시오.

public class MyAdapter extends BaseAdapter { 

    List<String> listMenuItems; 
    private LayoutInflater inflater; 
    private Context context; 

    public MyAdapter(List<String> listMenuItems, Context context) { 
     this.context = context; 
     this.listMenuItems = listMenuItems; 
     this.inflater = LayoutInflater.from(context); 
    } 

    ... 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = inflater.inflate(R.layout.your_layout, null); 
     ... 

     PopupMenu popupMenu = new PopupMenu(activity, imgDropDown); 
     // edited here 
     popupMenu.getMenu().add(listMenuItems.get(position)); // array to be added here 
     popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener({ 
       // on click events for each item 
     }); 
     return view; 
    } 
} 
+0

이것이 작동하지 않았습니다 .. –

+0

@GRK의 답을 시도해 볼 수 있습니다. – KeLiuyue

0

당신은

interface MenuItemProvider { 
    getMenuItems(); // add parameters, returntype based on your need. 
} 

활동이 인터페이스를 구현하고 활성으로 제조하는 많은 ListItems 반환하여 getMenuItems() 메소드를 구현 아래와 같은 인터페이스를 정의 할 수있다.

이제 어댑터에이 인터페이스를 설정하는 설정 메소드가 있어야합니다.

class yourActivity extends <> implements MenuItemProvider { 

// other implementation. 

// pass this implementation to your base adapter. 
baseadapterinstance.setMenuItemProvider(this); 


getMenuItems(){ 
// return list items. 
} 



    } 


/*** BaseAdpater class. ***/ 
private MenuItemProvider menuItemProviderImpl; 

void setMenuItemProvider(MenuItemProvider menuItemProviderImpl){ 
this. menuItemProviderImpl = menuItemProviderImpl; 
} 

// when you need to get the list, call 
menuItemProviderImpl.getMenuItems(); 

희망이 있습니다.