Loading...
package com.example.helloworld;

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

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class HelloActivity extends Activity implements OnItemClickListener, OnClickListener {
List<String> countries = new ArrayList<String>();
ArrayAdapter<String> adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
  
        loadData();
       
        ListView lv = (ListView) findViewById(R.id.mylistview);
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, countries);
        lv.setAdapter(adapter);
       
        lv.setOnItemClickListener(this);
       
        Button btn = (Button) findViewById(R.id.mybutton);
        btn.setOnClickListener(this);
    }
   
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        Uri uri = Uri.parse("http://www.google.com/search?hl=en&q="
                + Uri.encode(countries.get(pos), null) + "&btnI=" + Uri.encode("I'm Feeling Lucky"));
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);             
        startActivity(intent);
}

public void onClick(View view) {
EditText et = (EditText) findViewById(R.id.mytextview);
countries.add(et.getText().toString());
        adapter.notifyDataSetChanged();
        saveData();
}

void saveData() {
SharedPreferences.Editor spe = getPreferences(MODE_PRIVATE).edit();
StringBuilder sb = new StringBuilder();
int i;
for (i = 0; i < countries.size(); i++)
sb.append( ((i == 0) ? "" : ";") + countries.get(i));
spe.putString("countries", sb.toString());
spe.commit();
}

void loadData() {
SharedPreferences sp = getPreferences(MODE_PRIVATE);
String countryList = sp.getString("countries", "Argentina;Brazil;Chile;Mexico");
for (String country : countryList.split(";")) countries.add(country);
}
}