Android Custom listview事件监听器

时间:2017-06-04 15:58:31

标签: android

我需要一些帮助。我想从我的自定义ListView中提取数据,但不知道如何。我想获取另一个活动中所选项目的数据。我必须显示用户选择的菜单。

这是我的代码:

package com.example.mouhamedalysidibe.restaurant;

public class Menus extends AppCompatActivity {
    public TabHost host ;

int[] Imagesfood = {R.drawable.pizza,R.drawable.baguette, R.drawable.pastas , R.drawable.donuts , R.drawable.snacks };
    String [] nomsfood = {"Pizza" , "Baguettes" ,"Pastas" , "Donuts" , "Snacks"};
    String [] descriptionfood = {"Les meilleurs pizzas de Dakar" , "Explosion de saveurs a prix mini" , "Laissez vous tenter" , "Al dente et tellement genereuses ", "Un delice a chaque bouchée"};

    int[] Imagesdrink = {R.drawable.gazeuse,R.drawable.alcool, R.drawable.cocktail , R.drawable.chaude};
    String [] nomsdrink = {"Boissons gazeuses" , "Alccol " ,"Cocktails" , "Boissons chaudes"};
    String [] descriptiondrink = {"Pensez a vous desalterer" , "De la bonne qualite" , "Decouvrez nos coktails" , "Un bon cafe ne fait jamais de mal"};

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menus);

        //tabhost
        host = (TabHost)findViewById(R.id.tabHost);
        host.setup();

        //Layouts
        TabHost.TabSpec spec = host.newTabSpec("EAT");
        spec.setContent(R.id.Eat);
        spec.setIndicator("FOOD");
        host.addTab(spec);

        //Widgets
        spec = host.newTabSpec("DRINKS");
        spec.setContent(R.id.Drink);
        spec.setIndicator("DRINK");
        host.addTab(spec);


        TextView tablefood = (TextView)findViewById(R.id.table);
        TextView tableDrink =(TextView)findViewById(R.id.tableD);
        Intent intent = getIntent();


        String tabletextfood = intent.getStringExtra("table");
        tablefood.setText(""+tabletextfood);
        String tabletextdrink = intent.getStringExtra("table");
        tableDrink.setText(""+tabletextdrink);

        //Listview Food

        final ListView listmenufood = (ListView)findViewById(R.id.listView_food);
        CustomAdapterFood customAdapterFood = new CustomAdapterFood();
        listmenufood.setAdapter(customAdapterFood);
        listmenufood.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                view =(View) adapterView.getItemAtPosition(i);
                Intent intent = new Intent(Menus.this,Plats.class);

                startActivity(intent);


            }
        });

        //Listview drink

        ListView listmenudrink = (ListView)findViewById(R.id.listView_drink);
        CustomAdapterDrink customAdapterDrink = new CustomAdapterDrink();

        listmenudrink.setAdapter(customAdapterDrink);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==R.id.appel){
            final AlertDialog retry = new AlertDialog.Builder( this ).create();
            retry.setTitle(R.string.app_name);
            retry.setMessage("MERCI DE PATIENTER QUELQUES INSTANTS , NOUS VENONS BIENTOT PRENDRE VOTRE COMMANDE");
            retry.setButton(AlertDialog.BUTTON_NEUTRAL,"OK",new  DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    retry.cancel();
                }
            });



            retry.show();


        }
        return super.onOptionsItemSelected(item);
    }


    // Foods

    public class CustomAdapterFood extends BaseAdapter{


        @Override
        public int getCount() {
            return Imagesfood.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = getLayoutInflater().inflate(R.layout.customlayout ,null);
            ImageView imageView = (ImageView)view.findViewById(R.id.img);
            TextView nom = (TextView)view.findViewById(R.id.TextView_nom);
            TextView descrip =(TextView)view.findViewById(R.id.TextView_description);
            imageView.setImageResource(Imagesfood[i]);
            nom.setText(nomsfood[i]);
            descrip.setText(descriptionfood[i]);

            return view;
        }
    }



    //Drinks

    public class CustomAdapterDrink extends BaseAdapter{


        @Override
        public int getCount() {
            return Imagesdrink.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = getLayoutInflater().inflate(R.layout.customdrinklayout,null);
            ImageView imageView = (ImageView)view.findViewById(R.id.img);
            TextView nom = (TextView)view.findViewById(R.id.TextView_nom);
            TextView descrip =(TextView)view.findViewById(R.id.TextView_description);
            imageView.setImageResource(Imagesdrink[i]);
            nom.setText(nomsdrink[i]);
            descrip.setText(descriptiondrink[i]);
            return view;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您想要返回图片,请输入姓名&amp;描述你需要为你的食物和饮料创建课程

//food
public class food {
int image;
String name,description;

public food(int image, String name, String description) {

    this.image = image;
    this.name = name;
    this.description = description;
}
public String getDescription() {
    return description;
}

public String getName() {
    return name;
}

public int getImage() {
    return image;
}

}

同样适用于饮料

添加食物和饮料对象列表

 foods.add(new food(R.drawable.pizza,"Pizza","description"));
    foods.add(new food(R.drawable.pasta,"Pasta","description"));
    //add the rest

        //similarly for drink

    drinks.add(new drink(R.drawable.somedrink,"name","description"));
    drinks.add(new drink(R.drawable.somedrink,"name","description"));

    //add the rest

将自定义适配器更改为

        public class CustomAdapterFood extends BaseAdapter {

ArrayList foodlist;

    public CustomAdapterFood(ArrayList<food> foodlist) {
        this.foodlist = foodlist;
    }

    @Override
    public int getCount() {
        return foodlist.size();
    }

    @Override
    public Object getItem(int i) {
        return foodlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = getLayoutInflater().inflate(R.layout.customlayout ,null);
        ImageView imageView = (ImageView)view.findViewById(R.id.img);
        TextView nom = (TextView)view.findViewById(R.id.TextView_nom);
        TextView descrip =(TextView)view.findViewById(R.id.TextView_description);
        imageView.setImageResource(foodlist.get(i).getImage());
        nom.setText(foodlist.get(i).getName());
        descrip.setText(foodlist.get(i).getDescription());

        return view;
    }
}

    public class CustomAdapterDrink extends BaseAdapter{
    ArrayList<drink> drinklist;

    public CustomAdapterDrink(ArrayList<drink> drinklist) {
        this.drinklist = drinklist;
    }

    @Override
    public int getCount() {
        return drinklist.size();
    }

    @Override
    public Object getItem(int i) {
        return drinklist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = getLayoutInflater().inflate(R.layout.customdrinklayout,null);
        ImageView imageView = (ImageView)view.findViewById(R.id.img);
        TextView nom = (TextView)view.findViewById(R.id.TextView_nom);
        TextView descrip =(TextView)view.findViewById(R.id.TextView_description);
        imageView.setImageResource(drinklist.get(i).getImage());
        nom.setText(drinklist.get(i).getName());
        descrip.setText(drinklist.get(i).getDescription());
        return view;
    }
}

创建适配器时将列表传递给它们 像这样:

        CustomAdapterDrink customAdapterDrink = new CustomAdapterDrink(drinks);

        CustomAdapterFood customAdapterFood = new CustomAdapterFood(foods);