selecting contacts from list view

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

selecting contacts from list view

Postby kec6227 » Thu Aug 13, 2009 6:03 pm

My program is such that I want to have the contacts in a list on the right and when you select one, it is added to a TextView on the left.

It *almost* works. it seems that the updates are always one click behind.

For example, checking something off the list the first time does nothing, unchecking it or checking something else adds the first thing checked to the text view.

Here is the code:

main.xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="vertical"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    >
  12.  
  13.    
  14.  
  15.     <TextView
  16.  
  17.         android:id="@+id/contact_text"
  18.  
  19.         android:text="Chosen:"
  20.  
  21.                 android:layout_width="150px"
  22.  
  23.         android:layout_height="fill_parent"
  24.  
  25.         android:layout_alignParentLeft="true"
  26.  
  27.         />
  28.  
  29.        
  30.  
  31.         <ListView
  32.  
  33.                 android:id="@+id/contact_list"
  34.  
  35.         android:layout_width="wrap_content"
  36.  
  37.         android:layout_height="fill_parent"
  38.  
  39.         android:layout_alignParentRight="true"
  40.  
  41.         android:layout_toRightOf="@id/contact_text"
  42.  
  43.    />
  44.  
  45.        
  46.  
  47. </RelativeLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class contact extends Activity {
  2.  
  3.  
  4.  
  5.     SparseBooleanArray chosen;
  6.  
  7.     TextView tv;
  8.  
  9.     ListView lv;
  10.  
  11.    
  12.  
  13.     public void onCreate(Bundle savedInstanceState) {
  14.  
  15.         super.onCreate(savedInstanceState);
  16.  
  17.         setContentView(R.layout.main);
  18.  
  19.        
  20.  
  21.         loadContacts();
  22.  
  23.     }
  24.  
  25.    
  26.  
  27.     private void loadContacts(){
  28.  
  29.  
  30.  
  31.         String[] contacts = new String[] {People.NAME};
  32.  
  33.         final String[] cols = new String[] {
  34.  
  35.                 Contacts.People.NAME,
  36.  
  37.                 Contacts.People._ID,
  38.  
  39.                 Contacts.People.NUMBER
  40.  
  41.               };
  42.  
  43.        
  44.  
  45.         final Cursor C = getContentResolver().query(People.CONTENT_URI, cols, null, null, null);
  46.  
  47.         startManagingCursor(C);
  48.  
  49.  
  50.  
  51.         lv = (ListView)findViewById(R.id.contact_list);
  52.  
  53.         ListAdapter mAdapter = new SimpleCursorAdapter(this,
  54.  
  55.                 android.R.layout.simple_list_item_multiple_choice,
  56.  
  57.                 C,
  58.  
  59.                 cols,
  60.  
  61.                 new int[] {android.R.id.text1} );
  62.  
  63.         lv.setAdapter(mAdapter);
  64.  
  65.        
  66.  
  67.         tv = (TextView)findViewById(R.id.contact_text);
  68.  
  69.        
  70.  
  71.         lv.setOnTouchListener(new OnTouchListener() {
  72.  
  73.             public boolean onTouch(View v, MotionEvent event) {
  74.  
  75.                 chosen = lv.getCheckedItemPositions();
  76.  
  77.                 updateAttendance(chosen, C);
  78.  
  79.                 tv.invalidate();
  80.  
  81.                 return false;
  82.  
  83.             }
  84.  
  85.         });
  86.  
  87.         lv.setItemsCanFocus(false);
  88.  
  89.         lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  90.  
  91.  
  92.  
  93.         lv.invalidate();
  94.  
  95.     }
  96.  
  97.    
  98.  
  99.     private void updateAttendance(SparseBooleanArray array, Cursor c) {
  100.  
  101.         tv.setText("Chosen:\n");
  102.  
  103.  
  104.  
  105.         chosen = lv.getCheckedItemPositions();
  106.  
  107.         boolean inLoop = c.moveToFirst();
  108.  
  109.         while(inLoop) {
  110.  
  111.             if(chosen.get(c.getPosition() ) ) {
  112.  
  113.                 tv.append(c.getString(0) + "\n");
  114.  
  115.             }
  116.  
  117.             inLoop = c.moveToNext();
  118.  
  119.         }
  120.  
  121.     }
  122.  
  123. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
kec6227
Developer
Developer
 
Posts: 30
Joined: Fri Jul 24, 2009 7:31 pm

Top

Postby kec6227 » Thu Aug 13, 2009 7:51 pm

Ok so strangely enough I have a solution.

I changed from simple_list_item_multiple_choice to simple_list_item_checked and I can use OnItemClickedListener (which wasn't working before).

Using the same process, it now updates correctly. I still don't understand why onTouchListener doesn't work though.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         lv.setOnItemClickListener(new OnItemClickListener() {
  2.  
  3.             public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
  4.  
  5.                 chosen = lv.getCheckedItemPositions();
  6.  
  7.                 updateAttendance(chosen, C);
  8.  
  9.                 tv.invalidate();
  10.  
  11.             }
  12.  
  13.            
  14.  
  15.         });
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
kec6227
Developer
Developer
 
Posts: 30
Joined: Fri Jul 24, 2009 7:31 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google [Bot] and 9 guests