Created
June 20, 2013 07:53
-
-
Save Phonbopit/5820966 to your computer and use it in GitHub Desktop.
ListView + ImageView
@see http://devsharing.com/2013/android/android-listview-example/
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
public class CustomAdapter extends ArrayAdapter<String> { | |
private final Context context; | |
private final String[] values; | |
public CustomAdapter(Context context, String[] values) { | |
super(context, R.layout.team_list, values); | |
this.context = context; | |
this.values = values; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
LayoutInflater inflater = (LayoutInflater) context | |
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
View view = inflater.inflate(R.layout.team_list, parent, false); | |
TextView teamName = (TextView) view.findViewById(R.id.team_name); | |
ImageView nation = (ImageView) view.findViewById(R.id.nation); | |
String name = values[position]; | |
teamName.setText(name); | |
if (position < 5) { | |
nation.setImageResource(R.drawable.thailand); | |
} else if (position < 11) { | |
nation.setImageResource(R.drawable.england); | |
} else { | |
nation.setImageResource(R.drawable.spain); | |
} | |
return view; | |
} | |
} |
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="wrap_content" | |
android:layout_height="wrap_content" | |
android:padding="5dp" > | |
<ImageView | |
android:id="@+id/nation" | |
android:layout_width="50dp" | |
android:layout_height="50dp" /> | |
<TextView | |
android:id="@+id/team_name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="30dp" | |
android:layout_weight="30"/> | |
</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
public class TeamListActivity extends ListActivity { | |
static final String[] TEAM = | |
new String[] { "Chonburi FC", "Muangthong", "Buriram Utd", "BEC Tero", "Bangkok Glass", | |
"Liverpool", "Man Utd", "Chelsea", "Arsenal", "Man City", | |
"Barcelona", "Real Madrid", "At.Madrid", "Valencia", "At.Bilbao" }; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setListAdapter(new CustomAdapter(this, TEAM)); | |
} | |
@Override | |
protected void onListItemClick(ListView l, View v, int position, long id) { | |
String name = (String) getListAdapter().getItem(position); | |
Toast.makeText(this, name, Toast.LENGTH_SHORT).show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment