jan

10

Posted by : admin | On : 10 janvier 2012

Rss Reader

Bellow a Simple code about how to make a simple Rss Reader handle by SAX handler.



package mapping.rss;

public class Item {
	private String title ;
	private String description;
	private String link;
	private String pubDate;
	private String source;
	private String mediaContent;
	private String mediaText;
	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}
	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}
	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	/**
	 * @return the link
	 */
	public String getLink() {
		return link;
	}
	/**
	 * @param link the link to set
	 */
	public void setLink(String link) {
		this.link = link;
	}
	/**
	 * @return the pubDate
	 */
	public String getPubDate() {
		return pubDate;
	}
	/**
	 * @param pubDate the pubDate to set
	 */
	public void setPubDate(String pubDate) {
		this.pubDate = pubDate;
	}
	/**
	 * @return the source
	 */
	public String getSource() {
		return source;
	}
	/**
	 * @param source the source to set
	 */
	public void setSource(String source) {
		this.source = source;
	}
	/**
	 * @return the mediaContent
	 */
	public String getMediaContent() {
		return mediaContent;
	}
	/**
	 * @param mediaContent the mediaContent to set
	 */
	public void setMediaContent(String mediaContent) {
		this.mediaContent = mediaContent;
	}
	/**
	 * @return the mediaText
	 */
	public String getMediaText() {
		return mediaText;
	}
	/**
	 * @param mediaText the mediaText to set
	 */
	public void setMediaText(String mediaText) {
		this.mediaText = mediaText;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Item [title=");
		builder.append(title);
		builder.append(", description=");
		builder.append(description);
		builder.append(", link=");
		builder.append(link);
		builder.append(", pubDate=");
		builder.append(pubDate);
		builder.append(", source=");
		builder.append(source);
		builder.append(", mediaContent=");
		builder.append(mediaContent);
		builder.append(", mediaText=");
		builder.append(mediaText);
		builder.append("]");
		return builder.toString();
	}

}

package xml;

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

import mapping.rss.Item;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SearchItemRssHandler extends DefaultHandler {
	private final static String CHANNEL="channel";
	private final static String ITEM="item";
	private final static String TITLE="title";
	private final static String LINK="link";
	private final static String DESCRIPTION="description";
	private final static String SOURCE="source";
	private final static String PUBDATE="PUBDATE";

	private boolean bfChannel = false;
	private boolean bfItem = false;
	private boolean bfTitle = false;
	private boolean bflink = false ;
	private boolean bfDescription = false ;
	private boolean bfSource = false ;
	private boolean bfPubdate = false; 

	private Item item ;
	private List<Item> items ;
	public void startElement(String uri, String localName,String qName,
			Attributes attributes) throws SAXException {
		if(qName.equalsIgnoreCase(CHANNEL)){
			items = new ArrayList<Item>();
			bfChannel = true;
		}
		if(qName.equalsIgnoreCase(ITEM)){
			item = new Item();
			bfItem = true ;
		}
		if(qName.equalsIgnoreCase(TITLE)){
			bfTitle = true ;
		}
		if(qName.equalsIgnoreCase(LINK)){
			bflink = true ;
		}
		if(qName.equalsIgnoreCase(PUBDATE)){
			bfPubdate = true ;
		}
		if(qName.equalsIgnoreCase(SOURCE)){
			bfSource = true ;
		}
		if(qName.equalsIgnoreCase(DESCRIPTION)){
			bfDescription = true ;
		}

	}
	public void endElement(String uri, String localName,
			String qName) throws SAXException {
		if(qName.equalsIgnoreCase(ITEM)){
			items.add(item);
			bfItem = false;
		}
		if(qName.equalsIgnoreCase(CHANNEL)){
			setItems(items);
			bfChannel = false ;
		}
	}
	public void characters(char ch[], int start, int length) throws SAXException {
		String resu= new String(ch, start, length);
		if(bfItem){
			bfItem = false;
		}
		if(bfDescription){
			if(item!=null)
			item.setDescription(resu);
			bfDescription = false;
		}
		if(bfTitle){
			if(item!=null)
			item.setTitle(resu);
			bfTitle = false;
		}
		if(bflink){
			if(item!=null)
			item.setLink(resu);
			bflink = false ;
		}
		if(bfPubdate){
			if(item!=null)
			item.setPubDate(resu);
			bfPubdate = false;
		}
		if(bfSource){
			if(item!=null)
			item.setSource(resu);
		}
	}
	/**
	 * @return the items
	 */
	public List<Item> getItems() {
		return items;
	}
	/**
	 * @param items the items to set
	 */
	public void setItems(List<Item> items) {
		this.items = items;
	}
}

///TEST Main 

package xml.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import mapping.rss.Item;

import org.xml.sax.InputSource;

import xml.SearchItemRssHandler;

public class ReadRssXmlFile {
	public static void main(String argv[]) {
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();

			File file = new File("C:\\workspace\src\\rss-test-fr.xml");
			InputStream inputStream= new FileInputStream(file);
			Reader reader = new InputStreamReader(inputStream,"UTF-8");

			InputSource is = new InputSource(reader);
			is.setEncoding("UTF-8");

			SearchItemRssHandler handler = new SearchItemRssHandler();
			saxParser.parse(is, handler);
			List<Item> items  = handler.getItems();
			for (Item item : items) {
				System.out.println(item);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

jan

09

Posted by : admin | On : 9 janvier 2012

Android developper doc :

http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

Simple example :

http://www.botskool.com/geeks/how-create-auto-complete-textbox-android

Cook Book

http://androidcookbook.com/ViewTOC.seam

Run autocomplete android

http://stackoverflow.com/questions/7596508/autocomplete-data-retrived-via-http-request-not-hardcded-when-user-type-in-tex

with filter interface

http://stackoverflow.com/questions/8653260/autocomplete-in-android-not-working-with-dynamic-data

http://www.vingtseptpointsept.fr/2011/08/20/bricolons-un-peu-avec-idref/

1) Create below class in your activity

private class CostomTextWatcher implements TextWatcher {

@Override
public void afterTextChanged(Editable s) {
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() > 0) {
//Make HTTP connection and retrives autocomplete strings from webservice
}

}
}

2) set this above class to edittext using below way

