listview的每行单击事件

时间:2021-06-03 05:25:13
package com.Fxc.shanghai;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract.Data;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class FirstActivity extends Activity implements OnItemClickListener{
Button b1,b2,b3,b4;
ListView lv;
SimpleAdapter sa1,sa2,sa3,sa4;
List<HashMap<String, Object>> list1,list2,list3,list4;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.first_main);

b1=(Button) findViewById(R.id.button1);
b2=(Button) findViewById(R.id.button2);
b3=(Button) findViewById(R.id.button3);
b4=(Button) findViewById(R.id.button4);


lv=(ListView) findViewById(R.id.listView1);
lv.setOnItemClickListener(this);



String s[]={"imageView1","textView1","textView2","textView3","textView4"};
int i[]={R.id.imageView1,R.id.textView1,R.id.textView2,R.id.textView3,R.id.textView4};
//String x[]={"imageView1","textView1","textView2"};
//int c[]={R.id.imageView1,R.id.textView1,R.id.textView2};

String info1[]={"宜兴乐园","西施洞","灵谷洞","龙池山风景区","龙背山","张公洞","善卷洞风景区","玉女山庄","竹海风景区"};
String info2[]={"国家5A级  价格:免费","国家5A级  价格:¥40","国家4A级  价格:¥120","国家5A级  价格:¥120",
"国家4A级  价格:免费","国家4A级  价格:¥20","国家4A级  价格:¥160","国家5A级  价格:¥20","国家5A级  价格:¥180"};
String info3[]={"宜兴团氿风景区","江苏宜兴市湖父镇","灵谷洞在宜兴城西南约30...","龙池山风景区位于宜兴西城","龙背山位于宜兴城","位于宜兴城西南约22公里"
,"位于宜兴城","坐落在张公洞","宜兴市"};
String info4[]={"风景名胜","风景名胜","民族文化","风景名胜","风景名胜","风景名胜","水上运动","风景名胜","风景名胜"};

list1=new ArrayList<HashMap<String,Object>>();

for(int j=0;j<9;j++){
HashMap<String, Object> map=new HashMap<String, Object>();
map.put("imageView1", R.drawable.ic_launcher);
map.put("textView1", info1[j]);
map.put("textView2", info2[j]);
map.put("textView3", info3[j]);
map.put("textView4", info4[j]);

list1.add(map);
}


sa1=new SimpleAdapter(this, list1, R.layout.item, s, i);
lv.setAdapter(sa1);



list2=new ArrayList<HashMap<String,Object>>();


String info5[]={"粉肠猪肝","红烧牛腩","狗地芽煎蛋","唱歌鱼","苗家酸汤鱼","羊肉米线","回锅肉","花江狗肉","辣子鸡","盐菜炒汤圆","青椒土豆丝","炖土鸡","瀑布河鱼","太安鱼","红烧肉","蕨菜腊肉","纸包鸡","地方土鸡","牛肉面"};
String info6[]={"价格:¥30","价格:¥75","价格:¥30","价格:¥68","价格:¥68","价格:¥10","价格:¥35","价格:¥80","价格:¥68","价格:¥48","价格:¥10","价格:¥58","价格:¥88","价格:¥68","价格:¥68","价格:¥28","价格:¥35","价格:¥168","价格:¥10"};
String info7[]={"黄果树新城","黄果树新城","黄果树新城","黄果树新城","黄果树新城","在景区停车场出口处","黄果树新城","黄果树新城","黄果树新城","黄果树新城","黄果树新城","黄果树新城","黄果树新城","黄果树新城","黄果树新城","黄果树新城","黄果树新城","黄果树新城","黄果树新城"};
String info8[]={"本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色","本地特色"};

for(int j=0;j<19;j++){
HashMap<String, Object> map1=new HashMap<String, Object>();
map1.put("imageView1", R.drawable.ic_launcher);
map1.put("textView1", info5[j]);
map1.put("textView2", info6[j]);
map1.put("textView3", info7[j]);
map1.put("textView4", info8[j]);

list2.add(map1);
}


sa2=new SimpleAdapter(this, list2, R.layout.item, s, i);


list3=new ArrayList<HashMap<String,Object>>();

