代码如下:

  1.TestAdapter

package com.TestAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class TestAdapter extends Activity {
/** Called when the activity is first created. */
private int [] images ;
private ListView listview;
private MyAdapter adapter;
List
< Map < String , Integer >> al;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
images
= new int []{android.R.drawable.ic_btn_speak_now,
android.R.drawable.alert_light_frame,
android.R.drawable.arrow_down_float,
android.R.drawable.arrow_up_float,
android.R.drawable.btn_star_big_off,
android.R.drawable.btn_star_big_on,
android.R.drawable.button_onoff_indicator_off,
android.R.drawable.button_onoff_indicator_on,
android.R.drawable.checkbox_off_background,
android.R.drawable.checkbox_on_background,
android.R.drawable.ic_btn_speak_now,
android.R.drawable.ic_delete};
listview
= (ListView)findViewById(R.id.listview);
al
= new ArrayList < Map < String , Integer >> ();
for ( int i = 0 ; i < 12 ; i ++ ){
HashMap
< String , Integer > map = new HashMap < String , Integer > ();
map.put(
"" + i, images[i]);
al.add(map);
}

adapter
= new MyAdapter(this, al, R.layout.list_item, new String []{ " imageview " , " tv " },
new int []{R.id.imageview, R.id.tv});
listview.setAdapter(adapter);
listview.setOnItemClickListener(
new OnItemClickListener() {

public void onItemClick(AdapterView < ? > arg0, View arg1, int count,
long arg3) {
if (adapter.getCount() == count + 1 ){
HashMap
< String , Integer > map = new HashMap < String , Integer > ();
map.put(
"" + (adapter.mItemList.size()), android.R.drawable.ic_dialog_email);
al.add(map);
adapter.mItemList
= al;
adapter.notifyDataSetChanged();
Toast.makeText(TestAdapter.this,
" 正在刷新 " , Toast.LENGTH_SHORT).show();
}
}
});

}


private class MyAdapter extends SimpleAdapter{
int count = 0 ;
private List < Map < String , Integer >> mItemList;
public MyAdapter(Context context, List < ? extends Map < String , Integer >> data,
int resource, String [] from, int [] to ) {
super(context, data, resource, from,
to );
mItemList
= (List < Map < String , Integer >> ) data;
if (data == null ){
count
= 0 ;
}
else {
count
= data.size();
}
}
public int getCount() {
return mItemList.size();
}

public Object getItem( int pos) {
return pos;
}

public long getItemId( int pos) {
return pos;
}

@Override
public View getView( int position, View convertView, ViewGroup parent) {
Map
< String , Integer > map = mItemList.get(position);
int image = map.get( "" + position);
View view
= super.getView(position, convertView, parent);
ImageView imageview
= (ImageView)view.findViewById(R.id.imageview);
TextView tv
= (TextView)view.findViewById(R.id.tv);
imageview.setBackgroundResource(image);
tv.setText(
"" + position);
return view;
}
}
}

  2.main.xml

< ?xml version = " 1.0 " encoding = " utf-8 " ? >
< LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
android:orientation
= " vertical "
android:layout_width
= " fill_parent "
android:layout_height
= " fill_parent "
>
< ListView android:id = " @+id/listview "
android:layout_width
= " fill_parent "
android:layout_height
= " wrap_content "
></ ListView >

</ LinearLayout >

  3.list_item.xml

< ?xml version = " 1.0 " encoding = " utf-8 " ? >
< LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
android:orientation
= " vertical "
android:layout_width
= " fill_parent "
android:layout_height
= " fill_parent "
>
< ImageView
android:id
= " @+id/imageview "
android:layout_width
= " wrap_content "
android:layout_height
= " wrap_content " >
</ ImageView >
< TextView
android:id
= " @+id/tv " android:layout_width = " fill_parent "
android:layout_height
= " wrap_content "
></ TextView >
</ LinearLayout >

更多相关文章

  1. Android网格布局实现--recyclerview
  2. Android: ViewPager and Fragments
  3. Android(安卓)代码混淆异常 transformClassesAndResourcesWithPr
  4. Android(安卓)AsyncTask 异步任务取消
  5. Android代码混淆
  6. BaseAdapter
  7. Android(安卓)Spinner控件的简单应用
  8. 调用android系统自带功能
  9. 关于android的des算法代码

随机推荐

  1. Android(安卓)响应式编程框架 - RxJava2(
  2. Android(安卓)OpenGLES 3.0 开发极简教程
  3. inbox
  4. android onTrimMemory()和onLowMemory()
  5. Error:Could not find com.android.tools
  6. Android(安卓)DEV : Custom new state fo
  7. Activity使用Dialog样式导致点击空白处自
  8. Android中socket的实例分析(二)
  9. android4启程
  10. java Android(安卓)OKHttp HTTPS 请求证