Android 从自定义列表视图获取数据

Android 从自定义列表视图获取数据,android,listview,Android,Listview,这是我的密码 public class SecondScreenActivity extends Activity { ListView foodJntListView; ArrayList<Restaurent> restaurentData; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreat

这是我的密码

public class SecondScreenActivity extends Activity {
ListView foodJntListView;
ArrayList<Restaurent> restaurentData;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.second_screen);

    restaurentData = getFoodJnt();

    foodJntListView=(ListView)findViewById(R.id.listView_foodjnt);
    foodJntListView.bringToFront();

    // setting the adapter to the list 
    foodJntListView.setAdapter(new RestaurantBaseAdapter(this,restaurentData));

    //setting the onclick listener,activity on clicking on an item of the 
    foodJntListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
            String hotelname=restaurentData.get(position).toString();
             //things to write
        }
    });
}

// get all the list of foodjoints

private ArrayList<Restaurent> getFoodJnt() {
    // TODO Auto-generated method stub
    ArrayList<Restaurent> results=new ArrayList<Restaurent>();

    Restaurent restrnt=new Restaurent();

    restrnt.setFoodJointname("Ashila");
    restrnt.setCuisine("Biriyani,Moughlai");
    restrnt.setAddress("Kolkata,E M Bypass");
    restrnt.setOpenhours("10:00am-10:00pm");
    results.add(restrnt);

    restrnt=new Restaurent();
    restrnt.setFoodJointname("Bhajohori Manna");
    restrnt.setCuisine("Bengali,Chinese");
    restrnt.setAddress("Kolkata,Esplanede");
    restrnt.setOpenhours("10:00am-10:00pm");
    results.add(restrnt);

    restrnt=new Restaurent();
    restrnt.setFoodJointname("Bar B Q");
    restrnt.setCuisine("Bengali,Chinese,Thai");
    restrnt.setAddress("Kolkata,Park Street");
    restrnt.setOpenhours("10:00am-10:00pm");
    results.add(restrnt);

    return results;
}


public void makeAToast(String str) {
    //yet to implement
    Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();
}
公共类SecondScreenActivity扩展活动{
ListView foodJntListView;
ArrayList restaurentData;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(右布局第二个屏幕);
restaurentData=getFoodJnt();
foodJntListView=(ListView)findViewById(R.id.ListView\u foodjnt);
foodJntListView.bringToFront();
//将适配器设置为列表
setAdapter(新的RestaurantBaseAdapter(这个,restaurantData));
//设置onclick侦听器,在单击
setOnItemClickListener(新的OnItemClickListener()){
@凌驾
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
//TODO自动生成的方法存根
字符串hotelname=restaurentData.get(position.toString();
//要写的东西
}
});
}
//获取所有食物点列表
私有数组列表getFoodJnt(){
//TODO自动生成的方法存根
ArrayList结果=新建ArrayList();
Restaurent restnt=新的Restaurent();
restnt.setFoodJointname(“Ashila”);
Restnt.setCuisine(“Biriyani,Moughlai”);
限制设置地址(“加尔各答,E-M旁路”);
限制设置开放时间(“上午10:00-晚上10:00”);
结果:添加(restnt);
restnt=新餐厅();
restrnt.setFoodJointname(“Bhajohori Manna”);
restrnt.Setcouring(“孟加拉语,中文”);
注册地址(“加尔各答,西班牙”);
限制设置开放时间(“上午10:00-晚上10:00”);
结果:添加(restnt);
restnt=新餐厅();
restrnt.setFoodJointname(“Bar B Q”);
restrnt.Setcouring(“孟加拉、中国、泰国”);
restrnt.setAddress(“加尔各答,公园街”);
限制设置开放时间(“上午10:00-晚上10:00”);
结果:添加(restnt);
返回结果;
}
公共void makeAToast(字符串str){
//尚未实施
Toast Toast=Toast.makeText(this,str,Toast.LENGTH\u SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
我可以显示一个定制的listview,上面有一堆textview作为它的项目,但是,我想在列表视图上获得restauirants setOnItemClick的名称

e、 每当我点击“Bar B Q”,“加尔各答美食广场”,它只会显示“Bar B Q”,“加尔各答美食广场”,而不会显示其他信息。 提前通知。如果你需要什么,请随时通知我


“我的应用程序屏幕截图”

假设您在
restarent
类中有类似
getFoodJointname()
的方法,您可以在
onItemClick()中编写以下内容:


这将有望奏效

Restaurent rest= (Restaurent) foodJntListView.getSelectedItem();

我认为你的代码是对的。你在“hotelname”中得到了什么??
Restaurent rest= (Restaurent) foodJntListView.getSelectedItem();