【第22期】观点:IT 行业加班,到底有没有价值?

ListView自定义适配器和适配类型

原创 2015年11月18日 11:09:04
package com.example.testlistview2;

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;
    }

}
package com.example.testlistview2;

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

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
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 fa = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitlist);
        ListView lv = (ListView)findViewById(R.id.list_view); 
        lv.setAdapter(fa);
        //点击事件
        lv.setOnItemClickListener(new OnItemClickListener() {

            /*@Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub

            }*/
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fruit fruit = fruitlist.get(position);
                Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();

            }


        });

    }

    private void initFruits(){
        Fruit a = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(a);
        Fruit b = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(b);
        Fruit c = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(c);
        Fruit d = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(d);
        Fruit e = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(e);
        Fruit f = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(f);
        Fruit h = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(h);
        Fruit i = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(i);
        ////////
        Fruit aa = new Fruit("苹果1",R.drawable.apple);
        fruitlist.add(aa);

        Fruit ba = new Fruit("Android1",R.drawable.ic_launcher);
        fruitlist.add(ba);


        Fruit ca = new Fruit("苹果2",R.drawable.apple);
        fruitlist.add(ca);

        Fruit da = new Fruit("Android2",R.drawable.ic_launcher);
        fruitlist.add(da);
        Fruit ea = new Fruit("苹果3",R.drawable.apple);
        fruitlist.add(ea);

        Fruit fa = new Fruit("Android3",R.drawable.ic_launcher);
        fruitlist.add(fa);
        Fruit ha = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(ha);

        Fruit ia = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(ia);
        Fruit ab = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(ab);

        Fruit bb = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(bb);


        Fruit cb = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(cb);

        Fruit db = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(db);
        Fruit eb = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(eb);

        Fruit fb = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(fb);
        Fruit hb = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(hb);

        Fruit ib = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(ib);

        Fruit a1 = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(a1);

        Fruit b1 = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(b1);


        Fruit c1 = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(c1);

        Fruit d1 = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(d1);
        Fruit e1 = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(e1);

        Fruit f1 = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(f1);
        Fruit h1 = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(h1);

        Fruit i1 = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(i1);

        Fruit aa1 = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(aa1);

        Fruit ba1 = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(ba1);


        Fruit ca1 = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(ca1);

        Fruit da1 = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(da1);
        Fruit ea1 = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(ea1);

        Fruit fa1 = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(fa1);
        Fruit ha1 = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(ha1);

        Fruit ia1 = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(ia1);
        Fruit ab1 = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(ab1);

        Fruit bb1 = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(bb1);


        Fruit cb1 = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(cb1);

        Fruit db1 = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(db1);
        Fruit eb1 = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(eb1);

        Fruit fb1 = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(fb1);
        Fruit hb1 = new Fruit("苹果",R.drawable.apple);
        fruitlist.add(hb1);

        Fruit ib1 = new Fruit("Android",R.drawable.ic_launcher);
        fruitlist.add(ib1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
package com.example.testlistview2;

import java.util.List;

import android.content.Context;
import android.util.Log;
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<Fruit> {

    private static final String TAG = "FruitAdapter";
    private 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 f = getItem(position);//getItem返回的是泛型
        View view = LayoutInflater.from(getContext()).inflate(resourceId, null);//加载布局文件,类似findviewById,只是它加载控件。
        ImageView fi = (ImageView)view.findViewById(R.id.fruit_image);
        TextView fn = (TextView)view.findViewById(R.id.fruit_name);

        fi.setImageResource(f.getImageId());
        fn.setText(f.getName());

        return view;

    }*/
    //优化方法
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Fruit f = getItem(position);//getItem返回的是泛型
        View view;
        ViewHolder viewhodler;
        //convertView主要是将一开始的旧View加载好的布局进行缓存,以便之后可以进行重用。
        //所以在将加载的第一页面内convertView=null,向下滑动时候Log显示convertView !=null.
        if(convertView == null){
            Log.i(TAG,"convertView == null");
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);//加载布局文件,类似findviewById,只是它加载控件。
            viewhodler = new ViewHolder();
            viewhodler.fruitImage = (ImageView)view.findViewById(R.id.fruit_image);
            viewhodler.fruitName = (TextView)view.findViewById(R.id.fruit_name);
            view.setTag(viewhodler);//将viewholder存储在view中

        }else{
            Log.i(TAG,"convertView != null");
            view = convertView;
            viewhodler = (ViewHolder) view.getTag();//取出缓存的ViewHolder
        }
        viewhodler.fruitImage.setImageResource(f.getImageId());
        viewhodler.fruitName.setText(f.getName());

        return view;
    }
    //新增创建内部类
    class ViewHolder{

        ImageView fruitImage;
        TextView fruitName;
    }
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   <!--  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" /> -->
     <ListView 
        android:id = "@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</RelativeLayout>
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

[startrelatedarticles]

{relatedtitle}

{relateddes}
[endrelatedarticles] [startrelatedarticlesad1]

{relatedtitle}

{relateddes}
[endrelatedarticlesad1] [startrelatedarticlesad2]
{relateddes}
[endrelatedarticlesad2]
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)