etSearch.addTextChangedListener(new CostomTextWatcher());

You have to make http connection using Background Thread best is AsyncTask

 

 

 
Edit Text recherche dynamique

http://code.google.com/p/android-threshold-edittext/

Autre exemple

http://code.google.com/p/androidsearchexample/downloads/list

Search Suggestion On android
Exemple synthétique :

http://matthias.jimdo.com/blog/

Exemple pratique complet:

http://weblog.plexobject.com/?p=1689

explication globale

http://developer.android.com/guide/topics/search/search-dialog.html

Exemple de recherche Edit Text dynamique

package com.MobileAnarchy.Android.ThresholdEditText;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.TextView;

public class ThresholdEditTextActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final TextView standardInput = (TextView)findViewById(R.id.TextViewStandardTextChange);
final TextView threasholdInput = (TextView)findViewById(R.id.TextViewThreasholdTextChange);

// Get the EditText reference
ThresholdEditText editText = (ThresholdEditText)findViewById(R.id.EditTextInput);

// Subscribe to the standard TextChanged events
editText.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable arg0) {
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
standardInput.setText(arg0);
}

});

// You can manually set the threshold value (default is 500ms)
editText.setThreshold(1000);

// Subscribe to the OnTresholdTextChanged event
editText.setOnThresholdTextChanged(new ThresholdTextChanged() {

@Override
public void onThersholdTextChanged(Editable text) {
Log.d(« Test », « Threshold text changed event was called –  » + text.toString());
threasholdInput.setText(text);
}

});

}
}

package com.MobileAnarchy.Android.ThresholdEditText;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

public class ThresholdEditText extends EditText {

// =========================================
// Private members
// =========================================

private int threshold;
private ThresholdTextChanged thresholdTextChanged;
private Handler handler;
private Runnable invoker;
private boolean disableThresholdOnEmptyInput;

// =========================================
// Constructors
// =========================================

public ThresholdEditText(Context context) {
super(context);
initAttributes(null);
init();
}

public ThresholdEditText (Context context, AttributeSet attrs) {
super(context, attrs);
initAttributes(attrs);
init();
}

// =========================================
// Public properties
// =========================================

/**
* Get the current threshold value
*/
public int getThreshold() {
return threshold;
}

/**
* Set the threshold value (in milliseconds)
* @param threshold Threshold value
*/
public void setThreshold(int threshold) {
this.threshold = threshold;
}

/**
* @return True = the callback will fire immediately when the content of the EditText is emptied
* False = The threshold will be used even on empty input
*/
public boolean isDisableThresholdOnEmptyInput() {
return disableThresholdOnEmptyInput;
}

/**
* @param disableThresholdOnEmptyInput Set to true if you want the callback to fire immediately when the content of the EditText is emptied
*/
public void setDisableThresholdOnEmptyInput(boolean disableThresholdOnEmptyInput) {
this.disableThresholdOnEmptyInput = disableThresholdOnEmptyInput;
}

/**
* Set the callback to the OnThresholdTextChanged event
* @param listener
*/
public void setOnThresholdTextChanged(ThresholdTextChanged listener) {
this.thresholdTextChanged = listener;
}

// =========================================
// Private / Protected methods
// =========================================

/**
* Load properties values from xml layout
*/
private void initAttributes(AttributeSet attrs) {
if (attrs != null) {
String namespace= »http://schemas.android.com/apk/res/com.MobileAnarchy.ThresholdEditText »;

// Load values to local members
this.threshold = attrs.getAttributeIntValue(namespace, « threshold », 500);;
this.disableThresholdOnEmptyInput = attrs.getAttributeBooleanValue(namespace, « disableThresholdOnEmptyInput », true);;
}
else {
// Default threshold value is 0.5 seconds
threshold = 500;

// Default behaviour on emptied text – no threshold
disableThresholdOnEmptyInput = true;
}
}

/**
* Initialize the private members with default values
*/
private void init() {

handler = new Handler();

invoker = new Runnable() {

@Override
public void run() {
invokeCallback();
}

};

this.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable s) { }

@Override
public void beforeTextChanged(CharSequence s, int start, int count,    int after) { }

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

// Remove any existing pending callbacks
handler.removeCallbacks(invoker);

if (s.length() == 0 && disableThresholdOnEmptyInput)
{
// The text is empty, so invoke the callback immediately
invoker.run();
}
else {
// Post a new delayed callback
handler.postDelayed(invoker, threshold);
}
}

});
}

/**
* Invoking the callback on the listener provided (if provided)
*/
private void invokeCallback() {
if (thresholdTextChanged != null) {
thresholdTextChanged.onThersholdTextChanged(this.getText());
}
}

}

package com.MobileAnarchy.Android.ThresholdEditText;

import android.text.Editable;

public interface ThresholdTextChanged {
void onThersholdTextChanged(Editable text);
}