Skip to content

Instantly share code, notes, and snippets.

@acappelli
Created October 9, 2015 13:54
Show Gist options
  • Save acappelli/706afc2a7671ae2aa647 to your computer and use it in GitHub Desktop.
Save acappelli/706afc2a7671ae2aa647 to your computer and use it in GitHub Desktop.
/*
* Kontalk Android client
* Copyright (C) 2015 Kontalk Devteam <devteam@kontalk.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kontalk.ui.adapter;
import org.kontalk.R;
import org.kontalk.data.Contact;
import org.kontalk.ui.ContactsListActivity;
import org.kontalk.ui.view.ContactsListItem;
import org.kontalk.ui.view.MessageListItem;
import org.w3c.dom.Text;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.AbsListView.RecyclerListener;
import android.widget.TextView;
import com.twotoasters.sectioncursoradapter.SectionCursorAdapter;
import lb.library.cursor.SearchablePinnedHeaderCursorListViewAdapter;
public class ContactsListAdapter extends SearchablePinnedHeaderCursorListViewAdapter {
private static final String TAG = ContactsListActivity.TAG;
private final LayoutInflater mFactory;
private OnContentChangedListener mOnContentChangedListener;
private Context context;
public ContactsListAdapter(Context context, ListView list) {
super(context, null, false);
mFactory = LayoutInflater.from(context);
list.setRecyclerListener(new RecyclerListener() {
public void onMovedToScrapHeap(View view) {
if (view instanceof MessageListItem) {
((ContactsListItem) view).unbind();
}
}
});
this.context = context;
}
@Override
protected TextView findHeaderView(View itemView) {
return null;
}
public interface OnContentChangedListener {
void onContentChanged(ContactsListAdapter adapter);
}
public void setOnContentChangedListener(OnContentChangedListener l) {
mOnContentChangedListener = l;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View inflated = mFactory.inflate(R.layout.contacts_list_item, parent, false);
final ViewHolder holder = new ViewHolder();
holder.headerView = (TextView) inflated.findViewById(R.id.header_text);
inflated.setTag(holder);
return inflated;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
if (!(view instanceof ContactsListItem)) {
Log.e(TAG, "Unexpected bound view: " + view);
return;
}
ContactsListItem headerView = (ContactsListItem) view;
Contact contact = Contact.fromUsersCursor(context, cursor);
headerView.bind(context, contact);
}
@Override
protected Cursor getFilterCursor(CharSequence charSequence) {
return null;
}
@Override
protected void onContentChanged() {
Cursor c = getCursor();
if (c != null && !c.isClosed() && mOnContentChangedListener != null) {
mOnContentChangedListener.onContentChanged(this);
}
}
private static class ViewHolder {
public TextView headerView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment