Dynamic Checkboxes: Java Code (Cont.)

final LinkedHashMap<String, String>
    alphabet = new LinkedHashMap<String, String>( );

  • HashMap is hashtable based implementation of the Map interface. The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.

  • LinkedHashMap differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

alphabet.put( "260", ".NET" );
The method put associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.

Set<?> set = alphabet.entrySet( );
Returns a Set view of the mappings contained in this map.

Iterator<?> i = set.iterator( );
Returns an iterator over the elements in this set.

while ( i.hasNext( ) ) {
Returns true if the iteration has more elements.

@SuppressWarnings( "rawtypes" )
Indicates that the named compiler warnings should be suppressed in the annotated element.

Map.Entry me = ( Map.Entry ) i.next( );
A map entry is a key-value pair. The method next returns the next element in the iteration.
DynamicCheckbox/app/src/main/java/com/ecs/wenchen/dynamiccheckbox/MainActivity.java
package com.ecs.wenchen.dynamiccheckbox;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.EditText;
import android.graphics.Color;

public class MainActivity extends Activity {
  LinearLayout linearBox;
  CheckBox     checkBox, checkBox1;

  @Override
  protected void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );

    linearBox = (LinearLayout) findViewById( R.id.linearBox );
    final EditText number = (EditText) findViewById( R.id.number );
    final EditText title  = (EditText) findViewById( R.id.title );
    final Button   button = (Button)   findViewById( R.id.add );

    button.setOnClickListener( new View.OnClickListener( ) {
      public void onClick( View v ) {
        checkBox1 = new CheckBox( getApplicationContext( ) );
        checkBox1.setId( Integer.parseInt( number.getText( ).toString( ) ) );
        checkBox1.setText( title.getText( ).toString( ) );
        checkBox1.setTextColor( Color.BLACK );
        checkBox1.setOnClickListener( getOnClickDoSomething( checkBox1 ) );
        linearBox.addView( checkBox1 );
      }
    } );

    final LinkedHashMap<String, String>
      alphabet = new LinkedHashMap<String, String>( );
    alphabet.put( "260", ".NET" );
    alphabet.put( "370", "Computer Architecture" );

    Set<?> set = alphabet.entrySet( );
    // Get an iterator.
    Iterator<?> i = set.iterator( );
    // Display elements.
    while ( i.hasNext( ) ) {
      @SuppressWarnings( "rawtypes" )
      Map.Entry me = ( Map.Entry ) i.next( );
      checkBox     = new CheckBox( this );
      checkBox.setId( Integer.parseInt( me.getKey( ).toString( ) ) );
      checkBox.setText( me.getValue( ).toString( ) );
      checkBox.setOnClickListener( getOnClickDoSomething( checkBox ) );
      linearBox.addView( checkBox );
    }
  }

  View.OnClickListener getOnClickDoSomething( final Button button ) {
    return new View.OnClickListener( ) {
      public void onClick( View v ) {
        final TextView tvView = (TextView) findViewById( R.id.textView3 );
        tvView.setText( button.getId( ) + ": " + button.getText( ).toString( ) );
      }
    };
  }
}




      The other day, my wife asked me to pass her lipstick,    
      but I accidentally passed her a glue stick.    
      She still isn’t talking to me.