ContentsListTagSearchActivity.java :  » App » jatta » com » tamtamy » jatta » activity » Android Open Source

Android Open Source » App » jatta 
jatta » com » tamtamy » jatta » activity » ContentsListTagSearchActivity.java
/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 04): <l.altieri@reply.it> wrote this file.
 * As long as you retain this notice you can do whatever you want with this
 * stuff. If we meet some day, and you think this stuff is worth it, you can buy
 * me a beer in return
 * ----------------------------------------------------------------------------
 */
package com.tamtamy.jatta.activity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.tamtamy.jatta.R;
import com.tamtamy.jatta.TamTamyHelper;
import com.tamtamy.jatta.activity.content.ContentDetailsActivity;
import com.tamtamy.jatta.adapter.ContentListAdapter;
import com.tamtamy.jatta.strongbox.JattaData;
import com.tamtamy.jttamobile.data.Content;
import com.tamtamy.jttamobile.data.ContentList;
import com.tamtamy.jttamobile.data.TagDetail;
import com.tamtamy.jttamobile.exception.JTTAException;

/**
 * @author mirko
 * @version $Id: ContentsListTagSearchActivity.java 144 2011-02-26 15:50:05Z m.sciachero@gmail.com $
 *
 */
public class ContentsListTagSearchActivity extends ListActivity implements OnItemClickListener {

  private static ContentList contents;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.content_list);
    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(this);
    SearchContent sc = new SearchContent();
    sc.execute();
  }


  private ContentList getContentsList(List<String> contentIdList) {
    ArrayList<Content> result = new ArrayList<Content>();
    Iterator<String> idListIterator = contentIdList.iterator();
    while (idListIterator.hasNext()) { //this while is not good! What if 1K results   
      String id = (String) idListIterator.next();
      Content content = null;
      try {
        content = TamTamyHelper.getInstance().getContent(id);
        result.add(content);
      } catch (JTTAException e) {
        android.util.Log.e(this.getClass().getSimpleName(), e.getMessage(), e);
      }
    }
    contents = new ContentList(result);
    return contents;
  }


  protected void onClickHandler() {
    // open contentDetails activity
    Intent contentDetailsIntent = new Intent(ContentsListTagSearchActivity.this, ContentDetailsActivity.class);
    startActivity(contentDetailsIntent);
  }


  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    android.util.Log.d(this.getClass().getSimpleName(),"position: " + position);
    Intent contentDetailsIntent = new Intent(this, ContentDetailsActivity.class);
    contentDetailsIntent.putExtra("com.tamtamy.jatta.content_list_item_selected", position);
    contentDetailsIntent.putExtra("com.tamtamy.jatta.datasource", ContentDetailsActivity.CONTENT_TAG_LIST);
    startActivity(contentDetailsIntent);    
  }

  public static Content getSelectedContent(int position){
    return contents.get(position);    
  }
  
  private class SearchContent extends AsyncTask<String, Void, Void> {
    final private ProgressDialog dialog = new ProgressDialog(getDialogContext());
    //private String op = null;
    private Context getDialogContext() {
      Context context;
      if (getParent() != null) 
        context = getParent();
      else 
        context = ContentsListTagSearchActivity.this;
      return context;
    }

    protected void onPreExecute() {
      try {
        this.dialog.setMessage("Loading...");
        this.dialog.show();
      } catch (Exception e) {
      }
    }

    protected Void doInBackground(final String... args) {
      try {
        String searchString = JattaData.getInstance().getSearchString();
        TagDetail tagDetails = null;
        tagDetails = TamTamyHelper.getInstance().getTagDetail(searchString);
        List<String> contentIdList = tagDetails.getContentsid();
        contents = getContentsList(contentIdList);
        
      } catch (JTTAException e) {
        // TODO Auto-generated catch block
        android.util.Log.e(this.getClass().getSimpleName(), e.getMessage(), e);
      }    
      return null;
    }

    protected void onPostExecute(final Void unused) {
      ContentsListTagSearchActivity.this.setListAdapter(new ContentListAdapter(ContentsListTagSearchActivity.this, R.layout.content_list_item, contents));
      if (this.dialog!=null && this.dialog.isShowing()) {
        this.dialog.dismiss();
      }
    }
  }
}  

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.