MainActivity.java


package it.freenamesystem.fnssearchapp;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class MainActivity extends Activity {
private AdView adView;
static final int CONFIRM_DIALOG = 0;
AlertDialog alertDialog;
private String url;
EditText edit1;
TextView testo_dialog;

WebView webpage;
LayoutInflater inflater;
View layout;

public void setUrl1(String url){
this.url = url;
}
public String getUrl1(){
return this.url;
}

public void ads(){
String id_editore= "a150c7a234cea5a";
adView = new AdView(this, AdSize.BANNER, id_editore);
RelativeLayout layout = (RelativeLayout) this.findViewById(R.id.main);
layout.addView(adView);
adView.loadAd(new AdRequest());
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edit1 = (EditText) findViewById(R.id.editText1);
edit1.setHint("Ricerca (esempio: FNS)");

inflater= getLayoutInflater();
layout= inflater.inflate(R.layout.dialog_layout,(ViewGroup) findViewById(R.id.dialog_layout));
testo_dialog = (TextView) layout.findViewById(R.id.testo_dialog);

webpage = (WebView) findViewById(R.id.webview);

Button button = (Button) findViewById(R.id.bottone);
button.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
setUrl( edit1.getText().toString() );
setDefaultKeyMode(1);
new AsyncConnection().execute("fns");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit1.getWindowToken(), 0);
}
});
this.ads();
}

public String q;
public void setUrl(String c){
c= c.replaceAll(" ", "+");
this.q=c;
}

public String getUrl(){
return this.q;
}

private class AsyncConnection extends AsyncTask {
private ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item);

protected Object doInBackground(Object... arg0) {
String url = "http://search.freenamesystem.eu/json_service/";

if(q==null)
url += "JSON.php?q=fns";
else
url += "JSON.php?q="+q;

Connetti s = new Connetti();
try {
JSONArray jsonArray = new JSONArray(s.getRisultati(url));
String nome, titolo, descr, reversedName;
for(int i = 0; i < jsonArray.length(); i++) { nome = jsonArray.getJSONObject(i).getString("name"); titolo = jsonArray.getJSONObject(i).getString("titolo"); descr = jsonArray.getJSONObject(i).getString("descrizione"); reversedName = jsonArray.getJSONObject(i).getString("reversedName"); if( titolo == null || titolo.equals(null) || titolo.equals("null") ){ adapter.add( new Risultato(nome, descr, reversedName) ); } else { adapter.add( new Risultato(nome, titolo, descr, reversedName) ); } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object result) { ListView spinRisultati = (ListView) findViewById(R.id.spinner01); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinRisultati.setAdapter(adapter); spinRisultati.setBackgroundColor((int) R.color.Aqua); spinRisultati.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View view,
int position, long id) {

String testo = ((Risultato)parent.getAdapter().getItem(position)).getDescrizione();
if( testo.equalsIgnoreCase("") ) testo = "Nessuna descrizione disponibile\nNo description available";
testo = (Html.fromHtml(testo)).toString();
testo_dialog.setText(testo);

String url = "http://";
url += ((Risultato)parent.getAdapter().getItem(position)).getReversedName();
setUrl1(url);

showDialog(CONFIRM_DIALOG);

}
});

}
}

protected Dialog onCreateDialog(int id) {
Dialog dialog;

ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_fns);

switch(id) {
case CONFIRM_DIALOG:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Aprire la pagina web?");
builder.setView(layout);
builder.setCancelable(false);

builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(), "Apertura di "+getUrl1()+" in corso", Toast.LENGTH_LONG).show();
webpage.getSettings().setJavaScriptEnabled(true);
webpage.loadUrl(getUrl1());
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
}

I commenti sono chiusi.