Created
February 13, 2014 15:10
-
-
Save alterakey/8976735 to your computer and use it in GitHub Desktop.
ArrayAdapter Illustration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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="wrap_content"> | |
<TextView | |
android:id="@+id/counter" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:gravity="center" | |
android:layout_gravity="center_vertical" /> | |
<Button | |
android:id="@+id/down" | |
android:text="-" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_vertical" /> | |
<Button | |
android:id="@+id/up" | |
android:text="+" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_vertical" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<ListView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/list" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gmail.altakey.count; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
public class MainActivity extends Activity { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
final ListView lv = (ListView)findViewById(R.id.list); | |
lv.setAdapter(new AdapterBuilder(this).build()); | |
} | |
private static class AdapterBuilder { | |
private Context mContext; | |
public AdapterBuilder(final Context context) { | |
mContext = context; | |
} | |
public ArrayAdapter<Integer> build() { | |
final Integer[] data = new Integer[] { | |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 | |
}; | |
return new ArrayAdapter(mContext, R.layout.item, R.id.counter, data) { | |
@Override | |
public View getView(final int position, View convertView, ViewGroup parent) { | |
final View v = super.getView(position, convertView, parent); | |
final TextView counter = (TextView)v.findViewById(R.id.counter); | |
v.findViewById(R.id.up).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(final View v) { | |
counter.setText(String.valueOf(++data[position])); | |
} | |
}); | |
v.findViewById(R.id.down).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(final View v) { | |
counter.setText(String.valueOf(--data[position])); | |
} | |
}); | |
return v; | |
} | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment