Hi
I am copying my code below to do the above. The problem I have right
now is after I have picked a Contact from the list, I am not able to
add the name to the listview. I get a Force Close at that step. See
point no. A below to understand what I am talking about.
Also, the ListView is built using 'strings' ArrayList which is empty
the very first time. The Menu option 'Add' is then used to pick a
Contact and add the contact Name to the ArrayList which is then
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View.OnLongClickListener;
import android.provider.Contacts.People;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class ListViewTest extends ListActivity {
private Uri iSelectedImageURI = null;
private ListView listView;
private List<String> strings;
// private static final int EDIT_ID = 1;
// private static final int DELETE_ID = 2;
private static final int PICK_CONTACT = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
listView = (ListView) findViewById(R.id.listView);
strings = new ArrayList<String>();
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, strings));
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// For Long Press on ListView items
registerForContextMenu(listView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add_send, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:
Intent contact = new Intent(Intent.ACTION_PICK,
People.CONTENT_URI);
startActivityForResult(contact, PICK_CONTACT);
return true;
}
return true;
}
@SuppressWarnings("unchecked")
@Override
public void onActivityResult(int reqCode, int resultCode, Intent
data){
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null,
null);
if(c.moveToFirst()) { //
-----------------------------------------------------------------
POINT A
String name = c.getString
(c.getColumnIndexOrThrow(People.NAME));
strings.add(name);
// Toast.makeText(this, name, Toast.LENGTH_SHORT).show(); //
----------This works fine, I can read the Contact Name at this point.
((ArrayAdapter<String>)
((HeaderViewListAdapter) listView.getAdapter()).getWrappedAdapter
()).notifyDataSetChanged(); // I think this is where
the Problem comes. I am sure I am doing some stupid mistake.
}
}
break;
}
}
}
list_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Any help would be greatly appreciated. Thanks.
Abhishek
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en