Android 是否使用简单适配器在列表视图中使用图像和文本设置OnClickListener?
我在android中使用简单的适配器制作带有图像和文本的列表视图。但我厌倦了让OnClickListener使用intent切换其他活动。例如,我单击India,活动切换到IndiaActivity.java等。。。请帮帮我。。谢谢(对不起,英语不好) 我的代码:Android 是否使用简单适配器在列表视图中使用图像和文本设置OnClickListener?,android,listview,onclick,onclicklistener,simpleadapter,Android,Listview,Onclick,Onclicklistener,Simpleadapter,我在android中使用简单的适配器制作带有图像和文本的列表视图。但我厌倦了让OnClickListener使用intent切换其他活动。例如,我单击India,活动切换到IndiaActivity.java等。。。请帮帮我。。谢谢(对不起,英语不好) 我的代码: public class MainActivity extends Activity { // Array of strings storing country names String[] countries =
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);
}
}
公共类MainActivity扩展活动{
//存储国家名称的字符串数组
字符串[]国家/地区=新字符串[]{
“印度”,
“巴基斯坦”,
“斯里兰卡”,
“中国”,
“孟加拉国”,
“尼泊尔”,
“阿富汗”,
“朝鲜”,
“韩国”,
“日本”
};
//整数数组指向存储在/res/drawable ldpi中的图像/
int[]标志=新的int[]{
R.drawable.印度,
R.drawable.巴基斯坦,
R.drawable.srilanka,
R.drawable.china,
R.drawable.孟加拉国,
R.drawable.尼泊尔,
R.drawable.阿富汗,
R.drawable.nkorea,
R.drawable.skorea,
日本
};
//存储货币的字符串数组
字符串[]货币=新字符串[]{
“印度卢比”,
“巴基斯坦卢比”,
“斯里兰卡卢比”,
“人民币”,
“孟加拉国塔卡”,
“尼泊尔卢比”,
“阿富汗人”,
“朝鲜赢了”,
“韩元”,
“日元”
};
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//列表中的每一行存储国家名称、货币和国旗
列表列表=新的ArrayList();
对于(int i=0;iThanks!但是你能给出我的示例来实现if条件以选择将要运行的活动吗?抱歉,伙计,请探索然后询问..你只需要使用此项(对象)与字符串和相应对象的强制转换相匹配,抱歉,非常感谢4 u回答..我可以探索你的答案..)谢谢!但是你能给我一个例子来实现如果条件来选择将要运行的活动吗?对不起,伙计,请探索然后问..你只需要使用这个项目(对象)来匹配相应的字符串和相应的objectsOk@arbit,对不起,非常感谢4个你的答案..我可以浏览你的答案..:)
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
}
});