2015年12月14日 星期一

Custom ArrayAdapter

video:

https://plus.google.com/u/0/events/chlh8qqr5q5grs1lajpqnvvql8kcfem=1&authkey=CNXMrZuHsMWhNg

explain by website:
https://www.bignerdranch.com/blog/customizing-android-listview-rows-subclassing/

http://guides.codepath.com/android/Using-an-ArrayAdapter-with-ListView#using-a-custom-arrayadapter



ArrayAdapter
simple use
when only want to show same simple thing in the list view
for example ,a single text in the list view


ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"A", "B", "C", "D"});

custom the adapter
create  a class that extends ArrayAdapte
oerrride a constructor
overrride the get view
remember!!!!  in the getVIew
we need to check is the convertView==null


public class MyArrayAdapter extends ArrayAdapter<MyStoreClass> {


   public MyArrayAdapter(Context context, int resource, List<MyStoreClass> objects) {
       super(context, resource, objects);
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       MyStoreClass store = getItem(position);
       if(convertView==null){
           convertView=LayoutInflater.from(getContext()).inflate(R.layout.lisview_layout,parent,false);
       }

       TextView tv1 = (TextView) convertView.findViewById(R.id.tvList1);
       TextView tv2 = (TextView) convertView.findViewById(R.id.tvList2);
       tv1.setText(store.getN1()+"");
       tv2.setText(store.getN2()+"");



       return convertView;
   }
}