You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
etesync-android/app/src/main/java/at/bitfire/davdroid/ui/AccountListFragment.java

82 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* Copyright © 2013 2016 Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.davdroid.ui;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.OnAccountsUpdateListener;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import at.bitfire.davdroid.Constants;
import at.bitfire.davdroid.R;
public class AccountListFragment extends ListFragment implements OnAccountsUpdateListener {
protected AccountManager accountManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setListAdapter(new AccountListAdapter(getContext()));
accountManager = AccountManager.get(getContext());
accountManager.addOnAccountsUpdatedListener(this, null, true);
return inflater.inflate(R.layout.account_list, container, false);
}
@Override
public void onDestroyView() {
accountManager.removeOnAccountsUpdatedListener(this);
super.onDestroyView();
}
@Override
public void onAccountsUpdated(Account[] accounts) {
AccountListAdapter adapter = (AccountListAdapter)getListAdapter();
if (adapter != null) {
adapter.clear();
for (Account account : accounts)
if (Constants.ACCOUNT_TYPE.equals(account.type))
adapter.add(account);
}
}
class AccountListAdapter extends ArrayAdapter<Account> {
public AccountListAdapter(Context context) {
super(context, R.layout.account_list_item);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
v = inflater.inflate(R.layout.account_list_item, parent, false);
}
Account account = getItem(position);
TextView tvName = (TextView)v.findViewById(R.id.account_name);
tvName.setText(account.name);
return v;
}
}
}