import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class pencarian extends Activity{
EditText edCari;
ListView listview;
Button btnCari;
int jd;
dbHelper helper=null;
String[]arr_id;
String[]arr_longitude;
String[]arr_keterangan;
String[]arr_nama;
String[]arr_alamat;
String[]arr_latitude;
int[]arrGbr;
double[]arJarak;
String[]arr_id2;
String[]arr_longitude2;
String[]arr_keterangan2;
String[]arr_nama2;
String[]arr_alamat2;
String[]arr_latitude2;
int[]arrGbr2;
double[]arJarak2;
String[]arr_id3;
String[]arr_longitude3;
String[]arr_keterangan3;
String[]arr_nama3;
String[]arr_alamat3;
String[]arr_latitude3;
int[]arrGbr3;
double[]arJarak3;
String mylatitude="-6.353516";
String mylongitude="106.832056";
String mylocation="Kampus Politeknik Negeri Jakarta Jalan Lingkar Kampus Raya 16424, Indonesia (default)";
int textlength = 0;
ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<Integer> image_sort = new ArrayList<Integer>();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewcolorcari);
Intent io = this.getIntent();
mylocation=io.getStringExtra("mylocation");
mylatitude=io.getStringExtra("mylatitude");
mylongitude=io.getStringExtra("mylongitude");
helper=new dbHelper(this);
Cursor c=helper.getAll();
c.moveToFirst();
int row = c.getCount();
jd=row;
int i=0;
if (row>0){
arr_nama=new String[jd];
arr_alamat=new String[jd];
arr_latitude=new String[jd];
arr_id=new String[jd];
arr_longitude=new String[jd];
arr_keterangan=new String[jd];
arrGbr=new int[jd];
arJarak=new double[jd];
arr_nama2=new String[jd];
arr_alamat2=new String[jd];
arr_latitude2=new String[jd];
arrGbr2=new int[jd];
arr_longitude2=new String[jd];
arr_id2=new String[jd];
arr_keterangan2=new String[jd];
arJarak2=new double[jd];
double slati=Double.parseDouble(mylatitude);
double slongi=Double.parseDouble(mylongitude);
for(i=0;i<row;i++){
arr_id[i]=helper.getid(c);
arr_nama[i]=helper.getnama(c);
arr_alamat[i]=helper.getalamat(c);
arr_latitude[i]=helper.getlatitude(c);
arr_longitude[i]=helper.getlongitude(c);
arr_keterangan[i]=helper.getketerangan(c);
double jarak=1.0;
try {
double mlati=Double.parseDouble(arr_latitude[i]);
double mlongi=Double.parseDouble(arr_longitude[i]);
jarak=jarak(slati,slongi,mlati,mlongi);
} catch (NumberFormatException e) {
e.printStackTrace();
jarak=1.0;
}
arJarak[i]=jarak;
arrGbr[i]=R.drawable.add;
try{c.moveToNext();}
catch(Exception ee){}
}
c.close();
}
else{
Toast.makeText(pencarian.this," Maaf data belum tersedia....", Toast.LENGTH_LONG).show();
finish();
}
btnCari = (Button) findViewById(R.id.btnCari);
edCari = (EditText) findViewById(R.id.edCari);
listview = (ListView) findViewById(R.id.listCari);
listview.setAdapter(new MyCustomAdapter(arr_nama, arrGbr));
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int p, long id) {
Toast.makeText(getBaseContext(), "Anda telah memilih no: "+p+"="+ arr_nama[p], Toast.LENGTH_LONG).show();
Intent i = new Intent(pencarian.this, detail.class);
i.putExtra("id", arr_id[p]);
i.putExtra("nama", arr_nama[p]);
i.putExtra("keterangan", arr_keterangan[p]);
i.putExtra("latitude", arr_latitude[p]);
i.putExtra("longitude", arr_longitude[p]);
i.putExtra("alamat", arr_alamat[p]);
i.putExtra("jarak", String.valueOf(arJarak[p]));
i.putExtra("mylatitude", mylatitude);
i.putExtra("mylongitude", mylongitude);
i.putExtra("mylocation", mylocation);
startActivity(i);
Toast.makeText(pencarian.this,arr_nama[p]+ " telah dipilih....", Toast.LENGTH_LONG).show();
}
});
btnCari.setOnClickListener(new OnClickListener(){
public void onClick(View v){textlength = edCari.getText().length();
text_sort.clear();
image_sort.clear();
String scari=edCari.getText().toString().toLowerCase();
int ada=0;
for (int i = 0; i < jd; i++){
String snama=arr_nama[i].toLowerCase();
if (textlength <= arr_nama[i].length()){
if (snama.indexOf(scari)>=0){ //if (edCari.getText().toString().equalsIgnoreCase((String) arr_nama[i].subSequence(0, textlength))){//huruf yg awalannya sama
text_sort.add(arr_nama[i]);
image_sort.add(arrGbr[i]);
arr_nama2[ada]=arr_nama[i];
arr_alamat2[ada]=arr_alamat[i];
arr_latitude2[ada]=arr_latitude[i];
arr_id2[ada]=arr_id[i];
arr_longitude2[ada]=arr_longitude[i];
arr_keterangan2[ada]=arr_keterangan[i];
arrGbr2[ada]=arrGbr[i];
arJarak2[ada]=arJarak[i];
ada=ada+1;
}
}
}
arr_nama3=new String[ada];
arr_alamat3=new String[ada];
arr_latitude3=new String[ada];
arr_longitude3=new String[ada];
arr_id3=new String[ada];
arr_keterangan3=new String[ada];
arrGbr3=new int[ada];
arJarak3=new double[ada];
for (int i = 0; i < ada; i++){
arr_nama3[i]=arr_nama2[i];
arr_longitude3[i]=arr_longitude2[i];
arr_id3[i]=arr_id2[i];
arr_keterangan3[i]=arr_keterangan2[i];
arr_alamat3[i]=arr_nama2[i];
arr_latitude3[i]=arr_latitude2[i];
arrGbr3[i]=arrGbr2[i];
arJarak3[i]=arJarak2[i];
}
listview.setAdapter(new MyCustomAdapter(text_sort, image_sort));
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int p, long id) {
Toast.makeText(getBaseContext(), "Anda telah memilih no "+p+"="+ arr_nama3[p], Toast.LENGTH_LONG).show();
Intent i = new Intent(pencarian.this, detail.class);
i.putExtra("id", arr_id3[p]);
i.putExtra("nama", arr_nama3[p]);
i.putExtra("keterangan", arr_keterangan3[p]);
i.putExtra("latitude", arr_latitude3[p]);
i.putExtra("longitude", arr_longitude3[p]);
i.putExtra("alamat", arr_alamat3[p]);
i.putExtra("jarak", String.valueOf(arJarak3[p]));
i.putExtra("mylatitude", mylatitude);
i.putExtra("mylongitude", mylongitude);
i.putExtra("mylocation", mylocation);
startActivity(i);
Toast.makeText(pencarian.this,arr_nama[p]+ " telah dipilih....", Toast.LENGTH_LONG).show();
}
});
}});
}
class MyCustomAdapter extends BaseAdapter{
String[] data_text;
int[] data_image;
MyCustomAdapter(){}
MyCustomAdapter(String[] text, int[] image){
data_text = text;
data_image = image;
}
MyCustomAdapter(ArrayList<String> text, ArrayList<Integer> image){
data_text = new String[text.size()];
data_image = new int[image.size()];
for (int i = 0; i < text.size(); i++) {
data_text[i] = text.get(i);
data_image[i] = image.get(i);
}
}
public int getCount(){return data_text.length;}
public String getItem(int position){return null;}
public long getItemId(int position){return position;}
public View getView(int p, View convertView, ViewGroup parent){
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.listviewcolordetail, parent, false);
TextView textview = (TextView) row.findViewById(R.id.txtCari);
ImageView imageview = (ImageView) row.findViewById(R.id.imgCari);
textview.setText(data_text[p]);
imageview.setImageResource(data_image[p]);
return (row);
}
}
public static double jarak(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
int meterConversion = 1609;
return new Double(dist * meterConversion/1000).doubleValue();
}
}
++++++++++++
listviewcolorcari.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Search"
android:id="@+id/btnCari"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" android:id="@+id/edCari"
android:layout_toLeftOf="@+id/btnCari">
</EditText>
<ListView
android:layout_height="wrap_content"
android:layout_below="@+id/edCari"
android:layout_width="wrap_content"
android:id="@+id/listCari">
</ListView>
</RelativeLayout>
+++++++++++++++++
listviewcolordetail
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:gravity="left|center"
android:layout_width="fill_parent"
android:paddingBottom="5px"
android:background="#999999"
android:paddingTop="5px"
android:paddingLeft="5px">
<ImageView android:id="@+id/imgCari"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="50px"
android:maxHeight="50px"
android:minWidth="50px"
android:minHeight="50px"
>
</ImageView>
<TextView android:id="@+id/txtCari"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px"
android:textStyle="bold"
android:layout_marginLeft="10px"
android:textColor="#ffff00">
</TextView>
</LinearLayout>
Tidak ada komentar:
Posting Komentar