ListView適配器BaseAdapter

activity_main.xml

<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.listview.MainActivity" >

	<ListView 
	    android:layout_height="match_parent"
	    android:layout_width="match_parent"
	    android:id="@+id/lv"></ListView>
</RelativeLayout>

listlayout.xml需要適配的layout

<?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:layout_margin="10dp"
        android:id="@+id/iv"
        android:src="@drawable/ic_launcher"
        android:layout_marginRight="30dp"
        android:layout_width="30dp"
        android:layout_height="40dp"/>
    <LinearLayout 
        android:layout_marginTop="15dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <TextView 
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <TextView 
            android:id="@+id/tv_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</LinearLayout>

News.java列表對象

package com.example.listview;

public class News {
	private int image;
	private String name;
	private String content;
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getImage() {
		return image;
	}
	public void setImage(int image) {
		this.image = image;
	}
	
}
MainActivity.java

package com.example.listview;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extends Activity {
	
	private MyAdapter myAdapter;
	private MyAdapter1 myAdapter1;
	private ListView lv;
	private List<Integer> list;
	private List<News> newlist;
	private String[] content = {};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lv = (ListView)findViewById(R.id.lv);
		newlist = new ArrayList<News>();
		News new1;
		for(int i = 0; i < 100; i ++){
			new1 = new News();
			new1.setContent("第"+i+"條數據");
			new1.setImage(R.drawable.ic_launcher);
			new1.setName(i+"");
			newlist.add(new1);
		}
		//myAdapter = new MyAdapter(this, list);
		myAdapter1 = new MyAdapter1(this,newlist);
		lv.setAdapter(myAdapter1);
	}
}
自定義MyAdapter1繼承於BaseAdapter
package com.example.listview;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter1 extends BaseAdapter{
	
	private Context context;
	private List<News> list;
	
	public MyAdapter1(Context context,List<News> list) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.list = list;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(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
               //使用靜態內部類進行優化
                ViewHolder viewHolder;
		if(convertView == null){
			convertView = View.inflate(context, R.layout.listlayout, null);
			viewHolder = new ViewHolder();
			viewHolder.textView_content = (TextView)convertView.findViewById(R.id.tv_content);
			viewHolder.textView_name = (TextView)convertView.findViewById(R.id.tv_name);
			viewHolder.imageView = (ImageView)convertView.findViewById(R.id.iv);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
		}
		viewHolder.imageView.setImageResource(list.get(position).getImage());
		viewHolder.textView_content.setText(list.get(position).getContent());
		viewHolder.textView_name.setText(list.get(position).getName());
		return convertView;
	}
        於listlayout.xml對應
       static class ViewHolder{
		TextView textView_name;
		ImageView imageView;
		TextView textView_content;
	}
}


發佈了29 篇原創文章 · 獲贊 9 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章