Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to handel one textview and two buttons in each item in listview Contactlistview. But I do not know how the solution to this.

ex:
adel younes button button
samir jem3a button button
.
.
.
etc
I'm trying to create an application and I want to put next to the contactname two buttons , one for call and one for sms, and i want to handle textview and the two buttons in listview.
and this the class Who needs an amendment it.


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.View.OnKeyListener;
import android.view.Menu;
import android.view.View;
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();
		
		//CursorList = testing.getAllContacts();
		
		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);

		//To view the contact details
		try
		{
			ContactsListView.setOnItemClickListener(new OnItemClickListener() 
			{
				@SuppressWarnings("rawtypes")
				public void onItemClick(AdapterView parent, View v, int position, long id)
				{

					Intent contactDetails = new Intent(ContactList.this, ContactDetails.class);
					contactDetails.putExtra("posit",getRowID.get(position));
					finish();
					startActivity(contactDetails);

				}
			});
		}
		catch(Exception e)
		{
			Log.e("Phonebook_TAG","I got an error on clicking the contact name",e);
		}

		//Will be called when we search for a contact
		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) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_contact_list, menu);
		return true;
	}

	//to Add contact
	//public void addContact(View view)
	//{
		//finish();
		//Intent addContent=new Intent(this,AddContact.class);
		//startActivity(addContent);

	//}

	//Return updated search list view adapter after search
	public SimpleAdapter searchViewAdapter(String search,Cursor crList)
	{
		crList = db.getsearch();
		//crList = db.getContacts(count);
		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;
	}

	//To sort the contacts
	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;
	}
	
	//@Override
	//public void onBackPressed() {

		//finish();

		//testing.close();
		//db.close();
		
	//}

}

Sent you a link for full applications, which I did in order to understand the issue better and I hope you help me as soon as possible and thank you.



https://github.com/adelyounes/projetSMB215/tree/master/PhoneBook[^]
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900