Last active
December 16, 2015 14:39
-
-
Save leonguyen/5450429 to your computer and use it in GitHub Desktop.
Android Lab: List View with Custom Adapter - Create Custom Adapter class
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.example.androidlab; | |
| import java.util.List; | |
| import android.content.Context; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.ArrayAdapter; | |
| import android.widget.TextView; | |
| public class CustomAdapter extends ArrayAdapter<String> { | |
| private Context context; | |
| private String[] list; | |
| public CustomAdapter(Context context, int textViewResourceId, | |
| String[] objects) { | |
| super(context, textViewResourceId, objects); | |
| // TODO Auto-generated constructor stub | |
| this.context = context; | |
| this.list = objects; | |
| } | |
| @Override | |
| public View getView(int position, View convertView, ViewGroup parent) { | |
| // TODO Auto-generated method stub | |
| View v = convertView; | |
| if (v == null) { | |
| LayoutInflater vi = (LayoutInflater) context | |
| .getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
| v = vi.inflate(R.layout.list_item, null); | |
| } | |
| String str = list[position]; | |
| if (str != null) { | |
| TextView txt = (TextView) v.findViewById(R.id.textView); | |
| if (txt != null) { | |
| txt.setText(str); | |
| } | |
| } | |
| return v; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment