zoukankan      html  css  js  c++  java
  • 第六周作业

    <?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"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/iv"
            android:layout_width="190dp"
            android:layout_height="170dp"
            >
    
        </ImageView>
    
    
        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="60dp"
            android:text="1123">
    
        </TextView>
    
    
    </LinearLayout>
    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;  
        }  
    
    }
    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 FruitAdapter extends ArrayAdapter {
         private final int resourceId;  
          
            public FruitAdapter(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;
            }  
        
    
    }
    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();
            FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);  
            ListView listView = (ListView) findViewById(R.id.lv1);
            listView.setAdapter(adapter);  
        }
    
    
        private void initFruits(){
             Fruit apple = new Fruit("Apple", R.drawable.tu);
             fruitList.add(apple);
             Fruit banana = new Fruit("Banana",  R.drawable.tu);
             fruitList.add(banana);
             Fruit orange = new Fruit("Orange",R.drawable.tu);
             fruitList.add(orange);
             Fruit watermelon = new Fruit("Watermelon", R.drawable.tu);
             fruitList.add(watermelon);
             Fruit pear = new Fruit("Pear", R.drawable.tu);
             fruitList.add(pear);
             Fruit grape = new Fruit("Grape", R.drawable.tu);
             fruitList.add(grape);
             Fruit pineapple = new Fruit("Pineapple", R.drawable.tu);
             fruitList.add(pineapple);
             Fruit strawberry = new Fruit("Strawberry", R.drawable.tu);
             fruitList.add(strawberry);
             Fruit cherry = new Fruit("Cherry", R.drawable.tu);
             fruitList.add(cherry);
             Fruit mango = new Fruit("Mango", R.drawable.tu);
             fruitList.add(mango);
    
        
        }
    
    
    
       
        
    }
  • 相关阅读:
    Java相对路径读取文件
    【转载】 OpenCV ——双线性插值(Bilinear interpolation)
    【转载】 从ACM会议看中国大陆计算机科学与国外的差距
    【转载】 一个老博士的经验顺口溜! 研究生生活的精华总结!
    【转载】 研究生生活总结(2):从技术到研究再到技术的过程
    【转载】 研究生生活总结(1):当助教的那些人和事
    【转载】 如何看待 2019 年 CS PhD 现扎堆申请且大部分为 AI 方向?未来几年 AI 泡沫会破裂吗?
    【转载】 深度强化学习处理cartpole为什么reward很难超过200?
    【转载】 强化学习中 采用 【首次访问的蒙特卡洛预测法】 的算法描述
    【转载】 混合智能
  • 原文地址:https://www.cnblogs.com/wudongyang521/p/14086648.html
Copyright © 2011-2022 走看看