GameListSearchResult.java :  » Database-Persistance » cheatdatabase-android » com » cheatdatabase » android » Android Open Source

Android Open Source » Database Persistance » cheatdatabase android 
cheatdatabase android » com » cheatdatabase » android » GameListSearchResult.java
package com.cheatdatabase.android;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cheatdatabase.android.pojo.Game;
import com.cheatdatabase.android.tools.GetDataFromWeb;

/**
 * Anzeige einer Liste mit Games, welche durch die Suche gefunden wurden.
 * http://developer.android.com/guide/tutorials/views/hello-listview.html
 * 
 * @author erbsland {@link http
 *         ://www.softwarepassion.com/android-series-custom-listview
 *         -items-and-adapters}
 */
public class GameListSearchResult extends ListActivity {

  private String searchString, systemName, gameListAsString;
  private int systemId;
  private Game[] gameMatches;
  private Intent gameListIntent;

  // Transparent Popup
  private Animation animShow;
  private TransparentPanel popup = null;
  private Button hideButton = null;
  private TextView errorMsgTitle = null, errorMsgText = null;

  private ProgressDialog m_ProgressDialog = null;
  private ArrayList<Game> m_orders = null;
  private OrderAdapter m_adapter;
  private Runnable viewOrders;
  private ConnectivityManager cm;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cheatdatabaselist_layout);

    // Internet-Verbindungs Check
    cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    gameListIntent = getIntent();
    systemId = gameListIntent.getIntExtra("systemId", -1);
    searchString = gameListIntent.getStringExtra("searchString");
    systemName = gameListIntent.getStringExtra("systemName");
    gameListAsString = gameListIntent.getStringExtra("gameListAsString");

    if (cm.getActiveNetworkInfo() != null) {
      gameMatches = GetDataFromWeb.getGameListConvertStringToGameList(gameListAsString, systemId);
    } else {
      Toast.makeText(this, R.string.no_internet, Toast.LENGTH_SHORT).show();
    }

    // Fenstertitel setzen
    setTitle(R.string.search_results);
    setTitle(getTitle() + " '" + searchString + "'");

    m_orders = new ArrayList<Game>();
    this.m_adapter = new OrderAdapter(this, R.layout.cheatdatabaselist_layout, m_orders);
    setListAdapter(this.m_adapter);

    // "Loading" PopUp Information
    viewOrders = new Runnable() {
      @Override
      public void run() {

        m_orders = new ArrayList<Game>();

        if (gameMatches.length > 0) {
          for (int j = 0; j < gameMatches.length; j++) {
            m_orders.add(gameMatches[j]);
          }
        }

        runOnUiThread(returnRes);
      }
    };
    Thread thread = new Thread(null, viewOrders, "MagentoBackground");
    thread.start();
    m_ProgressDialog = ProgressDialog.show(GameListSearchResult.this, getString(R.string.please_wait) + "...", getString(R.string.retrieving_data) + "...", true);

  }

  private Runnable returnRes = new Runnable() {

    @Override
    public void run() {
      if (m_orders != null && m_orders.size() > 0) {
        m_adapter.notifyDataSetChanged();
        for (int i = 0; i < m_orders.size(); i++)
          m_adapter.add(m_orders.get(i));
      }
      m_ProgressDialog.dismiss();
      m_adapter.notifyDataSetChanged();
    }
  };

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if (cm.getActiveNetworkInfo() != null) {
      Log.v("Done", "Item clicked is " + position);

      // Ausgewhlte Informationen holen
      String cheatMatches = "";
      cheatMatches = GetDataFromWeb.getCheatTitleListAsString(gameMatches[position].getGameId());

      if (cheatMatches.length() < 5) {
        errorMsgTitle.setText(R.string.no_cheats_title);
        errorMsgText.setText(R.string.no_cheats_text);

        popup.setVisibility(View.VISIBLE);
        popup.startAnimation(animShow);
        hideButton.setEnabled(true);
      } else {
        Intent explicitIntent = new Intent(GameListSearchResult.this, CheatTitleList.class);
        explicitIntent.putExtra("gameId", gameMatches[position].getGameId());
        explicitIntent.putExtra("gameName", gameMatches[position].getGameName());
        explicitIntent.putExtra("systemName", systemName);
        explicitIntent.putExtra("systemId", systemId);

        startActivity(explicitIntent);
      }
    } else {
      Toast.makeText(this, R.string.no_internet, Toast.LENGTH_SHORT).show();
    }
  }

  private class OrderAdapter extends ArrayAdapter<Game> {

    private ArrayList<Game> items;

    public OrderAdapter(Context context, int textViewResourceId, ArrayList<Game> items) {
      super(context, textViewResourceId, items);
      this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View v = convertView;
      if (v == null) {
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.gamelist_item, null);
      }
      Game o = items.get(position);
      if (o != null) {
        TextView tt = (TextView) v.findViewById(R.id.toptext);
        tt.setPadding(0, 3, 0, 3);
        TextView bt = (TextView) v.findViewById(R.id.additional_text);
        if (tt != null) {
          tt.setText(o.getGameName());
        }
        if (bt != null) {
          if (o.getAnzahlCheats() == 1) {
            bt.setText(o.getAnzahlCheats() + " " + getString(R.string.cheat));
          } else {
            bt.setText(o.getAnzahlCheats() + " " + getString(R.string.cheats));
          }
        }
      }
      return v;
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.