String info9[]={"悦立达大酒店","神奇福运大酒店","天瀑大酒店","威斯汀风尚酒店","凯旋大酒店","君临大酒店","驾乘公寓大酒店","兴源大酒店","金谷瀑布山庄","安都宾馆","会宾酒店","贵宾酒店","悦宾旅馆","名瀑休闲山庄","龙宫酒店","屯堡酒店"};
String info0[]={"中华西路和中华北路交叉口","汽车南站","贵州省安顺市客车南站,距离2公里","贵州省安顺市镇宁布依族苗族自治县红星大道(...","西秀  南马广场","距黄果树机场9.3公里","黄果树景 新城票务中心左侧100米","距离黄果树票务大厅300米","黄果树大瀑布仅300米","黄果树风景区洒把新村中段","黄果树新城","距黄果树新城票务中心300米处","距黄果树票务大厅300米","距黄果树票务大厅300米","龙宫景区售票大厅入口处下行100米","距黄果树景 2.5公里"};

for(int k=0;k<16;k++){
HashMap<String, Object> map2=new HashMap<String, Object>();
map2.put("imageView1", R.drawable.ic_launcher);
map2.put("textView1", info9[k]);
map2.put("textView3", info0[k]);

list3.add(map2);
}
sa3=new SimpleAdapter(this,list3,R.layout.item,s,i);

list4=new ArrayList<HashMap<String,Object>>();

String info11[]={"葵花籽","杜仲","波波糖","根雕","蜡染","牛角梳","茅台酒","杜仲茶","蝴蝶手镯","石雕","特色草帽"};
String info12[]={"价格:¥10","价格:¥100","价格:¥10","价格:¥3600","价格:¥20","价格:¥20","价格:¥1519","价格:¥30","价格:¥480","价格:¥100","价格:¥15"};
String info13[]={"黄果树大瀑布出口处","黄果树大瀑布出口处","黄果树大瀑布出口处","黄果树大瀑布出口处","黄果树大瀑布出口处","黄果树大瀑布出口处","黄果树大瀑布出口处","黄果树大瀑布出口处","黄果树大瀑布由下至上乘坐点","黄果树大瀑布出口处","陡坡塘瀑布检票口对面"};
String info14[]={"民俗工艺","藏药","本地特色","民俗工艺","民俗工艺","民俗工艺","土特产","土特产","民俗工艺","民俗工艺","民俗工艺"};

for(int l=0;l<11;l++){
HashMap<String, Object> map4=new HashMap<String, Object>();
map4.put("imageView1", R.drawable.ic_launcher);
map4.put("textView1", info11[l]);
map4.put("textView2", info12[l]);
map4.put("textView3", info13[l]);
map4.put("textView4", info14[l]);

list4.add(map4);
}
sa4=new SimpleAdapter(this, list4, R.layout.item, s, i);

b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
lv.setAdapter(sa1);
}
});
b2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
lv.setAdapter(sa2);
}
});
b3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
lv.setAdapter(sa3);
}
});
b4.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
lv.setAdapter(sa4);
}
});

}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
//这里不知道怎么写了   主要是我是多个页面  然后我想可以区分  然后单击不同的行列可以跳转到别的Activity
}
}

5 个解决方案

#1


帮帮忙啊,本人新手,希望各位可以帮帮忙

#2


连问题都没有 直接贴一大堆代码也是够了 
listview的子项点击事件 
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub

}
});

#3


直接在item的点击事件里写你想干的事情就行了

#4


判断加载的是哪个数据源 然后 listView.getItemAtPosition(i) 得到强转类型就

#5


public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
//arg2为下标
            switch (arg2) {
case 0:
                        Intent intent=new Intent(this,页面一.class);
                                startActivity(intent);
break;
case 1:
                        Intent intent=new Intent(this,页面二.class);
                                startActivity(intent);
break;
case 2:
                        Intent intent=new Intent(this,页面三.class);
                                startActivity(intent);
break;
default:
break;
}
}


#1


帮帮忙啊,本人新手,希望各位可以帮帮忙

#2


连问题都没有 直接贴一大堆代码也是够了 
listview的子项点击事件 
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub

}
});

#3


直接在item的点击事件里写你想干的事情就行了

#4


判断加载的是哪个数据源 然后 listView.getItemAtPosition(i) 得到强转类型就

#5


public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
//arg2为下标
            switch (arg2) {
case 0:
                        Intent intent=new Intent(this,页面一.class);
                                startActivity(intent);
break;
case 1:
                        Intent intent=new Intent(this,页面二.class);
                                startActivity(intent);
break;
case 2:
                        Intent intent=new Intent(this,页面三.class);
                                startActivity(intent);
break;
default:
break;
}
}