Custom entity class: phoneEntity.javaCopy the code
package com.example.entity; /** * Created by Administrator on 2017/2/16. */ public class PhoneEntity { private int img; private String name,price,countNum; public int getImg() { return img; } public String getName() { return name; } public String getPrice() { return price; } public String getCountNum() { return countNum; } public void setImg(int img) { this.img = img; } public void setName(String name) { this.name = name; } public void setPrice(String price) { this.price = price; } public void setCountNum(String countNum) { this.countNum = countNum; }}Copy the code
Custom Layout acvititY_phone_list.xmlCopy the code
<? The 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" android:layout_margin="5dp"> <ImageView android:src="@drawable/ihone7plus" android:id="@+id/img_phone" android:layout_width="150dp" android:layout_height="150dp" android:layout_margin="10dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/phone_name" Android :layout_width="match_parent" Android :layout_height="wrap_content" Android :text="ihone7Plus Rose" android:textSize="20dp" android:layout_marginTop="50sp"/> <TextView android:id="@+id/phone_price" Android :layout_width="match_parent" Android :layout_height="wrap_content" Android :text="¥7188" Android :textSize="20dp" android:textColor="#f45959" /> <TextView android:id="@+id/phone_counter" android:layout_width="match_parent" Android :layout_height="wrap_content" Android :text="50353 "Android :textSize="15dp" android:textColor="@android:color/darker_gray"/> </LinearLayout> </LinearLayout>Copy the code
Customize acvitity_phone. XML (define ListView)Copy the code
<? The XML version = "1.0" encoding = "utf-8"? > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_phone" 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="com.example.jump.PhoneActivity"> <ListView android:id="@+id/lv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView> </RelativeLayout>Copy the code
Custom PhoneAcvitityCopy the code
package com.example.jump; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import com.example.adapter.PhoneAdapter; import com.example.entity.PhoneEntity; import java.util.ArrayList; import java.util.List; public class PhoneActivity extends AppCompatActivity { private ListView lv; private int[] ids=new int[]{R.drawable.ihone7plus,R.drawable.mi5s,R.drawable.leshi2,R.drawable.rongyao8}; Private String[] phonenames=new String[]{" iphone7plus,128G Rose Gold "," iphone5s full netcom, 32G black Gold "," iphone2 Full Netcom 32GB memory "," iphone8 full netcom 4GB32G pearl white "}; Private String[] phonePrice =new String[]{"¥7188","¥1899","¥1099","¥2229"}; Private String[] phonecounter=new String[]{"50353 ","20353 ","34553 ","65053 "}; private String[] phonecounter=new String[]{"50353 ","20353 ","34553 ","65053 "}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_phone); lv= (ListView) findViewById(R.id.lv_phone); intiCustomPhoneAdapter(); } private void intiCustomPhoneAdapter(){ List<PhoneEntity> list =new ArrayList<PhoneEntity>(); for (int i = 0; i < phonenames.length; i++) { PhoneEntity phone=new PhoneEntity(); phone.setImg(ids[i]); phone.setName(phonenames[i]); phone.setPrice(phoneprice[i]); phone.setCountNum(phonecounter[i]); list.add(phone); } PhoneAdapter phoneada=new PhoneAdapter(list,this); lv.setAdapter(phoneada); }}Copy the code
Custom Adapter (PhoneAdapter) reuse convertView, reduce the number of items: save memory, run more smoothly using ViewHolder classes, reduce the number of findViewById, the benefits of running more smoothlyCopy the code
package com.example.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.entity.PhoneEntity; import com.example.jump.R; import java.util.List; /** * Created by Administrator on 2017/2/16. */ public class PhoneAdapter extends BaseAdapter { private List<PhoneEntity> list; // Data source private Context Context; // Context object private LayoutInflater inflater; public PhoneAdapter(List<PhoneEntity> list, Context context) { this.list = list; this.context = context; inflater=LayoutInflater.from(context); @override public int getCount() {return list.size();} @override public int getCount() {return list.size(); @override public Object getItem(int position) {return list.get(position); @override public long getItemId(int position) {return position; } /** * specifies methods to load the item layout and display data on the item layout. * @param position * @param convertView * @param parent * @return */ @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder=null; If (convertView==null){// The convertView class still contains tag holder=new ViewHolder(); convertView=inflater.inflate(R.layout.activity_phone_list,null); holder.iv= (ImageView) convertView.findViewById(R.id.img_phone); holder.tv_phonename= (TextView) convertView.findViewById(R.id.phone_name); holder.tv_phoneprice= (TextView) convertView.findViewById(R.id.phone_price); holder.tv_phonecounter= (TextView) convertView.findViewById(R.id.phone_counter); convertView.setTag(holder); }else {// convertView is not null; if convertView is null, convertView contains tag holder= (ViewHolder) convertView.getTag(); } // View view=inflater.inflate(R.layout.activity_phone_list,null); // ImageView iv= (ImageView) view.findViewById(R.i.img_phone); // TextView tv_phonename= (TextView) view.findViewById(R.id.phone_name); // TextView tv_phoneprice= (TextView) view.findViewById(R.id.phone_price); // TextView tv_phonecounter= (TextView) view.findViewById(R.id.phone_counter); PhoneEntity phonety= (PhoneEntity) getItem(position); PhoneEntity phonety= (PhoneEntity) getItem(position); holder.iv.setImageResource(phonety.getImg()); holder.tv_phonename.setText(phonety.getName()); holder.tv_phoneprice.setText(phonety.getPrice()); holder.tv_phonecounter.setText(phonety.getCountNum()); return convertView; } class ViewHolder{ public ImageView iv; public TextView tv_phonename; public TextView tv_phoneprice; public TextView tv_phonecounter; }}Copy the code
MainAcvitity
Copy the code
package com.example.jump;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.example.adapter.MyAdapter;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button textButton, tv_btn, btn_edit, btn_cb, btn_imgsca, work_activity, rg_btn, timedatepick_btn, arrayadapter, btn_myadapter, btn_phone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {

        tv_btn = (Button) findViewById(R.id.btn_test);
        tv_btn.setOnClickListener(this);
        btn_edit = (Button) findViewById(R.id.btn_edit);
        btn_edit.setOnClickListener(this);
        btn_cb = (Button) findViewById(R.id.btn_cb);
        btn_cb.setOnClickListener(this);
        btn_imgsca = (Button) findViewById(R.id.btn_imgsca);
        btn_imgsca.setOnClickListener(this);
        work_activity = (Button) findViewById(R.id.work_activity);
        work_activity.setOnClickListener(this);
        rg_btn = (Button) findViewById(R.id.rg_btn);
        rg_btn.setOnClickListener(this);
        timedatepick_btn = (Button) findViewById(R.id.datetimepick_btn);
        timedatepick_btn.setOnClickListener(this);
        arrayadapter = (Button) findViewById(R.id.lv_arrayadapter);
        arrayadapter.setOnClickListener(this);
        btn_myadapter = (Button) findViewById(R.id.btn_myadapter);
        btn_myadapter.setOnClickListener(this);
        btn_phone = (Button) findViewById(R.id.btn_phone);
        btn_phone.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_edit:
                startNewApplication(EditTestActivity.class);
                break;
            case R.id.btn_cb:
                startNewApplication(CheckBoxActivity.class);
            case R.id.btn_imgsca:
                startNewApplication(ImageScaleActivity.class);
            case R.id.work_activity:
                startNewApplication(WorkActivity.class);
                break;
            case R.id.rg_btn:
                startNewApplication(RadioGroupActivity.class);
                break;
            case R.id.datetimepick_btn:
                startNewApplication(TimeDatePickerActivity.class);
                break;
            case R.id.btn_myadapter:
                startNewApplication(ArrayAdapterActivity.class);
                break;
            case R.id.btn_phone:
                startNewApplication(PhoneActivity.class);
                break;
        }

    }

    private void startNewApplication(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }
}
Copy the code