2012-09-30 3 views
1

안드로이드에서 간단한 어댑터를 사용하여 이미지와 텍스트로 목록보기를 만듭니다. 그러나 나는 의도를 사용하여 다른 활동을 전환하기 위해 OnClickListener를 사용하는 것에 지쳤다. 예를 들어, IndiaActivity.java 등으로 인도, 활동 전환을 클릭하십시오 ... 제발 도와주세요 .. 고마워!간단한 어댑터를 사용하여 이미지와 텍스트가있는 목록보기의 OnClickListener를 설정 하시겠습니까?

public class MainActivity extends Activity { 

    // Array of strings storing country names 
    String[] countries = new String[] { 
     "India", 
     "Pakistan", 
     "Sri Lanka", 
     "China", 
     "Bangladesh", 
     "Nepal", 
     "Afghanistan", 
     "North Korea", 
     "South Korea", 
     "Japan" 
    }; 

    // Array of integers points to images stored in /res/drawable-ldpi/ 
    int[] flags = new int[]{ 
     R.drawable.india, 
     R.drawable.pakistan, 
     R.drawable.srilanka, 
     R.drawable.china, 
     R.drawable.bangladesh, 
     R.drawable.nepal, 
     R.drawable.afghanistan, 
     R.drawable.nkorea, 
     R.drawable.skorea, 
     R.drawable.japan 
    }; 

    // Array of strings to store currencies 
    String[] currency = new String[]{ 
     "Indian Rupee", 
     "Pakistani Rupee", 
     "Sri Lankan Rupee", 
     "Renminbi", 
     "Bangladeshi Taka", 
     "Nepalese Rupee", 
     "Afghani", 
     "North Korean Won", 
     "South Korean Won", 
     "Japanese Yen" 
    }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Each row in the list stores country name, currency and flag 
     List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>(); 

     for(int i=0;i<10;i++){ 
      HashMap<String, String> hm = new HashMap<String,String>(); 
      hm.put("txt", "Country : " + countries[i]); 
      hm.put("cur","Currency : " + currency[i]); 
      hm.put("flag", Integer.toString(flags[i])); 
      aList.add(hm); 
     } 

     // Keys used in Hashmap 
     String[] from = { "flag","txt","cur" }; 

     // Ids of views in listview_layout 
     int[] to = { R.id.flag,R.id.txt,R.id.cur}; 

     // Instantiating an adapter to store each items 
     // R.layout.listview_layout defines the layout of each item 
     SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to); 

     // Getting a reference to listview of main.xml layout file 
     ListView listView = (ListView) findViewById(R.id.listview); 

     // Setting the adapter to the listView 
     listView.setAdapter(adapter); 
    } 

} 

답변

1
listView.setOnItemClickListener(new OnItemClickListener() { 

    public void onItemClick(AdapterView adapterView, View view, int position, long id) { 
    SimpleAdapter adapter = (SimpleAdapter) adapterView.getAdapter(); 
    ListView currentLv = (ListView) view; 

    Object item = adapter.getItem(position); 
    //Do some more stuff here and launch new activity 


    } 
}); 
+0

감사 :

내 코드 (영어 죄송합니다 나쁜)! 하지만 실행할 수있는 활동을 선택하기위한 조건을 구현하려면 예제를 제공 할 수 있습니까? –

+0

죄송합니다. 친구는 탐구하고 나서 물어보십시오. 그냥이 항목 (객체)을 사용하여 문자열과 해당 객체가있는 각각의 캐스팅과 일치해야합니다. –

+0

Ok @Arcit, 죄송합니다. 답변.. :) –

관련 문제