简单的ListView

直接上代码吧---
Fruitactity.java(MainActity)

package com.lhp.basic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.lhp.basic.adapter.FruitAdapter;
import com.lhp.basic.domain.Fruit;

import java.util.ArrayList;
import java.util.List;

public class FruitActity extends AppCompatActivity {
    List fruitList=new ArrayList();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fruit_actity);
        initFruits(); // 初始化水果数据
        FruitAdapter adapter = new FruitAdapter(FruitActity.this,
                R.layout.fruit_item, fruitList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);

    }
    public void initFruits(){
        for (int i = 0; i < 2; i++) {
            Fruit apple = new Fruit("Apple", R.drawable.chat);
            fruitList.add(apple);
            Fruit banana = new Fruit("Banana", R.drawable.heart);
            fruitList.add(banana);
            Fruit orange = new Fruit("Orange", R.drawable.like);
            fruitList.add(orange);
            Fruit watermelon = new Fruit("Watermelon", R.drawable.item);
            fruitList.add(watermelon);
            Fruit pear = new Fruit("Pear", R.drawable.like);
            fruitList.add(pear);
            Fruit grape = new Fruit("Grape", R.drawable.chat);
            fruitList.add(grape);
            Fruit pineapple = new Fruit("Pineapple", R.drawable.heart);
            fruitList.add(pineapple);
            Fruit strawberry = new Fruit("Strawberry", R.drawable.share);
            fruitList.add(strawberry);
            Fruit cherry = new Fruit("Cherry", R.drawable.location);
            fruitList.add(cherry);
            Fruit mango = new Fruit("Mango", R.drawable.plant);
            fruitList.add(mango);
        }
    }
}

FruitAdapter.java

package com.lhp.basic.adapter;

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;

import com.lhp.basic.R;
import com.lhp.basic.domain.Fruit;

import java.util.List;

public class FruitAdapter extends ArrayAdapter {

    private int resourceId;
    public FruitAdapter(Context context, int textViewResourceId,
                        List objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }


    @Override
    public View getView(int position,  View convertView,  ViewGroup parent) {
        Fruit fruit = getItem(position);//获取当前实例
        View view = LayoutInflater.from(getContext()).inflate(resourceId, parent,
                false);
        ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
        TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
        fruitImage.setImageResource(fruit.getImageId());
        fruitName.setText(fruit.getName());
        return view;
    }
}

fruit_actity.xml


    


fruit_item.xml


    
    


效果图


fruit.png

你可能感兴趣的:(简单的ListView)