Selasa, 15 Agustus 2017

Pencarian Array



package com.sttnf.apptanamanobat;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
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 pencarianTanaman extends Activity{
EditText edCari;
ListView listview;
Button btnCari;

int jd;
String[]arrNama;
String[]arrDes;
String[]arrKegunaan;
int[]arrGbr;

String[]arrNama2;
String[]arrDes2;
String[]arrKegunaan2;
int[]arrGbr2;

String[]arrNama3;
String[]arrDes3;
String[]arrKegunaan3;
int[]arrGbr3;

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.listviewcari);

Bundle extras = getIntent().getExtras();
arrNama= extras.getStringArray("arrNama");
arrDes =extras.getStringArray("arrDes");
arrKegunaan= extras.getStringArray("arrKegunaan");
arrGbr= extras.getIntArray("arrGbr");

jd=arrNama.length;

arrNama2=new String[jd];
arrDes2=new String[jd];
arrKegunaan2=new String[jd];
arrGbr2=new int[jd];

btnCari = (Button) findViewById(R.id.btnCari);
edCari = (EditText) findViewById(R.id.edCari);
listview = (ListView) findViewById(R.id.listCari);

listview.setAdapter(new MyCustomAdapter(arrNama, 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+"="+ arrNama[p], Toast.LENGTH_LONG).show();
        Intent i = new Intent(pencarianTanaman.this, Detail.class);
i.putExtra("nama", arrNama[p]);
i.putExtra("kegunaan", arrKegunaan[p]);
i.putExtra("deskripsi", arrDes[p]);
i.putExtra("gambar", arrGbr[p]);
startActivity(i);

  }
  });

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=arrNama[i].toLowerCase();
String skegunaan=arrKegunaan[i].toLowerCase();
String sdes=arrDes[i].toLowerCase();
if (textlength <= arrNama[i].length()){
if (snama.indexOf(scari)>=0 || skegunaan.indexOf(scari)>=0|| sdes.indexOf(scari)>=0){
text_sort.add(arrNama[i]);
image_sort.add(arrGbr[i]);
arrNama2[ada]=arrNama[i];
arrDes2[ada]=arrDes[i];
arrKegunaan2[ada]=arrKegunaan[i];
arrGbr2[ada]=arrGbr[i];
ada=ada+1;
}
  }
}

arrNama3=new String[ada];
arrDes3=new String[ada];
arrKegunaan3=new String[ada];
arrGbr3=new int[ada];

for (int i = 0; i < ada; i++){
arrNama3[i]=arrNama2[i];
arrDes3[i]=arrNama2[i];
arrKegunaan3[i]=arrKegunaan2[i];
arrGbr3[i]=arrGbr2[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+"="+ arrNama3[p], Toast.LENGTH_LONG).show();
        Intent i = new Intent(pencarianTanaman.this, Detail.class);
i.putExtra("nama", arrNama3[p]);
i.putExtra("kegunaan", arrKegunaan3[p]);
i.putExtra("deskripsi", arrDes3[p]);
i.putExtra("gambar", arrGbr3[p]);
startActivity(i);
  }
       });
}});
}

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.listviewdetail, 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);
}
}

}

++++
listviewcari.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>




listviewdetail.xml
<?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