• Android购物菜单


    package com.example.ddd;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.ListView;
    
    public class MainActivity extends Activity {
        private List<Fruit> fruitList = new ArrayList<Fruit>(); 
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            initFruits();
            aaa adapter = new aaa(MainActivity.this, R.layout.xxx, fruitList);  
            ListView listView = (ListView) findViewById(R.id.lv1);
            listView.setAdapter(adapter);  
        }
    
    
        private void initFruits(){
             Fruit apple = new Fruit("Apple", R.drawable.apple);
             fruitList.add(apple);
             Fruit banana = new Fruit("Banana",  R.drawable.banana);
             fruitList.add(banana);
             Fruit orange = new Fruit("Orange",R.drawable.orange);
             fruitList.add(orange);
             Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon);
             fruitList.add(watermelon);
             Fruit pear = new Fruit("Pear", R.drawable.pear);
             
             fruitList.add(pear);
        }
    }
    package com.example.ddd;
    
    import java.util.List;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class aaa extends ArrayAdapter {
         private final int resourceId;  
          
            public aaa(Context context, int textViewResourceId, List<Fruit> objects) {  
                super(context, textViewResourceId, objects);  
                resourceId = textViewResourceId;  
            }  
            @Override  
            public View getView(int position, View convertView, ViewGroup parent) {  
                Fruit fruit = (Fruit) getItem(position);
                View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
                ImageView fruitImage = (ImageView) view.findViewById(R.id.iv);
                TextView fruitName = (TextView) view.findViewById(R.id.tv1);
                fruitImage.setImageResource(fruit.getImageId());
                fruitName.setText(fruit.getName());
                return view;
            }  
        
    
    }
    package com.example.ddd;
    
    public class Fruit {
        private String name;  
        private int imageId;  
      
        public Fruit(String name, int imageId) {  
            this.name = name;  
            this.imageId = imageId;  
        }  
      
        public String getName() {  
            return name;  
        }  
      
        public int getImageId() {  
            return imageId;  
        }  
    
    }
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ListView
                android:id="@+id/lv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </ListView>
        </LinearLayout>
    </RelativeLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ListView
                android:id="@+id/lv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </ListView>
        </LinearLayout>
    </RelativeLayout>
  • 相关阅读:
    【HDU】4092 Nice boat(多校第四场1006) ——线段树 懒惰标记 Prime
    【POJ】2528 Mayor's posters ——离散化+线段树 Prime
    【HDU】1754 I hate it ——线段树 单点更新 区间最值 Prime
    C语言 linux环境基于socket的简易即时通信程序 Prime
    Java异常处理
    重载和重写的区别与联系
    SQL Server的优点与缺点
    Servlet基础
    C语言图形编程
    socket通讯,TCP,UDP,HTTP的区别
  • 原文地址:https://www.cnblogs.com/liu11xiansheng/p/14056004.html
Copyright © 2020-2023  润新知