Android Open Source - WarDroid Invasion List View Adapter






From Project

Back to project page WarDroid.

License

The source code is released under:

MIT License

If you think the Android project WarDroid 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

package com.deathsnacks.wardroid.adapters;
//from  ww w .  j a  va 2 s.  c o m
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Build;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.deathsnacks.wardroid.R;
import com.deathsnacks.wardroid.utils.PreferenceUtils;
import com.deathsnacks.wardroid.utils.Utils;
import com.deathsnacks.wardroid.utils.httpclasses.Invasion;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Admin on 25/01/14.
 */
public class InvasionListViewAdapter extends BaseAdapter {
    private ArrayList<String> mCompletedIds;
    private SharedPreferences mPreferences;
    private Activity mActivity;
    private List<String> mLines;
    private List<String> mOriginal;
    private LayoutInflater mInflater;
    private static String TAG = "InvasionListViewAdapter";
    private View mEmptyView;

    public InvasionListViewAdapter(Activity act, List<String> data, View emptyView) {
        this(act, data, emptyView, true);
    }

    public InvasionListViewAdapter(Activity act, List<String> data, View emptyView, boolean remove) {
        mActivity = act;
        mPreferences = PreferenceManager.getDefaultSharedPreferences(act);
        mCompletedIds = new ArrayList<String>(Arrays.asList(PreferenceUtils.fromPersistedPreferenceValue(mPreferences.getString("invasion_completed_ids", ""))));
        if (remove)
            data.remove(0);
        mLines = data;
        mOriginal = data;
        if (mPreferences.getBoolean("hide_completed", false)) {
            List<String> newList = new ArrayList<String>();
            for (int i = 0; i < mOriginal.size(); i++) {
                String line = mOriginal.get(i);
                if (line.split("\\|").length < 18)
                    continue;
                Invasion invasion = new Invasion(line);
                if (mCompletedIds.contains(invasion.getId())) {
                    Log.d(TAG, "marking invasion GONE. " + invasion.getNode());
                } else {
                    newList.add(line);
                }
            }
            mLines = newList;
        }
        mInflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mEmptyView = emptyView;
        /*if (mLines.size() == 0) {
            mEmptyView.setVisibility(View.VISIBLE);
        } else {
            mEmptyView.setVisibility(View.GONE);
        }*/
    }

    public String getOriginalValues() {
        return TextUtils.join("\n", mOriginal);
    }

    @Override
    public void notifyDataSetChanged() {
        mCompletedIds = new ArrayList<String>(Arrays.asList(PreferenceUtils.fromPersistedPreferenceValue(mPreferences.getString("invasion_completed_ids", ""))));
        if (mPreferences.getBoolean("hide_completed", false)) {
            List<String> newList = new ArrayList<String>();
            for (int i = 0; i < mOriginal.size(); i++) {
                String line = mOriginal.get(i);
                Invasion invasion = new Invasion(line);
                if (mCompletedIds.contains(invasion.getId())) {
                    Log.d(TAG, "marking invasion GONE. " + invasion.getNode());
                } else {
                    newList.add(line);
                }
            }
            mLines = newList;
        }
        /*if (mLines.size() == 0) {
            mEmptyView.setVisibility(View.VISIBLE);
        } else {
            mEmptyView.setVisibility(View.GONE);
        }*/
        super.notifyDataSetChanged();
    }

