Android Open Source - RealmSample Item Adapter






From Project

Back to project page RealmSample.

License

The source code is released under:

Apache License

If you think the Android project RealmSample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2014 Eduardo Barrenechea/*  w  w  w .j ava2 s  .c o m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ca.barrenechea.realmsample;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import io.realm.RealmResults;

class ItemAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private RealmResults<ListItem> mData;

    public ItemAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public void setData(RealmResults<ListItem> data) {
        mData = data;
        this.notifyDataSetChanged();
    }

    public RealmResults<ListItem> getData() {
        return mData;
    }

    public void sort(String column, boolean order) {
        if (mData != null) {
            mData = mData.sort(column, order);
            this.notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (mData == null) {
            return 0;
        }

        return mData.size();
    }

    @Override
    public ListItem getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = mInflater.inflate(R.layout.adapter_list_item, viewGroup, false);
        }

        final ListItem item = mData.get(i);
        ((TextView) view).setText("Random " + item.getAttribute());

        return view;
    }
}




Java Source Code List

ca.barrenechea.realmsample.ItemActivity.java
ca.barrenechea.realmsample.ItemAdapter.java
ca.barrenechea.realmsample.ListItem.java
ca.barrenechea.realmsample.MainActivity.java