2017-05-15 3 views
2

Android 개발의 그리드보기에서 선택한 항목을 가져 오려고합니다. 클릭 리스너에클릭 수신기의 Android 그리드보기 선택한 항목 받기

private static Integer[] iconlist = { 
     R.drawable.food_icon, R.drawable.transport_icon, R.drawable.entertainment_icon, 
     R.drawable.bill_icon, R.drawable.others_icon, R.drawable.salary_icon, 
     R.drawable.investment_icon, R.drawable.bonus_icon, R.drawable.medication_icon, 
     R.drawable.drinks_icon, R.drawable.car_icon, R.drawable.mask_icon, 
     R.drawable.shopping_icon, R.drawable.lottery_icon, R.drawable.pet_icon, 
     R.drawable.movie_icon, R.drawable.plant_icon, R.drawable.paint_icon, 
}; 

그리고있는 gridview : : 여기 그리드 뷰를 채우는 데 사용 당김의 목록입니다

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, 
           View v, int position, long id) { 

      Toast.makeText(getActivity(), "Grid Item " + (position + 1) + " Selected", Toast.LENGTH_LONG).show(); 
      int selected = position + 1; 
      for(int i = 0; i < iconlist.length; i++){ 
       if(iconlist[i] == selected){ 

       } 
      } 
     } 
    }); 

내가 클릭에 따라 올바른 정수 위치를 얻을 수 있었다. 내가 뭘 하려는지 선택한 drawable 기반으로, 문자열을 추출하려고합니다.

예를 들어 전송 아이콘을 클릭하면 "R.drawable.transport_icon"에서 "transport_icon"문자열을 가져 와서 문자열로 저장하려고합니다. 그러나 어떻게해야할지 모르겠습니다.

아이디어가 있으십니까?

+0

이 유 문자열로 "" "R.drawable.transport_icon을"이 저장 하시겠습니까? –

+0

아니요, "transport_icon"을 문자열로 저장하려고합니다. – hyperfkcb

+0

첫 번째 제거 위치는 선택한 항목을 가져 오는 +1입니다. iconList도 0 위치부터 시작합니다. –

답변

0

는 첫 번째 정수 배열에서 드로어 블 (ID를)

Drawable drawable = context.getResources().getDrawable(iconlist[n]); 

정의하고이 방법은 당신이 필요로 반환해야이

String name = context.getResources().getResourceEntryName(ID); 

같은 자원의 이름을 얻을 수 있습니다.

+0

getDrawable에는 API 21이 필요하지만 현재 최소값은 16입니다. 또한 문자열을 추출하기 위해 drawable을 int로 구문 분석하는 방법은 무엇입니까? – hyperfkcb

+0

add getResources() –

+0

문자열 이름 = context.getResources(). getResourceEntryName (ID); 이 하나는 내가 대답 테스트에 쓴 및 drawable의 이름을 얻을 것이다 >>> 텍스트 후 R.drawable.TEXT –

관련 문제