/** * * * * @author fansxnet * * * @deprecated pojo * * */ public class Item { private int i; private Drawable imge; public int getI() { return i; } public void setI(int i) { this.i = i; } public Drawable getImge() { return imge; } public void setImge(Drawable imge) { this.imge = imge; } }
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class ListViewTestActivity extends Activity { private ListView listview; private List<Item> itemlist; private Context context; private ListViewAdapter listadapter; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); itemlist = new ArrayList<Item>(); this.context = this; button = (Button)findViewById(R.id.button); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { itemlist = getData(); listadapter.addItem(itemlist); // listadapter.notifyDataSetChanged(); } }); listview = (ListView) findViewById(R.id.listView); listview.setOnItemClickListener(new ListItemOnClickListener()); listadapter = new ListViewAdapter(context); listview.setAdapter(listadapter); } private List<Item> getData(){ List<Item> templist = new ArrayList<Item>(); for(int i = 0;i<10;i++){ Item item = new Item(); item.setI(i); item.setImge(getResources().getDrawable(R.drawable.ic_launcher)); templist.add(item); } return templist; } class ListViewAdapter extends BaseAdapter { private List<Item> itemlist; private Context mContext; public ListViewAdapter(Context context) { this.itemlist = new ArrayList<Item>(); this.mContext = context; } public void addItem(List<Item> addlist){ itemlist.addAll(addlist); notifyDataSetChanged(); } @Override public int getCount() { // TODO Auto-generated method stub return itemlist.size() + 1; } @Override public Object getItem(int position) { // TODO Auto-generated method stub if (position == this.getCount() - 1) { return null; } else { return itemlist.get(position); } } @Override public long getItemId(int position) { // TODO Auto-generated method stub if (position != this.getCount() - 1) { return position; } else { return -1; } } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Item item = new Item(); if(position != this.getCount()-1){ item = itemlist.get(position); } if(position == this.getCount()-1){ LayoutInflater flater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); convertView = flater.inflate(R.layout.morestatus, null); return convertView; } convertView = LayoutInflater.from(mContext).inflate(R.layout.listviewitem, null); ImageView imageview = (ImageView)convertView.findViewById(R.id.imageView); TextView textview = (TextView)convertView.findViewById(R.id.textView); imageview.setImageDrawable(item.getImge()); textview.setText(String.valueOf(item.getI())); return convertView; } } class ListItemOnClickListener implements OnItemClickListener{ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { long id = arg0.getItemIdAtPosition(arg2); if(id == -1){ itemlist = getData(); listadapter.addItem(itemlist); listadapter.notifyDataSetChanged(); }else{ Toast.makeText(context, String.valueOf(id), 5000).show(); } } } }