Adding items to ListView dynamically

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Adding items to ListView dynamically

Postby megabot » Mon Mar 30, 2009 8:36 pm

Hello,

I have a ListView (ListViewContacts) and another ListView (ContactList).

When I click on a Button in ListViewContacts I get ContactList, when I select/click a contact in ContactList, this contact is added to the list in ListViewContacts.

This scenario above is what I would like to achieve, though I haven't found the right way to do this.

This is the onListItemClick code for ContactList, this starts the Activity ListViewContact and also gives the clicked contact:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. protected void onListItemClick(ListView l, View v, int position, long id) {
  3.  
  4.                 super.onListItemClick(l, v, position, id);
  5.  
  6.                 Intent intent = new Intent(ContactList.this, BlablaActivity.class);
  7.  
  8.                 Uri person = ContentUris.withAppendedId(People.CONTENT_URI, id);
  9.  
  10.                 intent.setData(person);
  11.  
  12.                 startActivity(intent);
  13.  
  14.         }
  15.  
  16.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


This is part of the onCreate() from ListViewContacts, this retrieves the contact from ContactList and adds it to the ListView:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. if(getIntent().getData() != null) {
  3.  
  4.                         Uri data = getIntent().getData();
  5.  
  6.                        
  7.  
  8.                         Cursor addedContactCursor = getContentResolver().query(data, null, null, null, null);
  9.  
  10.                         startManagingCursor(addedContactCursor);
  11.  
  12.                         String[] name = new String[]{People.NAME, People.NUMBER};
  13.  
  14.                         int[] to = new int[]{R.id.nametext, R.id.numbertext};
  15.  
  16.                         adapter = new SimpleCursorAdapter(this,R.layout.contact_row, addedContactCursor, name, to);
  17.  
  18.                         ListView listView =  (ListView) findViewById(R.id.addedcontactslist);
  19.  
  20.                         listView.setAdapter(adapter);
  21.  
  22.                 }
  23.  
  24.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


The problem here is that only 1 contact is added to the list each time, which is the last one that has been clicked on within the ContactList. The code speaks for it self. I assume this happens because a new SimpleCursorAdapter is initiated every time.

What is the best way of adding these contacts, so that the ListViewContacts keeps remembering all contacts and not only the last one that is added? Thanks in advace.
megabot
Freshman
Freshman
 
Posts: 8
Joined: Sun Mar 15, 2009 12:55 pm

Top

Postby megabot » Sat Apr 04, 2009 7:58 pm

Still stuck on this matter. Does anyone have experience with this? Help is much appreciated.
megabot
Freshman
Freshman
 
Posts: 8
Joined: Sun Mar 15, 2009 12:55 pm

Postby megabot » Thu Apr 09, 2009 9:46 am

I was able to fix this problem by adding the selected contact to an ArrayList and from there I added the ArrayList to an ArrayAdapter, which then will show the result(s).
megabot
Freshman
Freshman
 
Posts: 8
Joined: Sun Mar 15, 2009 12:55 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 14 guests