listview使用BaseAdapter显示图片和文字

package com.example.listview_7_1;

import android.app.Activity;
import android.os.Bundle;
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 {

	private ListView lv1;

	private int[] imagesId={R.drawable.cat,R.drawable.monkey,R.drawable.rabbit,R.drawable.rat};
	private	String[] names={"短毛猫","猴子","兔子","老鼠"};
	private  String[] contents={"可爱","顽皮","温顺","伶俐"};

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lv1 = (ListView) findViewById(R.id.listView1);
		
		BaseAdapter adapter = new BaseAdapter() {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO 自动生成的方法存根
				View layout=View.inflate(MainActivity.this, R.layout.custom_list, null);
				ImageView face = (ImageView)layout.findViewById(R.id.face);
				TextView name =(TextView)layout.findViewById(R.id.name);
				TextView mark = (TextView)layout.findViewById(R.id.mark);
				
				face.setImageResource(imagesId[position]);
				name.setText(names[position]);
				mark.setText(contents[position]);
				
				return layout;
			}
			
			@Override
			public long getItemId(int position) {
				// TODO 自动生成的方法存根
				return position;
			}
			
			@Override
			public Object getItem(int position) {
				// TODO 自动生成的方法存根
				return names[position];
			}
			
			@Override
			public int getCount() {
				// TODO 自动生成的方法存根
				return names.length;
			}
		};///new BaseAdapter()
		
		lv1.setAdapter(adapter);

	}
}



    

    

    



    
    




listview使用BaseAdapter显示图片和文字_第1张图片listview使用BaseAdapter显示图片和文字_第2张图片

你可能感兴趣的:(Android)