ListView+BaseAdapter实现电话簿

ListView+BaseAdapter实现电话簿_第1张图片

package xdglyt.software.jhwz.myapplicationlistview;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private String[] contact_n = {
            "李思思" ,
            "张略速" ,
            "王毅" ,
            "蒙氏将" ,
            "赵迪霞" ,
            "赵霞" ,
            "赵一飞" ,
            "沈梦辰" ,
            "杨洋" ,
            "沙本妮" ,
            "卢姥爷"
    };

    private String[] contact_p = {
            "19336992909" ,
            "12336992456" ,
            "15236992912" ,
            "17836993332" ,
            "15433567882" ,
            "15336992923" ,
            "19036992966" ,
            "17899992956" ,
            "10255392989" ,
            "15436992920" ,
            "15136992942"
    };
    private int[] contact_g = {
            R.drawable.touxiang_girl,
            R.drawable.touxiang_boy,
            R.drawable.touxiang_boy,
            R.drawable.touxiang_girl,
            R.drawable.touxiang_girl,
            R.drawable.touxiang_boy,
            R.drawable.touxiang_boy,
            R.drawable.touxiang_girl,
            R.drawable.touxiang_girl,
            R.drawable.touxiang_boy,
            R.drawable.touxiang_boy
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv =(ListView) findViewById(R.id.lv);
        MyContact contact = new MyContact();
        lv.setAdapter(contact);
    }

    class MyContact extends BaseAdapter {

        @Override
        public int getCount() {
            return contact_n.length;
        }

        @Override
        public Object getItem(int position) {
            return contact_n[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(MainActivity.this, R.layout.list_item, null);
            TextView name = (TextView) view.findViewById(R.id.contact_name);
            name.setText(contact_n[position]);
            TextView phone = (TextView) view.findViewById(R.id.contact_phone);
            phone.setText(contact_p[position]);
            ImageView iv = (ImageView) view.findViewById(R.id.graph);
            iv.setBackgroundResource(contact_g[position]);
            return view;


        }
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/graph"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/touxiang_boy"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/contact_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:text="张三"
                android:textSize="30sp"
                android:textColor="#000"/>
            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="10dp"
                android:background="@drawable/collection"/>
        LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="联系电话:"
                android:textSize="22sp"/>
            <TextView
                android:id="@+id/contact_phone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="15536992230"
                android:textSize="22sp"/>
        LinearLayout>

    LinearLayout>
LinearLayout>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#B0E0E6"
        android:dividerHeight="1dp"/>
LinearLayout>

你可能感兴趣的:(android,studio)