Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //The class
- public class AnotherAdapter extends ListActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ArrayList<Items> stats = new ArrayList<Items>();
- for (int i = 0; i < 14; i++) {
- stats.add(new Items("data1-" + i, "data2-" + i));
- }
- setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
- stats));
- }
- class MyAdapter extends ArrayAdapter<Items> {
- LayoutInflater inflat;
- private ArrayList<Items> items;
- public MyAdapter(Context context, int textViewResourceId,
- ArrayList<Items> objects) {
- super(context, textViewResourceId, objects);
- inflat = LayoutInflater.from(context);
- items = objects;
- }
- @Override
- public int getItemViewType(int position) {
- return (position == this.getCount() - 1) ? 1 : 0;
- }
- @Override
- public int getViewTypeCount() {
- return 2;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder = null;
- int theType = getItemViewType(position);
- if (convertView == null) {
- holder = new ViewHolder();
- if (theType == 0) {
- convertView = inflat.inflate(R.layout.row_type1, null);
- holder.textView1 = (TextView) convertView
- .findViewById(R.id.text_view1);
- holder.textView2 = (TextView) convertView
- .findViewById(R.id.text_view2);
- } else if (theType == 1) {
- convertView = inflat.inflate(R.layout.row_type2, null);
- holder.textView3 = (TextView) convertView
- .findViewById(R.id.text_view3);
- holder.button1 = (Button) convertView
- .findViewById(R.id.button1);
- }
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- Log.v("XXX", holder.toString());
- Items it = items.get(position);
- if (it != null) {
- if (theType == 0) {
- holder.textView1.setText(it.data1);
- holder.textView2.setText(it.data2);
- } else if (theType == 1) {
- holder.textView3.setText(it.data1);
- holder.button1.setText(it.data2);
- }
- }
- return convertView;
- }
- @Override
- public Items getItem(int position) {
- return items.get(position);
- }
- private class ViewHolder {
- TextView textView1, textView2, textView3;
- Button button1;
- public String toString() {
- return "-";
- }
- }
- }
- }
- class Items {
- String data1, data2;
- public Items(String data1, String data2) {
- this.data1 = data1;
- this.data2 = data2;
- }
- }
- //row_type1
- <?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="vertical" >
- <TextView
- android:id="@+id/text_view1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
- <TextView
- android:id="@+id/text_view2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
- </LinearLayout>
- //row_type2
- <?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" >
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:focusable="false"
- android:text="Button" />
- <ToggleButton
- android:id="@+id/toggleButton1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:focusable="false"
- android:text="ToggleButton" />
- <TextView
- android:id="@+id/text_view3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement