Last active
August 29, 2015 14:03
-
-
Save pavlospt/0f044cf02701fb62e0b9 to your computer and use it in GitHub Desktop.
Complete list with files for the CustomAdapter post of my blog.
This file contains 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.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.TextView; | |
import java.util.List; | |
public class CustomAdapter extends ArrayAdapter<String> { | |
protected Context mContext; | |
private LayoutInflater mInflater; | |
public CustomAdapter(Context context, List<String> objects) { | |
super(context, 0, objects); | |
this.mContext = context; | |
mInflater = LayoutInflater.from(context); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
ViewHolder holder; | |
//If convertView is null, we inflate our Layout in convertView. | |
if (convertView == null) { | |
convertView = mInflater.inflate(R.layout.item_layout, parent, false); | |
//We instantiate our ViewHolder object, in order to use it below. | |
holder = new ViewHolder(); | |
//Let's say that our custom layout file contains only one TextView. | |
//So we use the ViewHolder pattern to instantiate it. | |
holder.mTextView = (TextView) convertView.findViewById(R.id.textView); | |
convertView.setTag(holder); | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
//Because we extend ArrayAdapter, we can use the position and retrieve | |
//the current object using the getItem(int i) method. | |
String item = getItem(position); | |
//We only execute the following, if our item is not null. | |
if (item != null){ | |
holder.mTextView.setText(item); | |
} | |
return convertView; | |
} | |
private static class ViewHolder { | |
TextView mTextView; | |
} | |
public Context getContext() { | |
return mContext; | |
} | |
} |
This file contains 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.Activity; | |
import android.os.Bundle; | |
import android.widget.ListView; | |
import android.view.View; | |
import java.util.ArrayList; | |
import android.widget.TextView; | |
import android.widget.AdapterView; | |
public class CustomListActivity extends Activity { | |
private ListView mListView; | |
private CustomAdapter mAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_custom_list); | |
//ListView instantiation | |
mListView = (ListView) findViewById(android.R.id.list); | |
ArrayList<String> lists = new ArrayList<String>(); | |
//Filling an ArrayList with our objects | |
lists.add("a"); | |
lists.add("b"); | |
lists.add("c"); | |
lists.add("d"); | |
//Initializing our CustomAdapter and passing the context and our data. | |
mAdapter = new CustomAdapter(this,lists); | |
//Setting the adapter to our ListView | |
if (mListView != null){ | |
mListView.setAdapter(mAdapter); | |
} | |
} | |
} |
This file contains 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:orientation="vertical" android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textAppearance="?android:attr/textAppearanceMedium" | |
android:text="Medium Text" | |
android:id="@+id/textView" | |
android:layout_gravity="center_horizontal" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment