Custom Listview with search bar also passing string[] and image values to another activity

Introduction 

In my previous post , we implemented a Custom listview with search bar .
Now,we'll see how to implement a search function with string array and image values passing from one activity to another activity.The default text filter associated with theListView. For now,we'll see how we can search the ListView with passing values.

demo



Using the code

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">
    
<EditText 
    android:id="@+id/EditText01"
android:layout_height="40dp"
android:layout_width="fill_parent"
android:hint="search"/>
 <ListView android:id="@+id/android:list"
    android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>


search_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="8dp">

<TextView
android:id="@+id/title"
android:textColor="#000"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_margin="10dp"
    android:layout_height="wrap_content"/>
    
<ImageView 
    android:id="@+id/imageview"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:src="@drawable/ic_launcher"
    android:layout_marginRight="10dp"
    android:contentDescription="@string/app_name"/>

</LinearLayout>


MainActivity.java:
package com.example.listviewimage;

import java.util.ArrayList;
import java.util.Arrays;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.text.Editable;

import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends ListActivity {

private EditText et;
private String[] listview_names = {"India","Bangladesh","China","Indonesia"};
private String[] India = {"Delhi","20,000,000","Blue",};
private String[] Bangladesh = {"Dhaka","10,000,000","Green"};
private String[] China = {"Beijing", "25,000,000","Red"};
private String[] Indonesia = {"Jakarta","500,000","Blue"};
private int[] listview_images   = {R.drawable.india,R.drawable.bangladesh,R.drawable.china,R.drawable.indonesia};
private int[] india   = {R.drawable.india};
private int[] indonesia   = {R.drawable.indonesia};
private int[] bangladesh   = {R.drawable.bangladesh};
private int[] china   = {R.drawable.china};
private ArrayList<String> array_sort;
private ArrayList<Integer> image_sort;
int textlength=0;
private ListView lv;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et  (EditText) findViewById(R.id.EditText01);
lv = (ListView) findViewById(android.R.id.list);
array_sort=new ArrayList<String> (Arrays.asList(listview_names));
image_sort=new ArrayList<Integer>();
for (int index = 0; index < listview_images.length; index++)
    {
image_sort.add(listview_images[index]);
    }
setListAdapter(new bsAdapter(this));

et.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
                         // Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s,
int start, int before, int count)
{
textlength = et.getText().length();
array_sort.clear();
image_sort.clear();
for (int i = 0; i < listview_names.length; i++)
{
if (textlength <= listview_names[i].length())
{
if(listview_names[i].toLowerCase().contains(
et.getText().toString().toLowerCase().trim()))
{
array_sort.add(listview_names[i]);
image_sort.add(listview_images[i]);
}
                                       }
      }
AppendList(array_sort,image_sort);
}
});
lv.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0,
                    View arg1, int position, long arg3)
{
  
if(array_sort.get(position).equals("India"))
{
Intent i=new Intent(getApplicationContext(),Second.class);
i.putExtra("val",India);
i.putExtra("img", india);
startActivity(i);
}
if(array_sort.get(position).equals("Bangladesh"))
{
Intent i=new Intent(getApplicationContext(),Second.class);
i.putExtra("val",Bangladesh);
i.putExtra("img", bangladesh);
startActivity(i);
}
if(array_sort.get(position).equals("China"))
{
Intent i=new Intent(getApplicationContext(),Second.class);
i.putExtra("val",China);
i.putExtra("img", china);
startActivity(i);
}
if(array_sort.get(position).equals("Indonesia"))
{
Intent i=new Intent(getApplicationContext(),Second.class);
i.putExtra("val",Indonesia);
i.putExtra("img", indonesia);
startActivity(i);
}
}
});
}
public void AppendList(ArrayList<String> str,ArrayList<Integer> img)
{
setListAdapter(new bsAdapter(this));
}
public class bsAdapter extends BaseAdapter
        {
        Activity cntx;
        public bsAdapter(Activity context)
        {
            // TODO Auto-generated constructor stub
            this.cntx=context;
        }
        public int getCount()
        {
            // TODO Auto-generated method stub
            return array_sort.size();
        }
        public Object getItem(int position)
        {
            // TODO Auto-generated method stub
            return array_sort.get(position);
        }
        public long getItemId(int position)
        {
            // TODO Auto-generated method stub
            return array_sort.size();
        }
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            View row=null;
            
            LayoutInflater inflater=cntx.getLayoutInflater();
            row=inflater.inflate(R.layout.search_list_item, null);
            
            TextView   tv = (TextView) row.findViewById(R.id.title);
            ImageView im = (ImageView) row.findViewById(R.id.imageview);
            
            tv.setText(array_sort.get(position));
            im.setImageDrawable(getResources().getDrawable(image_sort.get(position)));
            
        return row;
        }
    }
}




Second.java:
package com.example.listviewimage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class Second extends Activity{
EditText ed1,ed2,ed3;
Button b;
ImageView m;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
ed1=(EditText)findViewById(R.id.editText1);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
b=(Button)findViewById(R.id.button1);
m=(ImageView)findViewById(R.id.imageView1);
Intent ii=getIntent();
String[] s=ii.getStringArrayExtra("val");
for (int i = 0; i < s.length; i++) {
ed1.setText(s[0]);
ed2.setText(s[1]);
ed3.setText(s[2]);
}
int[] flag = ii.getIntArrayExtra("img");
m.setImageResource(flag[0]);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent a=new Intent(getApplicationContext(),MainActivity.class);
startActivity(a);
}
});
}

}


Previous
Next Post »

1 comments:

Write comments
Anonymous
AUTHOR
May 19, 2019 at 10:25 PM delete

Hello! This post could not be written any better! Reading through this post reminds me of my good old room mate!
He always kept talking about this. I will forward this post to
him. Fairly certain he will have a good read. Many thanks for sharing!

Reply
avatar