Created
August 12, 2015 13:02
-
-
Save burnix/21d7d8745e9872c9edd1 to your computer and use it in GitHub Desktop.
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
import android.app.ListActivity; | |
import android.content.Context; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends ListActivity { | |
private static final List<Cat> cats = new ArrayList<Cat>(); | |
static { | |
cats.add(new Cat("Васька", "котэ")); | |
cats.add(new Cat("Мурзик", "котяра")); | |
cats.add(new Cat("Мурка", "кошка")); | |
cats.add(new Cat("Барсик", "котик")); | |
cats.add(new Cat("Лиза", "кошечка")); | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ArrayAdapter<Cat> adapter = new CatAdapter(this); | |
setListAdapter(adapter); | |
} | |
private static class Cat { | |
public final String name; | |
public final String gender; | |
public Cat(String name, String gender) { | |
this.name = name; | |
this.gender = gender; | |
} | |
} | |
private class CatAdapter extends ArrayAdapter<Cat> { | |
public CatAdapter(Context context) { | |
super(context, android.R.layout.simple_list_item_2, cats); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
Cat cat = getItem(position); | |
ViewHolder holder = new ViewHolder(); | |
if (convertView == null) { | |
convertView = LayoutInflater.from(getContext()) | |
.inflate(android.R.layout.simple_list_item_2, null); | |
(holder.name = (TextView) convertView.findViewById(android.R.id.text1)) | |
.setText(cat.name); | |
(holder.gender = (TextView) convertView.findViewById(android.R.id.text2)) | |
.setText(cat.gender); | |
convertView.setTag(holder); | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
return convertView; | |
} | |
} | |
static class ViewHolder{ | |
public TextView name,gender; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@OverRide
public View getView(int position, View convertView, ViewGroup parent) {
Cat cat = getItem(position);
ViewHolder holder;