Custom list adapter for ListView or ListFragments
How can we define Custom ListAdapter(like ArrayAdapter or IconicAdapter) for ListView in activity开发者_如何学JAVA? How can we do the same for ListFragments? any example or links will be appreciated.Thanks.
A ListFragment
holds a ListView
, so there is really no difference. You just call getListView()
to get a reference and then you can set an adapter as usual.
In my ListFragment onActivityCreated I set the adapter and notify.
The adapter is custom, which inflates a customview. The contents of the text view never show up.
public class MyAdapter extends BaseAdapter {
String[] items;
public MyAdapter(String[] url) {
items = url;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.itemwithimage, null);
}
TextView txt = (TextView) convertView
.findViewById(R.id.listtextView);
txt.setText(items[position]);
Log.d(TAG, "string " + items[position]);
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return items[position];
}
@Override
public int getCount() {
if (items == null)
return 0;
return items.length;
}
}
精彩评论