java


How to list both Numbers and Contact in a single row


I'm working on an assignment that requires me to display both the name and number in a single row. I tried creating a custom class, Contact, in order to house the string, Name and Number. The code is pasted below:
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1. The class ContactsContract allows access to various types of data stored in the phone
//Examples: Contacts, Phone settings, Contact groups etc.
//Creates cursor to search contacts
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//Creates array to store string (names from contact)
ArrayAdapter<Contact> list = new ArrayAdapter<Contact>(this,
R.layout.activity_main);
//2. Using the ContactsContract class above, we got a cursor to the data. Now we iterate through it to get all the data
while (cursor.moveToNext()) {
//3. In our example, we get the DISPLAY_NAME but other data elements are also available
String name = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contact newContact = new Contact();
newContact.setName(name);
newContact.setNumber(number);
list.add(newContact);
}
setListAdapter(list);
}
}
And this is my contact class:
public class Contact {
private String name;
private String number;
void setName(String Name){
name = Name;
}
void setNumber (String Number){
number = Number;
}
}
The key to achieve it is by managing a custome adapter for the Contact list.
CustomeAdapter.java
public class CustomeAdapter extends ArrayAdapter {
private final Activity activity;
private final List stocks;
public CustomeAdapter(Activity activity, List objects) {
super(activity, R.layout.row_list_item , objects);
this.activity = activity;
this.stocks = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ManageView mview = null;
if(rowView == null)
{
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.row_list_item, null);
mview= new ManageView();
mview.namev = (TextView) rowView.findViewById(R.id.nameid);
mview.numberv = (TextView) rowView.findViewById(R.id.numberid);
// Cache the view objects
rowView.setTag(mview);
} else {
mview= (ManageView) rowView.getTag();
}
Contact currentContact = stocks.get(position);
mview.namev.setText(currentContact.getName());
mview.numberv.setText(currentContact.getNumber());
return rowView;
}
protected static class ManageView
protected TextView namev;
protected TextView numberv;
}
}
row_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">
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/nameid" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/numberid" />
</LinearLayout>
In your activity
/* Creates array to store string (names from contact)
ArrayAdapter<Contact> list = new ArrayAdapter<Contact>(this,
R.layout.activity_main);*/
ArrayAdapter<Contact> list = new ArrayAdapter<Contact>();
while (cursor.moveToNext()) {
//3. In our example, we get the DISPLAY_NAME but other data elements are also available
String name = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contact newContact = new Contact();
newContact.setName(name);
newContact.setNumber(number);
list.add(newContact);
}
setListAdapter(new CustomeAdapter(this, list));
Listview has a setAdapter method. So just call listview.setAdapter(your_list_adapter_here);
Your list adapter is created from your list
You create an adapter class, as shown here Custom Adapter for List View

Related Links

how to dispose all jframes that running in java?
Proguard issue with external jar library
How can I make changes made to the system partition act immediately?
how can i connect from java to mysql server?
java.lang.ClassNotFoundException: org.pkb.springCalendar.dao.HolidayDaoImpl
How to check whether an object has a field
How to give name to a CLI instead of running via java <classpath>
change size of text of item of list view in .java [closed]
How is it possible to access the instance variable of aa abstract class to a class?
Extract JSON from multi-part HTTP response [duplicate]
How to make a query with 3 ManyToOne components?
Use MasterFlowDetail with XML data
How can I stop the a jMonkeyEngine game?
Else if isn't working
Using an array to store multiple variables from user input
How to change text from another activity?

Categories

HOME
keras
image-processing
push-notification
json.net
view
kalman-filter
setup-deployment
malloc
jsrender
frameworks
yum
directx
bs4
autotools
android-youtube-api
dacpac
ssl-client-authentication
circular-dependency
lombok
progressive-web-apps
saxon
jtextfield
grails3
conemu
dcevm
chromium-embedded
telerik-reporting
microsoft-sync-framework
javacv
fish
pingfederate
minitab
log4js-node
jspm
objectanimator
procdump
flink-streaming
javascriptcore
ioio
titanium-mobile
catel
copying
framemaker
textmate
form-data
android-kernel
gammu
language-concepts
normal-distribution
bytecode-manipulation
ssjs
cookiecutter-django
nomethoderror
segment
apple-news
git-diff
aurelia-validation
feeds
android-cursor
pearson
nbconvert
spring-cache
orthogonal
vtigercrm
xcb
wdf
sts-springsourcetoolsuite
url-masking
mikroc
etsy
player
fuzzy-search
simplewebrtc
whois
rvest
srand
jms2
eclipse-clp
angular-leaflet-directive
ionic
sankey-diagram
composite
notify
umbraco6
cdt
android-nested-fragment
javafx-webengine
websocket4net
php-5.4
wp-query
cloud-connect
multiprocessor
c18
spring-io
web2py-modules
file-locking
android-screen-support
ocunit
javaspaces
errai
onsubmit
windows-phone-7.1.1
transactionscope
window-management
appendto
visitor-statistic
gamequery
ext3
rescale
lzh

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App