Wednesday, August 15, 2012

Android Adapterview

Android Adapterview
An AdapterView is a view whose children are determined by an Adapter.
See Listview, Gridview, Spinner and Gallery for commonly used subclasses of AdapterView.
Below the example just i create array of values and set that values into ArrayAdapter.
Array of string  values 
Step 1

private static final String[] AndroidVersion = {

"Android2.1", "Android2.2", "Android2.3",

"Android3.0", "Android3.2", "Android4.0",

"Android4.1"

};


Step 2. i created one XML in that having Spinner lauout.
Spinner s = (Spinner) findViewById(R.id.spinner);
Step 3
then i added the array of values to ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

android.R.layout.simple_spinner_item, AndroidVersion);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Step 4.
Then Set the adapter to spinner 
s.setAdapter(adapter);

*Same like listview and Galleryview and Gridview
Step 5. Output -run the application

FULL SOURCE CODE
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
/**
 *
 * @author vijayakumar
 *
 */
public class AndroidMADQAActivity extends Activity implements AdapterView.OnItemSelectedListener {
    private static final String[] AndroidVersion = {
            "Android2.1", "Android2.2", "Android2.3",
            "Android3.0", "Android3.2", "Android4.0",
            "Android4.1"
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Spinner s = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, AndroidVersion);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);
        s.setOnItemSelectedListener(this);
    }
    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
}


Download Source Code 

2 comments:

  1. thank u very much
    u hav done gr8 job
    keep it on

    ReplyDelete
  2. http://androidroadies.blogspot.in/

    ReplyDelete

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...