使用数组填充视图 [英] Populate views with array

查看:94
本文介绍了使用数组填充视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android新手。我在我的书中找到了这个代码,我正在学习。它没有正确解释我创建了一个Drink java类,然后我在我的活动中使用了drink []。单击它会将单击的数组的Id传递给另一个使用intent的活动。现在在其他活动中,我想显示饮料的名称,图像和我们在数组中存储的描述,我知道我们已经将数组ID存储在Drinks类对象中,但为什么我们使用getter方法来获取名称和描述以及图像。它们与阵列没有联系?他们里面没有代码?然后实际发生了什么



有人可以解释一下吗?





< br $>




Drinks.java类代码



I am new to Android. I found this code in my book that i am learning from. Its not explained properly I created a Drink java class and then i used drinks[]in my activity. On click it passes the Id of the clicked Array to another activity using intent. Now in that other activity i want to display the name of the drink the image and the description that we stored in the array I understood that we have stored the array Id in the Drinks class object but why did we use the getter methods to get the name and description and the image. They are not linked with the array? They have no code inside them? Then what is actually happening

can someone explain?





Drinks.java class code

Class Drinks
{
 private String name ;
    private  String  description ;
    private  int rid ;

    public static final Drinks[] drinks = {
            new Drinks("Latte", "A couple of espresso shots with steamed milk",
                    R.drawable.latte),
            new Drinks("Cappuccino", "Espresso, hot milk, and a steamed milk foam",
                    R.drawable.cappuccino),
            new Drinks("Filter", "Highest quality beans roasted and brewed fresh",
                    R.drawable.filter)
    };


          public Drinks(String name, String description, int rid) {
                this.name = name;
                this.description = description;
                this.rid = rid;
            }
            public String getName() {
                return name;
            }

            public String getDescription() {
                return description;
            }

            public int getRid() {
                return rid;
            }
        }











我正在使用drink []并传递其ID的活动代码






The activity code where i am using the drinks[] and passing its Id

listDrinks = findViewById(R.id.listDrinks);



ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1 ,Drinks.drinks);
listDrinks.setAdapter(adapter);
listDrinks.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(Main2Activity.this , Main3Activity.class);
        intent.putExtra("id",id);

        startActivity(intent);

    }
});



------------------------------- ------------------------



我传递数组ID的其他活动并使用getter方法设置名称描述和图像以显示


-------------------------------------------------------

The other activity where i am passing the array id and using getter methods to set name description and image to display

  textView = findViewById(R.id.textname);
    text_description = findViewById(R.id.text_description);
    imageView = findViewById(R.id.photo);


    Intent intent= getIntent();
 int drinkId = Integer.parseInt(intent.getStringExtra("id"));
    Drinks d = Drinks.drinks[drinkId];


    textView.setText(d.getName());

    text_description.setText(d.getDescription());


    imageView.setImageResource(d.getRid())

;





我尝试过:



i无法理解这个概念,因为构造函数设置了完全不同的值。



What I have tried:

i cant understand the concept as the constructor is setting entirely different values.

推荐答案

您正在尝试通过Drinks对象的id值来处理数组。但是数组是通过0到N范围内的简单数值来寻址的。您需要索引数组中的每个项目,并将其id与传入的值进行比较。
You are trying to address the array by the id value of the Drinks object. But arrays are addressed by a simple numeric value in the range 0 to N. You need to index each item in the array and compare its id with the passed in value.


这篇关于使用数组填充视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