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

Java: How to save an input choice in a text file
Gujarati text in Java String
Running Java GUI apps on Cloud 9
Trigger java install using wix
Comprehension of a for loop
Search and remove item from array
Pulling files from remote system using java Runtime.exec on mac
InvocationTargetException while sendind Parcelable custom objects from one activity to another
How to center gridview?
mongodb distinct with where clause in java
how to compare every item from array to all items from another arraylist
Why does Jetty standalone JAR download itself? (packaged with Maven)
Single-page application with jsp mvc
simple date format with leading space instead of zero
JMS message failed to retrieve from a queue
How can I set pageToken to get item lists from Google Cloud Storage via Java SDK?

Categories

HOME
maven
cakephp
openlayers
listview
zeromq
tinymce
homebrew
webstorm
rsync
jira
yahoo-oauth
amazon-ecs
frameworks
ebean
gitpitch
spring-xd
wheelnav.js
resize
telephony
pc
connection-string
pythonanywhere
shared-hosting
immutable.js
jquery-ajaxq
spring-mybatis
wtx
dbclient
vision
objectanimator
c++-amp
r-raster
functor
form-data
gammu
google-api-nodejs-client
mmenu
environment-modules
devextreme
libraries
unspecified
lumberjack
particles.js
firebase-admin
vao
acoustics
angular2-meteor
skeleton-css-boilerplate
taffy
hendrix
mplayer
setter
libusb-win32
akka-cluster
mcafee
crosswalk-runtime
root-framework
instant
infix-notation
file-writing
measures
python-stackless
suffix-tree
historian
lib.web.mvc
marching-cubes
atk4
security-testing
splash
operation
cisco-ios
angular-local-storage
npapi
offloading
nstableviewcell
nsbutton
flask-cors
valuechangelistener
undefined-reference
hyprlinkr
srs
page-layout
sitemesh
spring-io
quartz-graphics
ril
batterylevel
pushbackinputstream
jquery-mobile-dialog
mqx
ocunit
google-email-migration
dice
first-responder
runas
word-processor
getmessage
parametric-equations
assembly-loading
custom-backend
eclipse-templates
fireworks
stage
blackberry-playbook
horizontal-accordion
jmock
lpeg
yslow
filtered-index
mirah
nintendo-ds
kdbg
pascal-fc
ajax-forms

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App