Advertisement
Guest User

ex

a guest
Mar 14th, 2012
1,936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. //The class
  2. public class AnotherAdapter extends ListActivity {
  3.  
  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         ArrayList<Items> stats = new ArrayList<Items>();
  8.         for (int i = 0; i < 14; i++) {
  9.             stats.add(new Items("data1-" + i, "data2-" + i));
  10.         }
  11.         setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
  12.                 stats));
  13.  
  14.     }
  15.  
  16.     class MyAdapter extends ArrayAdapter<Items> {
  17.  
  18.         LayoutInflater inflat;
  19.         private ArrayList<Items> items;
  20.  
  21.         public MyAdapter(Context context, int textViewResourceId,
  22.                 ArrayList<Items> objects) {
  23.             super(context, textViewResourceId, objects);
  24.             inflat = LayoutInflater.from(context);
  25.             items = objects;
  26.         }
  27.  
  28.         @Override
  29.         public int getItemViewType(int position) {
  30.             return (position == this.getCount() - 1) ? 1 : 0;
  31.         }
  32.  
  33.         @Override
  34.         public int getViewTypeCount() {
  35.             return 2;
  36.         }
  37.  
  38.         @Override
  39.         public View getView(int position, View convertView, ViewGroup parent) {
  40.             ViewHolder holder = null;
  41.             int theType = getItemViewType(position);
  42.             if (convertView == null) {
  43.                 holder = new ViewHolder();
  44.                 if (theType == 0) {
  45.                     convertView = inflat.inflate(R.layout.row_type1, null);
  46.                     holder.textView1 = (TextView) convertView
  47.                             .findViewById(R.id.text_view1);
  48.                     holder.textView2 = (TextView) convertView
  49.                             .findViewById(R.id.text_view2);
  50.                 } else if (theType == 1) {
  51.                     convertView = inflat.inflate(R.layout.row_type2, null);
  52.                     holder.textView3 = (TextView) convertView
  53.                             .findViewById(R.id.text_view3);
  54.                     holder.button1 = (Button) convertView
  55.                             .findViewById(R.id.button1);
  56.                 }
  57.                 convertView.setTag(holder);
  58.             } else {
  59.                 holder = (ViewHolder) convertView.getTag();
  60.             }
  61.             Log.v("XXX", holder.toString());
  62.             Items it = items.get(position);
  63.             if (it != null) {
  64.                 if (theType == 0) {
  65.                     holder.textView1.setText(it.data1);
  66.                     holder.textView2.setText(it.data2);
  67.                 } else if (theType == 1) {
  68.                     holder.textView3.setText(it.data1);
  69.                     holder.button1.setText(it.data2);
  70.                 }
  71.             }
  72.             return convertView;
  73.         }
  74.  
  75.         @Override
  76.         public Items getItem(int position) {
  77.             return items.get(position);
  78.         }
  79.  
  80.         private class ViewHolder {
  81.             TextView textView1, textView2, textView3;
  82.             Button button1;
  83.  
  84.             public String toString() {
  85.                 return "-";
  86.             }
  87.         }
  88.     }
  89.  
  90. }
  91.  
  92. class Items {
  93.     String data1, data2;
  94.  
  95.     public Items(String data1, String data2) {
  96.         this.data1 = data1;
  97.         this.data2 = data2;
  98.     }
  99. }
  100.  
  101. //row_type1
  102. <?xml version="1.0" encoding="utf-8"?>
  103. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  104.     android:layout_width="match_parent"
  105.     android:layout_height="match_parent"
  106.     android:orientation="vertical" >
  107.  
  108.     <TextView
  109.         android:id="@+id/text_view1"
  110.         android:layout_width="wrap_content"
  111.         android:layout_height="wrap_content"
  112.         android:text="TextView" />
  113.  
  114.     <TextView
  115.         android:id="@+id/text_view2"
  116.         android:layout_width="wrap_content"
  117.         android:layout_height="wrap_content"
  118.         android:text="TextView" />
  119.  
  120. </LinearLayout>
  121.  
  122. //row_type2
  123. <?xml version="1.0" encoding="utf-8"?>
  124. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  125.     android:layout_width="match_parent"
  126.     android:layout_height="match_parent"
  127.     android:orientation="horizontal" >
  128.  
  129.     <Button
  130.         android:id="@+id/button1"
  131.         android:layout_width="wrap_content"
  132.         android:layout_height="wrap_content"
  133.         android:focusable="false"
  134.         android:text="Button" />
  135.  
  136.     <ToggleButton
  137.         android:id="@+id/toggleButton1"
  138.         android:layout_width="wrap_content"
  139.         android:layout_height="wrap_content"
  140.         android:focusable="false"
  141.         android:text="ToggleButton" />
  142.  
  143.  
  144.     <TextView
  145.         android:id="@+id/text_view3"
  146.         android:layout_width="wrap_content"
  147.         android:layout_height="wrap_content"
  148.         android:text="TextView" />
  149.  
  150. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement