Android Open Source - SeeKampf Auction List Fragment






From Project

Back to project page SeeKampf.

License

The source code is released under:

GNU General Public License

If you think the Android project SeeKampf 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 net.avedo.seekampf.fragments;
/*from  w  w  w.j av  a 2s  .c om*/
import java.text.DecimalFormat;
import java.util.Date;

import net.avedo.seekampf.R;
import net.avedo.seekampf.R.color;
import net.avedo.seekampf.R.drawable;
import net.avedo.seekampf.R.id;
import net.avedo.seekampf.R.layout;
import net.avedo.seekampf.R.string;
import net.avedo.seekampf.core.CustomAdapter;
import net.avedo.seekampf.models.Auction;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class AuctionListFragment extends RestListFragment<Auction> {
  public static final String STONE_THROWER = "steinewerfer";
  public static final String SPEARMAN = "speertraeger";
  public static final String ARCHER = "bogenschtzen";

  public static final int AUCTION_NORMAL = 0;
  public static final int AUCTION_OUTBIT = AUCTION_NORMAL + 1;
  public static final int AUCTION_MAX_BID = AUCTION_OUTBIT + 1;
  public static final int AUCTION_OWNER = AUCTION_MAX_BID + 1;
  
  public static final String TAG = "AuctionList";
  
  private static Date today;

  @Override
  public void onCreate(Bundle paramBundle) {
    super.onCreate(paramBundle);
    
    today = new Date();
  }

  @Override
    protected String fetchServiceTag() {
      return TAG;
    }

  @Override
  protected void fetchServiceAdapter(Auction[] auctions) {
    adapter = new AuctionAdapter(getActivity(), R.layout.auction_row, auctions);
  }

  @Override
  protected Class<Auction[]> fetchServiceObjClass() {
    return Auction[].class;
  }

  @Override
  protected String fetchServiceUrl() {
    return "https://www.seekampf.de/api/api2.php?server="
        + settings.getString(res.getString(R.string.prefs_server_key), "1")
        + "&typ=auktionen";
  }
  
  private class AuctionAdapter extends CustomAdapter<Auction> {
    public AuctionAdapter(Context context, int resId, Auction[] auctions) {
      super(context, resId, auctions);
    }
    
    
    @Override
      public View getView(int position, View convertView, ViewGroup parent) {
      super.getView(position, convertView, parent);
      
          if (convertView == null) {
            // Fetch the layout inflater ...
              LayoutInflater li = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              
              // ... and load the auction row layout.
              convertView = li.inflate(R.layout.auction_row, null);
          }
          
      // Fetch the current auction.
      Auction auction = getItem(position);
      
      // Fetch the auction icon view ...
      ImageView auctionIcon = (ImageView) convertView.findViewById(R.id.auctionIcon);
      
      // ... and assign the correct icon.
      if(auction.getWas().equals(STONE_THROWER)) {
        auctionIcon.setImageResource(R.drawable.stone_thrower_icon);
      } else if(auction.getWas().equals(SPEARMAN)) {
        auctionIcon.setImageResource(R.drawable.speermen_icon);
      } else {
        auctionIcon.setImageResource(R.drawable.archer_icon);
      }
      
      // Fetch the auction type view, ...
      TextView auctionType = (TextView) convertView.findViewById(R.id.auctionType);
      
      // ... adjust the color of the auction title, ...
      if(auction.getStatus() == AUCTION_OUTBIT) {
        auctionType.setTextColor(getResources().getColor(R.color.red));
      } else if(auction.getStatus() == AUCTION_MAX_BID) {
        auctionType.setTextColor(getResources().getColor(R.color.green));
      } else if(auction.getStatus() == AUCTION_OWNER) {
        auctionType.setTextColor(getResources().getColor(R.color.blue));
      }
      
      // ... prepare the auction type ...
      String header = "" + auction.getAnzahl() + " ";
      if(auction.getWas().equals(STONE_THROWER)) {
        header += res.getString(R.string.stoneThrowers);
      } else if(auction.getWas().equals(SPEARMAN)) {
        header += res.getString(R.string.spearmen);
      } else {
        header += res.getString(R.string.archers);
      }
      
      // ... and assign it to the view.
      auctionType.setText(header);
      
      // Fetch the auction gold view ...
      TextView auctionGold = (TextView) convertView.findViewById(R.id.auctionGold);
      
      // ... and assign the received value.
      auctionGold.setText("" + auction.getGold());
      
      // Fetch the auction stone view ...
      TextView auctionStone = (TextView) convertView.findViewById(R.id.auctionStone);
      
      // ... and assign the received value.
      auctionStone.setText("" + auction.getStein());
      
      // Fetch the auction wood view ...
      TextView auctionWood = (TextView) convertView.findViewById(R.id.auctionWood);
      
      // ... and assign the received value.
      auctionWood.setText("" + auction.getHolz());
      
      // Fetch the auction timer view, ...
      final TextView auctionTimer = (TextView) convertView.findViewById(R.id.auctionTimer);
      
      // ... calculate the remaining time ...
      long remainingMillis = (auction.getTime() * 1000) - today.getTime();

      // ... and start a new CountDownTimer for this auction.
      new CountDownTimer(remainingMillis, 1000) {
        public void onTick(long millisUntilFinished) {
          // Initialize a variable that holds the remaining milliseconds ...
          long remaining = millisUntilFinished;
          
              // and a DecimalFormat object.
                DecimalFormat df = new DecimalFormat("00");
                
                // Get the elapsed days ...
                int days = (int) (remaining / (24 * 3600 * 1000));
                
                // ... and calculate the remaining milliseconds.
                remaining = remaining % (24 * 3600 * 1000);
                
                // Get the elapsed hours ...
                int hours = (int) (remaining / (3600 * 1000));
                
                // ... and calculate the remaining milliseconds.
                remaining = remaining % (3600 * 1000);

                // Get the elapsed minutes ...
                int minutes = (int)(remaining / (60 * 1000));
                
                // ... and calculate the remaining milliseconds.
                remaining = (int)(remaining % (60 * 1000));

                // Get the elapsed seconds, ...
                int seconds = (int)(remaining / 1000);
                
                // ... setup the result string ...
                String text = "";
                if(days > 0) {
                  text += days + "d ";
                }
                
                text += df.format(hours) + ":";
                text += df.format(minutes) + ":";
                text += df.format(seconds);

                // ... and display it.
          auctionTimer.setText(text);
        }

        public void onFinish() {
          auctionTimer.setText("00:00:00");
        }
      }.start();
      
      return convertView;
    }
  }
}




Java Source Code List

net.avedo.seekampf.BuildConfig.java
net.avedo.seekampf.core.ChangeLog.java
net.avedo.seekampf.core.CustomAdapter.java
net.avedo.seekampf.core.MainActivity.java
net.avedo.seekampf.core.OceanView.java
net.avedo.seekampf.core.OverScrollerCompat.java
net.avedo.seekampf.core.RestDetailsActivity.java
net.avedo.seekampf.core.ScaleGestureDetectorCompat.java
net.avedo.seekampf.core.VolleyActivity.java
net.avedo.seekampf.core.Zoomer.java
net.avedo.seekampf.fragments.AboutFragment.java
net.avedo.seekampf.fragments.AllianceListFragment.java
net.avedo.seekampf.fragments.AuctionListFragment.java
net.avedo.seekampf.fragments.HomeDetailsFragment.java
net.avedo.seekampf.fragments.HomeFragment.java
net.avedo.seekampf.fragments.IslandDetailsFragment.java
net.avedo.seekampf.fragments.IslandListFragment.java
net.avedo.seekampf.fragments.MessageDetailsFragment.java
net.avedo.seekampf.fragments.MessageListFragment.java
net.avedo.seekampf.fragments.OceanFragment.java
net.avedo.seekampf.fragments.PlayerListFragment.java
net.avedo.seekampf.fragments.RestDetailsFragment.java
net.avedo.seekampf.fragments.RestListFragment.java
net.avedo.seekampf.fragments.SettingsFragment.java
net.avedo.seekampf.models.Alliance.java
net.avedo.seekampf.models.Auction.java
net.avedo.seekampf.models.BaseModel.java
net.avedo.seekampf.models.Island.java
net.avedo.seekampf.models.Message.java
net.avedo.seekampf.models.Player.java
net.avedo.seekampf.utils.AuthGsonRequest.java
net.avedo.seekampf.utils.Constants.java
net.avedo.seekampf.utils.Interfaces.java
net.avedo.seekampf.utils.VolleyErrorHelper.java