Header Ads

[Android] Search in listview custom Arrayadapter and listener when click item

Search in listview custom Arrayadapter and listener when click each item in listview it wil open new Activity.
and display text of item you click in listview .

Source code: Download here

Let start with layout file :
activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" >Download source here ! <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:hint="Search here" android:layout_marginLeft="28dp" android:ems="10" /> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/editText1" > </ListView> </RelativeLayout>




single_item.xml  (for each line in listview)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/imageView1"
        android:text="TextView"
        android:paddingTop="10sp"
        android:paddingLeft="5sp"
        android:textSize="18sp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>


sub_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/imageView1"
        android:text="TextView"
        android:paddingTop="10sp"
        android:paddingLeft="5sp"
        android:textSize="18sp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

Now we have two activity and one arrayadapter custome :

CustomeArrayAdapter.java

package com.example.androidtest1;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.sax.RootElement;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomeArrayAdapter extends ArrayAdapter<String>{
    Context context;
    ArrayList<String> arrString;
    private LayoutInflater inflater;

    CustomeArrayAdapter(Activity context,
            ArrayList<String> arrString) {
        super(context, R.layout.single_item, arrString);
        this.context = context;
        this.arrString = arrString;
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public Context getContext() {
        return super.getContext();
    }

    @SuppressWarnings("rawtypes")
    @Override
    public View getView(final int position,View convertView, ViewGroup parent) {
        convertView = inflater.inflate(R.layout.single_item, null);
        TextView tv= (TextView)convertView.findViewById(R.id.tv);
        ImageView img = (ImageView)convertView.findViewById(R.id.imageView1);
        tv.setText(arrString.get(position));
        img.setImageResource(R.drawable.ic_launcher);
        return convertView;

    }
}


MainActivity.java

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.ViewDebug.IntToString;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

    EditText editsearch;
    ListView listView;
    private ArrayList<String> mItems;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        editsearch = (EditText)findViewById(R.id.editText1);
        listView = (ListView)findViewById(R.id.listView1);
        
        mItems = new ArrayList<String>();
        mItems.add("Diary of a Wimpy Kid 6: Cabin Fever");
        mItems.add("Steve Jobs");
        mItems.add("Inheritance (The Inheritance Cycle)");
        mItems.add("11/22/63: A Novel");
        mItems.add("The Hunger Games");
        mItems.add("The LEGO Ideas Book");
        mItems.add("Explosive Eighteen: A Stephanie Plum Novel");
        mItems.add("Catching Fire (The Second Book of the Hunger Games)");
        mItems.add("Elder Scrolls V: Skyrim: Prima Official Game Guide");
        mItems.add("Death Comes to Pemberley");
        mItems.add("Diary of a Wimpy Kid 6: Cabin Fever");
        mItems.add("Steve Jobs");
        mItems.add("Inheritance (The Inheritance Cycle)");
        mItems.add("11/22/63: A Novel");
        mItems.add("The Hunger Games");
        mItems.add("The LEGO Ideas Book");
        mItems.add("Explosive Eighteen: A Stephanie Plum Novel");
        mItems.add("Catching Fire (The Second Book of the Hunger Games)");
        mItems.add("Elder Scrolls V: Skyrim: Prima Official Game Guide");
        mItems.add("Death Comes to Pemberley");

        listView.setAdapter(new CustomeArrayAdapter(MainActivity.this, mItems));
        
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()  {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                
                Intent intent = new Intent(MainActivity.this, SubActivity.class);
                intent.putExtra("thanhcs",mItems.get(position));
                startActivity(intent); //when you click,
                   //open new activity and intent text
            }
            
            
        });
        
        
        editsearch.addTextChangedListener(new TextWatcher() { //edit search
            //Event when changed word on EditTex
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                    ArrayList<String> temp = new ArrayList<String>();
                    int textlength = editsearch.getText().length();
                    temp.clear();
                    for (int i = 0; i < mItems.size(); i++)
                    {
                        if (textlength <= mItems.get(i).length())
                        {
                            if(editsearch.getText().toString().equalsIgnoreCase(
                                    (String)
                                    mItems.get(i).subSequence(0,
                                            textlength)))
                            {
                                temp.add(mItems.get(i));
                            }
                        }
                    }
          listView.setAdapter(new CustomeArrayAdapter(MainActivity.this, temp));
                }

            
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
                
            }
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                
            }
        });
    
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


Sub_Activity.java

package com.example.androidtest1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.example.androidtest1.*;
public class SubActivity extends Activity{ 
    
    TextView tv;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setContentView(R.layout.sub_activity);
        super.onCreate(savedInstanceState);        
        tv =(TextView)findViewById(R.id.tv);
        Intent intent = getIntent();
        String text = intent.getExtras().getString("thanhcs");
        tv.setText(""+text);
    }
}



Source code: Download here





No comments:

Powered by Blogger.