结构:

Android基础TOP7_1:ListView制作列表-LMLPHP

Activity:

activity_main:

 <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="com.example.top7_1.MainActivity" > <ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"> </ListView> </RelativeLayout>

mylayout:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher"
android:layout_margin="10dp"
/>
<LinearLayout
android:id="@+id/ll"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/bt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="标题"
android:textSize="24dp"
android:textColor="#7B68EE"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/nw"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="内容"
android:textSize="16dp"
android:textColor="#DAA520"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</LinearLayout>

JAVA:

 package com.example.top7_1;

 import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity {
ListView lv;
int[] price=new int[]{,,,};
int[] resids=new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher
,R.drawable.ic_launcher,R.drawable.ic_launcher};
String[] balls=new String []{"","","",""};
String[] engNames={"","","",""};
MyAdapter adapter=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.lv);
adapter=new MyAdapter(this);
lv.setAdapter(adapter);
}
public class MyAdapter extends BaseAdapter{
private LayoutInflater lif; public MyAdapter(Context c) {
// TODO Auto-generated constructor stub
lif=LayoutInflater.from(c);
} @Override
public int getCount() {
// TODO Auto-generated method stub
return balls.length;
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return balls[position];
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView=lif.inflate(R.layout.mylayout,null);
ImageView ImgLogo=(ImageView) convertView.findViewById(R.id.img);
TextView textName=(TextView) convertView.findViewById(R.id.bt);
TextView textengName=(TextView) convertView.findViewById(R.id.nw);
ImgLogo.setImageResource(resids[position]);
textName.setText(balls[position]);
textengName.setText(engNames[position]);
return convertView;
} }
}
05-19 20:54