    public int getCount() {
        return mLines.size();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder;
        //define viewholder
        if (view == null) {
            view = mInflater.inflate(R.layout.list_item_invasion, null);
            holder = new ViewHolder();
            holder.completed = view.findViewById(R.id.invasion_completed);
            holder.node = (TextView) view.findViewById(R.id.invasion_node);
            holder.desc = (TextView) view.findViewById(R.id.invasion_desc);
            holder.percent = (TextView) view.findViewById(R.id.invasion_percent);
            holder.invadingfaction = (TextView) view.findViewById(R.id.invasion_invading_faction);
            holder.invadingtype = (TextView) view.findViewById(R.id.invasion_invading_type);
            holder.invadingrewards = (TextView) view.findViewById(R.id.invasion_invading_reward);
            holder.defendingfaction = (TextView) view.findViewById(R.id.invasion_defending_faction);
            holder.defendingtype = (TextView) view.findViewById(R.id.invasion_defending_type);
            holder.defendingrewards = (TextView) view.findViewById(R.id.invasion_defending_reward);
            holder.eta = (TextView) view.findViewById(R.id.invasion_eta);
            holder.bar = (ProgressBar) view.findViewById(R.id.invasion_bar);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        view.setEnabled(true);
        view.setVisibility(View.VISIBLE);
        holder.completed.setVisibility(View.GONE);

        //catch race conditions with hiding items
        if (position >= mLines.size()) {
            Log.e(TAG, "We are above size of invasion array: " + position + " (" + mLines.size() + "), setting GONE");
            view.setVisibility(View.GONE);
            view.setEnabled(false);
            view.setTag(holder);
            return view;
        }

        String line = mLines.get(position);
        String[] parts = line.split("\\|");
        //check if whatever we are parsing is valid
        if (parts.length != 19) {
            view.setVisibility(View.GONE);
            view.setEnabled(false);
            view.setTag(holder);
            return view;
        }

        Invasion invasion = new Invasion(line);
        holder.invasion = invasion;
        if (mCompletedIds.contains(invasion.getId())) {
            if (mPreferences.getBoolean("hide_completed", false)) {
                notifyDataSetChanged();
            } else {
                holder.completed.setVisibility(View.VISIBLE);
            }
        }
        holder.node.setText(String.format("%s (%s)", invasion.getNode(), invasion.getRegion()));
        holder.invadingfaction.setText(invasion.getInvadingFaction());
        holder.invadingtype.setText(invasion.getInvadingFaction().contains("Infestation") ? "" : invasion.getInvadingType());
        String invadingReward = invasion.getInvadingReward();
        if (invadingReward.endsWith("cr"))
            invadingReward = invadingReward.replaceAll("cr$", "*c");
        holder.invadingrewards.setText(invasion.getInvadingFaction().contains("Infestation") ? ""
                : Utils.getImageSpannable(mActivity, invadingReward));
        holder.defendingfaction.setText(invasion.getDefendingFaction());
        holder.defendingtype.setText(invasion.getDefendingType());
        String defendingReward = invasion.getDefendingReward();
        if (defendingReward.endsWith("cr")) {
            defendingReward = defendingReward.replaceAll("cr$", "*c");
        }
        holder.defendingrewards.setText(Utils.getImageSpannable(mActivity, defendingReward));
        holder.percent.setText(invasion.getPercent() + "%");
        holder.desc.setText(invasion.getDescription());
        int percentvalue = (int) Double.parseDouble(invasion.getPercent());
        if (percentvalue > 100)
            percentvalue = 100;
        if (percentvalue < 0)
            percentvalue = 0;
        holder.bar.setProgress(0);
        Rect bounds = null;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
            bounds = holder.bar.getProgressDrawable().getBounds();
            holder.bar.getProgressDrawable().setBounds(bounds);
        }
        if (invasion.getInvadingFaction().contains("Grineer"))
            holder.bar.setProgressDrawable(mActivity.getResources().getDrawable(R.drawable.grineer_corpus_bar));
        else if (invasion.getInvadingFaction().contains("Corpus"))
            holder.bar.setProgressDrawable(mActivity.getResources().getDrawable(R.drawable.corpus_grineer_bar));
        else if (invasion.getInvadingFaction().contains("Infestation")) {
            if (invasion.getDefendingFaction().contains("Corpus"))
                holder.bar.setProgressDrawable(mActivity.getResources().getDrawable(R.drawable.infestation_corpus_bar));
            else if (invasion.getDefendingFaction().contains("Grineer"))
                holder.bar.setProgressDrawable(mActivity.getResources().getDrawable(R.drawable.infestation_grineer_bar));
        }
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
            holder.bar.getProgressDrawable().setBounds(bounds);
        }
        holder.bar.setProgress(percentvalue);
        holder.eta.setText(invasion.getEta());
        view.setTag(holder);
        return view;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public class ViewHolder {
        public View completed;
        public TextView node;
        public TextView desc;
        public TextView percent;
        public TextView invadingfaction;
        public TextView defendingfaction;
        public TextView invadingtype;
        public TextView invadingrewards;
        public TextView defendingtype;
        public TextView defendingrewards;
        public TextView eta;
        public ProgressBar bar;
        public Invasion invasion;
    }
}




Java Source Code List

