How do I use android.R.layout.simple_list_item_activated_1 in a custom adapter?
I need to use a custom adapter for my ListView in Android because I want to create a special kind of ListViews.
My adapter looks like this:
public class RecordingListAdapter extends BaseAdapter {
List<Recording> recordings;
Context context;
public RecordingListAdapter(Context context, List<Recording> recordings) {
this.context = context;
this.recordings = recordings;
}
@Override
public int getCount() {
return recordings.size();
}
@Override
public Object getItem(int position) {
return recordings.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.recorder_list_item, parent, false);
}
TextView title = (TextView) convertView.findViewById(R.id.recordingTitle);
TextView date = (TextView) convertView.findViewById(R.id.date);
TextView duration = (TextView) convertView.findViewById(R.id.duration);
TextView tags = (TextView) convertView.findViewById(R.id.tags);
Recording recording = recordings.get(position);
title.setText(recording.getTitle());
date.setText(context.getString(R.string.date) + ": " + new SimpleDateFormat("dd.MM.yyyy").format(recording.getDate().getTime()).toString());
duration.setText(context.getString(R.string.duration) + ": " + recording.getDuration().toString());
tags.setText(context.getString(R.string.tags) + ": " + recording.getTagsAsString());
return convertView;
}
}
In my layout, I already use a navigation drawer that uses an ArrayAdapter with the following properties:
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
mMenuEntries
));
I want my list to look like a list of boxes, especially with selected items.
So how can I use this android.R.layout.simple_list_item_activated_1 file in my custom adapter?
Thank!
source to share
Ok, I found the answer myself:
Taking a look at simple_list_item_activated_1.xml I saw:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
Attribute
android:background="?android:attr/activatedBackgroundIndicator"
is interesting. I set this attribute to my RelativeLayout of my custom list item and finally got the same style.
Also: To set the first item checked in your list, add something like
mListView.setItemChecked(0, true);
to your list inside onViewCreated.
source to share
Just inflate the correct layout item in the Adapter class inside the getView () method.
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.simple_list_item_activated_1, parent, false);
}
source to share