android - 使用简单的适配器在带有图像和文本的 ListView 中设置 OnClickListener?

标签 android listview onclick onclicklistener simpleadapter

我在 android 中使用简单的适配器制作带有图像和文本的 ListView 。但我厌倦了让 OnClickListener 使用 Intent 切换其他 Activity 。例如,我点击印度, Activity 切换到 IndiaActivity.java 等...请帮助我..谢谢! (对不起英语不好)

我的代码:

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);
    }

}

最佳答案

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


  }
});

关于android - 使用简单的适配器在带有图像和文本的 ListView 中设置 OnClickListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12661705/

相关文章:

android - 无效路径错误 : get the external memory size

android - ListView Activity OnItemClickListener TextView 的空指针异常

asp.net - 从代码隐藏访问 ListView 项模板

android - 如何在android中获取视频缩略图?

android - 带有 getView() 的 ListView 由于不断的 GC 而过度缓慢?

android - 更改包名称时出现奇怪的解析问题

c# - 删除 ListViewItem 集合中所有选定项目的最有效方法是什么?

javascript - 将变量从复选框传递给 onclick 函数

android - 单击动画上的事件 ImageView 仅在开始位置触发

Javascript滚动到带有动画onclick的元素