afzkl.development.colorpickerview.dialog.ColorPickerDialog.java
afzkl.development.colorpickerview.drawable.AlphaPatternDrawable.java
afzkl.development.colorpickerview.preference.ColorPickerPreference.java
afzkl.development.colorpickerview.view.ColorPanelView.java
afzkl.development.colorpickerview.view.ColorPickerView.java
android.support.v4.preference.PreferenceFragment.java
android.support.v4.preference.PreferenceManagerCompat.java
com.deathsnacks.wardroid.Constants.java
com.deathsnacks.wardroid.activities.CustomFilterActivity.java
com.deathsnacks.wardroid.activities.MainActivity.java
com.deathsnacks.wardroid.activities.SettingsActivity.java
com.deathsnacks.wardroid.adapters.AlertsListViewAdapter.java
com.deathsnacks.wardroid.adapters.BadlandsListViewAdapter.java
com.deathsnacks.wardroid.adapters.InvasionListViewAdapter.java
com.deathsnacks.wardroid.adapters.NewsListViewAdapter.java
com.deathsnacks.wardroid.adapters.SeparatedListAdapter.java
com.deathsnacks.wardroid.fragments.AlertsFragment.java
com.deathsnacks.wardroid.fragments.BadlandDialogFragment.java
com.deathsnacks.wardroid.fragments.BadlandsFragment.java
com.deathsnacks.wardroid.fragments.InvasionFragment.java
com.deathsnacks.wardroid.fragments.NewsFragment.java
com.deathsnacks.wardroid.fragments.SettingsFragment.java
com.deathsnacks.wardroid.gson.alert.Activation.java
com.deathsnacks.wardroid.gson.alert.Alert.java
com.deathsnacks.wardroid.gson.alert.CountedItem.java
com.deathsnacks.wardroid.gson.alert.Expiry.java
com.deathsnacks.wardroid.gson.alert.MissionInfo.java
com.deathsnacks.wardroid.gson.alert.MissionReward.java
com.deathsnacks.wardroid.gson.alert._id.java
com.deathsnacks.wardroid.gson.badlands.AttId.java
com.deathsnacks.wardroid.gson.badlands.AttackerInfo.java
com.deathsnacks.wardroid.gson.badlands.BadlandNode.java
com.deathsnacks.wardroid.gson.badlands.ConflictExpiration.java
com.deathsnacks.wardroid.gson.badlands.CountedItem.java
com.deathsnacks.wardroid.gson.badlands.DefId.java
com.deathsnacks.wardroid.gson.badlands.DefenderInfo.java
com.deathsnacks.wardroid.gson.badlands.DeploymentActivationTime.java
com.deathsnacks.wardroid.gson.badlands.End.java
com.deathsnacks.wardroid.gson.badlands.History.java
com.deathsnacks.wardroid.gson.badlands.Id.java
com.deathsnacks.wardroid.gson.badlands.LastHealTime.java
com.deathsnacks.wardroid.gson.badlands.MissionInfo.java
com.deathsnacks.wardroid.gson.badlands.MissionRewardExtra.java
com.deathsnacks.wardroid.gson.badlands.MissionReward.java
com.deathsnacks.wardroid.gson.badlands.Start.java
com.deathsnacks.wardroid.gson.badlands.TaxChangeAllowedTime.java
com.deathsnacks.wardroid.gson.badlands.WinId.java
com.deathsnacks.wardroid.gson.badlands._id.java
com.deathsnacks.wardroid.services.GcmBroadcastReceiver.java
com.deathsnacks.wardroid.services.NotificationsUpdateReceiver.java
com.deathsnacks.wardroid.services.PollingAlarmReceiver.java
com.deathsnacks.wardroid.services.UpdateReceiver.java
com.deathsnacks.wardroid.utils.GlobalApplication.java
com.deathsnacks.wardroid.utils.Http.java
com.deathsnacks.wardroid.utils.Names.java
com.deathsnacks.wardroid.utils.Platform.java
com.deathsnacks.wardroid.utils.PreferenceActivityCallbacks.java
com.deathsnacks.wardroid.utils.PreferenceUtils.java
com.deathsnacks.wardroid.utils.UnifiedPreferenceUtils.java
com.deathsnacks.wardroid.utils.Utils.java
com.deathsnacks.wardroid.utils.gcmclasses.Alert.java
com.deathsnacks.wardroid.utils.gcmclasses.Invasion.java
com.deathsnacks.wardroid.utils.httpclasses.Alert.java
com.deathsnacks.wardroid.utils.httpclasses.InvasionMini.java
com.deathsnacks.wardroid.utils.httpclasses.Invasion.java
com.deathsnacks.wardroid.utils.httpclasses.News.java
com.deathsnacks.wardroid.utils.httpclasses.Sale.java
com.deathsnacks.wardroid.utils.preferences.IntEditTextPreference.java
com.deathsnacks.wardroid.utils.preferences.MultiSelectListPreference.java