Where can I find a good tutorial for creating a custom ArrayAdapter? [closed]
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionat开发者_如何学运维ed answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this questionI am trying to build a custom adapter for my app, who's main screen is a ListActivity
with a ListView
in the center. I am absolutely clueless as to how I'd create the adapter, so I was wondering if anyone knew of any good tutorials to walk me through it.
I remember learning ArrayAdapters and it wasn't easy. There are plenty of tutorials on the internet.
Extending ArrayAdapter you'll probably want to override methods.
getCount() - Number of items in your list.
public int getViewTypeCount() - Number of ways data will be presented by the ArrayList. Usually this is 1. But if your list has multiple types of data in it that has multiple views then you'll have to change this number. An Example is the contacts list. It has 2 types. 1 for the contact itself and the other is the letter category that you see for A's, B's, etc.
getItemViewType(position) - For the position, what type should it get? Usually, 1. Unless you have multiple types.
public long getItemId(int position) - The type of item you're going to present.
The really important one! public View getView(int position, View convertView, ViewGroup parent)
- Position obviously is the current item in the list.
- The convertView is for view recycling. What that means if an item scrolls off the screen, it's view is held and waiting to be reused by another item that will come on the screen. This helps performance since you'll only have about 8 or so of the same view on the screen max. Initially convertView will be null since it can't reuse anything but as the user scrolls on the screen you'll get them. ViewGroup parent I'm not so sure beyond using it in view inflation.
Below is an example of getView using the ViewHolder pattern. ViewHolder helps makes your scrolling smooth since you don't have to redo all that findViewById stuff over and over again. Each time you update the view with the new information use the holder.WidgetName to do it.
/* Hypotetical list of names. */
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
ListItemWithSideButton view;
if(convertView == null) {
view = (ListItemWithSideButton)_inflater.inflate(R.layout.some_line_item, parent, false);
holder = new ViewHolder();
view.setTag(holder);
holder.SomeButton = view.findViewById(R.id.some_button);
/* Other views. */
}
else {
view = (ListItemWithSideButton) convertView;
holder = (ViewHolder) convertView.getTag();
}
/* Get the item. */
final String name = getItem(position);
/* Update the data of the view with the new information. */
holder.SomeButton.setText(name);
return view;
}
static class ViewHolder {
Button SomeButton;
/* Other views. */
}
}
Right now I am not having any reference to give you, but this is what you might be doing to get what you want:
You might have the ListView in the XML, so instantiate a ListView object in your code:
ListView myList = (ListView)findViewById(R.id.myListView)
As soon as you get reference to it, create a custom class that extends BaseAdapter.
A good idea will be to put this class inside your Activity class so that it can access all the data that your Activity holds.
While extending BaseAdapter, there are some things that you need to do in order to get things working.
I am explaining all of them below, but implementing the getView() method of the BaseAdapter is the most important thing. This method will be called by the runtime system everytime the ListView draws a row.
So you should be doing all inside this method, that you would want to be done for a single row.
Find the code below:
Class myListActivity extends Activity{
... some code here...
public void onCreate(Bundle savedInstanceState){
.....
myList.setAdapter(new myCustomAdapter());
.....
}
/**
*Custom Adapter class for the ListView containing data
*/
class myCustomAdapter extends BaseAdapter{
TextView userName;
/**
* returns the count of elements in the Array that is used to draw the text in rows
* @see android.widget.Adapter#getCount()
*/
@Override
public int getCount() {
//return the length of the data array, so that the List View knows how much rows it has to draw
return DataArr.length;
}
/**
* @param position The position of the row that was clicked (0-n)
* @see android.widget.Adapter#getItem(int)
*/
@Override
public String getItem(int position) {
return null;
}
/**
* @param position The position of the row that was clicked (0-n)
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public long getItemId(int position) {
return position;
}
/**
* Returns the complete row that the System draws.
* It is called every time the System needs to draw a new row;
* You can control the appearance of each row inside this function.
* @param position The position of the row that was clicked (0-n)
* @param convertView The View object of the row that was last created. null if its the first row
* @param parent The ViewGroup object of the parent view
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final int pos = position;
if(row == null){
//getting custom layout to the row
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.list_row, parent, false);
}
//get the reference to the textview of your row. find the item with row.findViewById()
userName= (TextView)row.findViewById(R.id.user_name);
userName.setText(DataArr[position]);
return row; //the row that ListView draws
}
}
}
Hope it helped you.
Remember to create the layout of the row in a separate layout file.
If you're trying to get deeper, try this link at CommonsGuy's website. It is an excerpt to his awesome book, that specifically deals with the ListView custom adapters
EDIT: here's my blog post about it and a sample project too: http://thetechnib.blogspot.com/2010/12/android-tutorial-custom-adapter-for.html
精彩评论