package com.blobinfo.contact;
import java.util.*;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View.OnKeyListener;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.database.Cursor;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ContactList extends Activity {
private EditText contactName;
private Cursor CursorList;
private ListView ContactsListView;
private String rowID;
int count;
private MyDatabase db;
private HashMap<Integer, String> getRowID=new HashMap<Integer, String>();
private List<HashMap<String, String>> listContact=new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_list);
contactName=(EditText)findViewById(R.id.search_contact);
ContactsListView=(ListView)findViewById(R.id.lstViewContacts);
db = new MyDatabase(this);
CursorList = db.getEmployees();
count=0;
if (CursorList.moveToFirst())
{
do
{
HashMap<String, String> contactDet=new HashMap<String, String>();
String rowID=CursorList.getString(0).toString();
String contactFirstName=CursorList.getString(1).toString();
String contactLastName=CursorList.getString(2).toString();
contactDet.put("name",""+contactFirstName+" "+contactLastName);
listContact.add(contactDet);
getRowID.put(count, rowID);
count++;
}while (CursorList.moveToNext());
}
String[] itemControl = {"name"};
int[] itemLayout={R.id.name};
listContact=sortContact(listContact);
SimpleAdapter adapter = new SimpleAdapter(this.getBaseContext(),listContact,R.layout.list_contact_layout,itemControl,itemLayout);
ContactsListView.setAdapter(adapter);
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view==null)
{
LayoutInflater inflater=getLayoutInflater();
view=inflater.inflate(R.layout.list_contact_layout, parent, false);
TextView textView=(TextView)view.findViewById(R.id.name);
Button cal = (Button) findViewById(R.id.callb);
view.setTag(new Holder(cal,textView));
}
Holder h = (Holder) view.getTag();
h.textView.setText([position]);
return ;
}
}
try
{
contactName.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event) {
SimpleAdapter adapter=searchViewAdapter(contactName.getText().toString(),CursorList);
ContactsListView.setAdapter(null);
ContactsListView.setAdapter(adapter);
return false;
}
});
}
catch(Exception e)
{
Log.e("Phonebook_TAG","I got an error while searching",e);
}
db.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
public SimpleAdapter searchViewAdapter(String search,Cursor crList)
{
crList = db.getsearch();
count=0;
listContact=new ArrayList<HashMap<String,String>>();
if (crList.moveToFirst())
{
do
{
HashMap<String, String> contactDet=new HashMap<String, String>();
String rowID=crList.getString(0).toString();
String fullName=crList.getString(1).toString()+" "+crList.getString(2).toString();
String emailAdd=crList.getString(3).toString();
String phoneNumber=crList.getString(4).toString();
String phoneNumber1=crList.getString(5).toString();
if(fullName.toLowerCase().contains(search.toLowerCase()) && search!="")
{
contactDet.put("name",fullName);
listContact.add(contactDet);
getRowID.put(count, rowID);
count++;
}
else if(phoneNumber.toLowerCase().contains(search.toLowerCase()) && search!="")
{
contactDet.put("name",fullName);
listContact.add(contactDet);
getRowID.put(count, rowID);
count++;
}
else if(emailAdd.toLowerCase().contains(search.toLowerCase()) && search!="")
{
contactDet.put("name",fullName);
listContact.add(contactDet);
getRowID.put(count, rowID);
count++;
}
else if(search=="")
{
contactDet.put("name",fullName);
listContact.add(contactDet);
getRowID.put(count, rowID);
count++;
}
}while (crList.moveToNext());
}
String[] itemControl = {"name"};
int[] itemLayout={R.id.name};
listContact=sortContact(listContact);
SimpleAdapter adapter = new SimpleAdapter(this.getBaseContext(),listContact,R.layout.list_contact_layout,itemControl,itemLayout);
return adapter;
}
public List<HashMap<String, String>> sortContact(List<HashMap<String, String>> contacts)
{
List<String> lst=new ArrayList<String>();
List<HashMap<String, String>> sortContacts=new ArrayList<HashMap<String,String>>();
for(int i=0;i<contacts.size();i++)
{
lst.add(contacts.get(i).get("name")+","+getRowID.get(i));
}
Collections.sort(lst);
getRowID=new HashMap<Integer, String>();
for(int i=0;i<lst.size();i++)
{
HashMap<String, String> hashContacts=new HashMap<String, String>();
String splitData[]=lst.get(i).split(",");
hashContacts.put("name",splitData[0]);
sortContacts.add(hashContacts);
getRowID.put(i, splitData[splitData.length-1]);
}
return sortContacts;
}
}
i need your help
In the fastest time
|