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

时间:2012-04-17 07:09:43

标签: android listview

这是我的代码,

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

我能够显示一个带有一堆textview的自定义列表视图作为它的项目,但是,我想在List视图中获取restauirants setOnItemClick的名称。

例如,每当我点击“Bar B Q”,“加尔各答美食广场”时,它只会向我显示“Bar B Q”,“加尔各答美食广场”并非其他信息。 thnx提前。如果你需要任何东西,请随意。

enter image description here“我的应用程序屏幕截图”

2 个答案:

答案 0 :(得分:1)

假设您的getFoodJointname()课程中有Restaurent方法,您可以在onItemClick()中写下以下内容:

String hotelname = restaurentData.get(position).getFoodJointname();
makeAToast(hotelname);

答案 1 :(得分:1)

这将有希望地发挥作用。

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