Android基础TOP7_1:ListView制作列表

结构:

Activity:

activity_main:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.top7_1.MainActivity" >
10 
11 <ListView 
12     android:id="@+id/lv"
13     android:layout_width="fill_parent"
14     android:layout_height="wrap_content">
15     
16 </ListView>
17 
18 </RelativeLayout>

mylayout:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal"
 6         >
 7     <ImageView 
 8         android:id="@+id/img"
 9         android:layout_width="50dp"
10         android:layout_height="50dp"
11         android:src="@drawable/ic_launcher"
12         android:layout_margin="10dp"
13         />
14 <LinearLayout 
15     android:id="@+id/ll"
16     android:layout_height="wrap_content"
17     android:layout_width="wrap_content"
18     android:orientation="vertical"
19     >
20     <TextView 
21         android:id="@+id/bt"
22         android:layout_width="fill_parent"
23         android:layout_height="wrap_content"
24         android:text="标题"
25         android:textSize="24dp"
26         android:textColor="#7B68EE"
27         android:layout_marginLeft="10dp"
28         android:layout_marginTop="5dp"        />
29      <TextView 
30         android:id="@+id/nw"
31         android:layout_width="fill_parent"
32         android:layout_height="wrap_content"
33         android:text="内容"
34         android:textSize="16dp"
35         android:textColor="#DAA520"
36         android:layout_marginLeft="10dp"
37              />
38 </LinearLayout>
39 </LinearLayout>

JAVA:

 1 package com.example.top7_1;
 2 
 3 import android.app.Activity;
 4 import android.content.Context;
 5 import android.os.Bundle;
 6 import android.view.LayoutInflater;
 7 import android.view.Menu;
 8 import android.view.MenuItem;
 9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.BaseAdapter;
12 import android.widget.ImageView;
13 import android.widget.ListView;
14 import android.widget.TextView;
15 
16 
17 public class MainActivity extends Activity {
18 ListView lv;
19 int[] price=new int[]{100,300,400,500};
20 int[] resids=new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher
21         ,R.drawable.ic_launcher,R.drawable.ic_launcher};
22 String[] balls=new String []{"1","2","3","4"};
23 String[] engNames={"1","2","3","4"};
24 MyAdapter adapter=null;
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.activity_main);
29         lv=(ListView) findViewById(R.id.lv);
30     adapter=new MyAdapter(this);
31     lv.setAdapter(adapter);
32     }
33     public class MyAdapter extends BaseAdapter{
34         private LayoutInflater lif;
35 
36         public MyAdapter(Context c) {
37             // TODO Auto-generated constructor stub
38          lif=LayoutInflater.from(c);
39         }
40 
41         @Override
42         public int getCount() {
43             // TODO Auto-generated method stub
44             return balls.length;
45         }
46 
47         @Override
48         public Object getItem(int position) {
49             // TODO Auto-generated method stub
50             return balls[position];
51         }
52 
53         @Override
54         public long getItemId(int position) {
55             // TODO Auto-generated method stub
56             return position;
57         }
58 
59         @Override
60         public View getView(int position, View convertView, ViewGroup parent) {
61             // TODO Auto-generated method stub
62             convertView=lif.inflate(R.layout.mylayout,null);
63             ImageView ImgLogo=(ImageView) convertView.findViewById(R.id.img);
64             TextView textName=(TextView) convertView.findViewById(R.id.bt);
65             TextView textengName=(TextView) convertView.findViewById(R.id.nw);
66             ImgLogo.setImageResource(resids[position]);
67             textName.setText(balls[position]);
68             textengName.setText(engNames[position]);
69             return convertView;
70         }
71         
72         
73     }
74 }

 

posted @ 2017-04-13 14:51  AndrioidCSY  阅读(356)  评论(0编辑  收藏  举报