Shopper app | CustomArrayAdapter in SelectParticipants-Activity #10

Hello,

welcome to the next post. Today, I going to discuss how to use dialog boxes on the Android platform. As shown on the screen below, a user can select participants of his group which should be assigned to the current purchase. This is a typical example of user feedback where users can make choices between several alternatives.

The Select-Participant box looks like a “popup box” or a dialog box. Well, in fact android provides you with possibilities to create dialogs . These are simple popups with no complex content like a a dialogbox which is prompting you for a information. If the content gets more complex you can use a common activity which looks like a dialog. :) Android provides several themes for activities and other stuff. So, to tell android that a common activity should look like a dialogbox you have to extend the respective activity tag in the AndroidManifest.xml file with the attribute android:theme

 	<activity android:name=".activities.SelectParticipantsActivity" android:theme="@android:style/Theme.Dialog"/> 

From this point on this activity will be shown as a DialogBox :)

Now let’s take a look at the sourcecode of the SelectParticipantsActivity class, because it covers an interesting topic. In the last discussion we took a look at
android listviews (especially the ListActivity) which was constructed via xml layout files and got its content from a socalled ArrayAdapter. If the listrows contains
more complex content than just a String it’s recommended to implement your own custom ArrayAdapter. Writing your own Arrayadapter is quite easy and gives you the
opertunity to provide more complexe userinterface for each row and interact with it. This is the current sourcecode of the SelectParticipantsActivity class with the inner ArrayAdapter:

package com.berndwarmuth.client.shopper.activities;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.berndwarmuth.client.shopper.R;

public class SelectParticipantsActivity extends ListActivity {

	private final String PREFS_NAME = "shopper_prefs";
	private Button mBtnOK;
	private SelectParticipantsArrayAdapter spaa;
	private List<String> selectedParticipants = new ArrayList<String>();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_participants);
		this.mBtnOK = (Button) findViewById(R.id.btn_ok_participants);

		this.spaa = new SelectParticipantsArrayAdapter(this,
				new ArrayList<String>());
		this.setListAdapter(spaa);

		this.mBtnOK.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {

				// RETURN TO PREV ACTIVITY & SUBMIT TO SERVER
				Intent i = new Intent();
				i.putStringArrayListExtra("selected_participants",
						(ArrayList<String>) selectedParticipants);
				setResult(RESULT_OK, i);
				finish();
			}
		});
	}

	// ARRAY ADAPTER
	private class SelectParticipantsArrayAdapter extends ArrayAdapter<String> {

		private Activity context;
		private List<String> participants;

		public SelectParticipantsArrayAdapter(Activity context,
				List<String> participants) {
			super(context, R.layout.select_paricipants_row, participants);

			this.context = context;
			this.participants = participants;

			// READ OUT CURRENT JOINED GROUP MEMBERNAMES
			SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
			String groupmembers = settings.getString("group_members", "no");
			String[] members = groupmembers.split("-");

			// ADD MEMBERNAMES TO PARTICIPANTS AND SELECTED PARTICIPANTS LISTS
			for (int i = 0; i < members.length; i++) {
				this.participants.add(members[i]);
				SelectParticipantsActivity.this.selectedParticipants
						.add(members[i]);
			}
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = context.getLayoutInflater();
			View rowView = inflater.inflate(R.layout.select_paricipants_row,
					null, true);

			TextView lblParticipant = (TextView) rowView
					.findViewById(R.id.lbl_participant);
			CheckBox cbParticipant = (CheckBox) rowView
					.findViewById(R.id.cb_participant);

			lblParticipant.setText(this.participants.get(position));
			cbParticipant.setTag(position);

			cbParticipant
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							int pos = (Integer) buttonView.getTag();
							if (isChecked) {
								if (!selectedParticipants.contains(participants
										.get(pos))) {
									selectedParticipants.add(participants
											.get(pos));
								}
							} else {
								if (selectedParticipants.contains(participants
										.get(pos))) {
									selectedParticipants.remove(participants
											.get(pos));
								}
							}

						}
					});
			return rowView;
		}

		@Override
		public String getItem(int position) {
			// TODO Auto-generated method stub
			return super.getItem(position);
		}

	}

}

The ArrayAdapter manages the listview backed by an array of arbitrary objects. The most interesting Method of our Adapter is the getView() method which is used to construct a more complex userinterface for each row of your list. Overriding this method gives you the opportunity to create Images, Textviews and whatever you want for each row. In our example we use the Views defined in our Laout xml for the rows, which contains Textviews, Checkboxes and TextEdit Elements.

cya

, , ,
  • Delicious
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • Twitter

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>