Code example for ListView

Methods: getAdapter, getSelectedItemPosition

0
		}); 
 
		bInstance = (Button) findViewById(R.id.newsreader_choosecats_del);
		bInstance.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if (mMembersList.getSelectedItemPosition() >= 0) {
					deleteMember(mMembersList.getSelectedItemPosition());
				} else { 
					Toast.makeText(ChooseCategoriesDialog.this,
							"no contact selected", 1000); 
				} 
			} 
		}); 
 
		bInstance = (Button) findViewById(R.id.newsreader_choosecats_save);
		bInstance.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				ChooseCategoriesDialog.this.save();
			} 
		}); 
 
		init(); 
	} 
 
	public void init() { 
 
		mCategories = managedQuery(News.Categories.CONTENT_URI,
		// new String[]{android.provider.Contacts.PeopleColumns.NAME}, 
				null, null, null, null); 
		// managedQuery(Uri uri, String[] projection, String selection, String[] 
		// selectionArgs, String sortOrder) 
		SimpleCursorAdapter sca = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_1, mCategories,
				new String[] { News.Categories.NAME },
				new int[] { android.R.id.text1 });
 
		mCategoryList.setAdapter(sca);
 
		Bundle b = getIntent().getExtras();
 
		String s = b.getString(CURRENT_CATS);
		String[] ccats = s.split(News.CAT_DELIMITER);
		ArrayAdapter aa = new ArrayAdapter(this,
				android.R.layout.simple_list_item_1);
		for (int i = 0; i < ccats.length; i++) {
			aa.add(ccats[i]);
		} 
 
		mMembersList.setAdapter(aa);
		/* 
		 * if (mState == STATE_CREATE) { long now = System.currentTimeMillis(); 
		 * String snow = Long.toString(now); mName.setText(snow); ContentValues 
		 * cv = new ContentValues(); cv.put(JamSessions.Session.NAME, snow); 
		 * mSessionUri = JamSessions.insert(JamSessions.Session.CONTENT_URI, 
		 * cv); cSession = managedQuery(mSessionUri, 
		 * JamSessions.Session.PROJECTION, null, null); Log.d(_TAG, "uri>>" + 
		 * mSessionUri.toString()); 
		 *  
		 * } else if (mState == STATE_EDIT) { mSessionUri = 
		 * getIntent().getData(); cSession = managedQuery(mSessionUri, 
		 * JamSessions.Session.PROJECTION, null, null); cSession.first(); 
		 * mName.setText(cSession.getString(cSession 
		 * .getColumnIndex(JamSessions.Session.NAME))); } 
		 *  
		 * cDetails = managedQuery(JamSessions.SessionDetails.CONTENT_URI, 
		 * JamSessions.SessionDetails.PROJECTION, 
		 * JamSessions.SessionDetails.SESSION_ID + " =" + 
		 * mSessionUri.getLastPathSegment(), null); 
		 *  
		 * sca = new SimpleCursorAdapter(this, 
		 * android.R.layout.simple_list_item_1, cDetails, new String[] { 
		 * JamSessions.SessionDetails.CONTACT_NAME }, new int[] { 
		 * android.R.id.text1 }); mMembersList.setAdapter(sca); 
		 */ 
	} 
 
	private void addContactAsMember(int pos) {
 
		mCategories.moveToPosition(pos);
 
		String name = mCategories.getString(mCategories
				.getColumnIndex(News.Categories.NAME));
		ArrayAdapter adapter = (ArrayAdapter) mMembersList.getAdapter();
		adapter.add(name);
 
		save(); 
 
	} 
 
	private void deleteMember(int pos) {
 
		ArrayAdapter adapter = (ArrayAdapter) mMembersList.getAdapter();
		adapter.remove(adapter.getItem(pos));
		/* 
		 * cDetails.moveTo(pos); cDetails.deleteRow(); 
		 */ 
		save();