Android课程设计第四天ListView运用

满堂花醉三千客,一剑霜寒十四洲。这篇文章主要讲述Android课程设计第四天ListView运用相关的知识,希望能为你提供帮助。
注意:课程设计只为完成任务,不做细节描述~ 
【Android课程设计第四天ListView运用】效果图

Android课程设计第四天ListView运用

文章图片

1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7tools:context=".MainActivity"> 8< ListView 9android:id="@+id/listview" 10android:layout_width="match_parent" 11android:layout_height="match_parent" 12android:text="asad" 13> 14< /ListView> 15 16 < /RelativeLayout>

< ?xml version="1.0" encoding="utf-8"?> < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> < ImageView android:layout_width="80dp" android:layout_height="80dp" android:id="@+id/img_ico" android:src="https://www.songbingjia.com/android/@mipmap/x2017612"/> < TextView android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/text1" android:layout_toRightOf="@id/img_ico" android:text="miemie" android:textSize="16sp" android:textColor="#0ccfff"/> < TextViewandroid:layout_width="match_parent" android:layout_height="40dp" android:id="@+id/text2" android:layout_below="@id/text1" android:layout_alignLeft="@id/text1" android:text="xxxxx" android:textSize="16sp" android:textColor="#0ccfff" /> < /RelativeLayout>

1 package com.example.myapplication; 2 3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.BaseAdapter; 8 import android.widget.ImageView; 9 import android.widget.TextView; 10 11 /** 12* Created by 樱花落舞 on 2017/6/12. 13*/ 14 15 public class MyAdapter extends BaseAdapter { 16private String names[], contents[]; 17private int images[]; 18//上下文 19private Context context; 20 21public MyAdapter(String names[], String contents[], int images[],Context context) { 22this.names = names; 23this.contents = contents; 24this.images = images; 25this.context=context; 26} 27 28//获取列表总数 29@Override 30public int getCount() { 31return names.length; 32} 33 34//获取单个item 35@Override 36public Object getItem(int position) { 37return names[position]; 38} 39 40//获取itemid 41@Override 42public long getItemId(int position) { 43return position; 44} 45 46//得到item视图 47@Override 48public View getView(int position, View convertView, ViewGroup parent) { 49View view = LayoutInflater.from(context).inflate(R.layout.item,null); 50ImageView image = (ImageView) view.findViewById(R.id.img_ico); 51TextView name = (TextView) view.findViewById(R.id.text1); 52TextView content = (TextView) view.findViewById(R.id.text2); 53 54image.setImageResource(images[position]); 55name.setText(names[position]); 56content.setText(contents[position]); 57 58return view; 59} 60 }

1 package com.example.myapplication; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.widget.ArrayAdapter; 6 import android.widget.ListView; 7 /* 8listview的使用 9在布局中准备控件 10在java代码中声明实例化 11准备数据源 12创建适配器 13加载适配器 14 15*/ 16 public class MainActivity extends AppCompatActivity { 17//声明控件 18private ListView listView; 19 20@Override 21protected void onCreate(Bundle savedInstanceState) { 22super.onCreate(savedInstanceState); 23setContentView(R.layout.activity_main); 24listView= (ListView) findViewById(R.id.listview); 25 26String names[]={"xh","xhh","xhhh","xhhhh"}; 27String contents[]={"Max","max","min","Min"}; 28int images[]={R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d}; 29MyAdapter adapter =new MyAdapter(names,contents,images,MainActivity.this); 30listView.setAdapter(adapter); 31} 32 }

 

    推荐阅读