1 Replies - 4013 Views - Last Post: 14 April 2011 - 09:40 AM

#1 jess2705  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-November 10

To get The Spinner Value

Posted 14 April 2011 - 04:37 AM

i am trying to add the data in to spinner from the database and i successfully do that thing but now i want to select the data from the spinner in to textview but it shows the exception .
i tried getSelectedItem() i also tried getItem(position) and its equivalent but it's not work
My code is:
package com.database.login;

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

import android.app.Activity;
import android.database.Cursor;
import android.net.NetworkInfo.State;
import android.net.UrlQuerySanitizer.ValueSanitizer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Registration extends Activity{
   
	public List<String> lsSemMsater = new ArrayList<String>();
	public List<String> lsSemBE = new ArrayList<String>();
	public List<String> lsBranch = new ArrayList<String>();
	public List<String> lsBatch = new ArrayList<String>();
	public List<String> lsSub = new ArrayList<String>();
	public List<String> lsFaculty = new ArrayList<String>();

	public ArrayAdapter<String> aspSemMaster;
	public ArrayAdapter<String> aspSemBE;
	public ArrayAdapter<String> aspBranch;
	public ArrayAdapter<String> aspBatch;
	public ArrayAdapter<String> aspSub;
	public ArrayAdapter<String> aspFaculty;
	

	DatabaseHelper dbHelper;
	Spinner spinBranch;
	Spinner spinBatch;
	Spinner spinFaculty;
	Spinner spinSem;
	Spinner spinSubject;
	
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {

    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        spinBranch=(Spinner)findViewById(R.id.spinBranch);
        spinBatch=(Spinner)findViewById(R.id.spinBatch);
        spinFaculty=(Spinner)findViewById(R.id.spinFaculty);
        spinSem=(Spinner)findViewById(R.id.spinSem);
        spinSubject=(Spinner)findViewById(R.id.spinSub);
        
        
        Calendar c = Calendar.getInstance();
        //fillBranchSpinner();
    	//fillFacultySpinner();
    	//fillSubjectSpinner();
        
        
        //Batch Filling
    	for(int i= 2007;i<=c.get(Calendar.YEAR);i++)
    	{
    		lsBatch.add(""+i);
    	}
    	aspBatch= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,lsBatch);
    	aspBatch.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinBatch.setAdapter(aspBatch);
        
        //Semester Filling of BE
        for(int i= 1;i<=8;i++)
    	{
    		lsSemBE.add(""+i);
    	}
    	aspSemBE= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,lsSemBE);
    	aspSemBE.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //spinSem.setAdapter(aspSemBE);
        
        //Semester Filling of MCA
        for(int i= 1;i<=6;i++)
    	{
    		lsSemMsater.add(""+i);
    	}
    	aspSemMaster= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,lsSemMsater);
    	aspSemMaster.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //spinSem.setAdapter(aspSemMaster);

        
    }
    @Override
    public void onstart()
    {
    	try
    	{
    		super.onstart();
    		
    	
    		
    		dbHelper = new DatabaseHelper(this);
    		
    		//Branch fetched to spinner from Databse
    		Cursor cbranch = dbHelper.getAllBranch();
    		startManagingCursor(cbranch);
    		
    		String[] from = new String[]{DatabaseHelper.BranchName,"_id"};
    		int[] to = new int[]{android.R.id.text1};
    		
    		final SimpleCursorAdapter adp = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, cbranch,from,to);
    		adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		
    		spinBranch.setAdapter(adp);
    		
    		
    		
    		spinBranch.setOnItemSelectedListener(new OnItemSelectedListener() {
    			@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) 
    			{
    				String[] arry=new String[]{dbHelper.BranchID,dbHelper.BranchName};
    				    				
    				
    				//String stradptr = adptr.getItem(0);
    				
    				//String str = adp.getItem(arg2).toString();
    				toastState("Position "+arg2+" \nValue");
					
				}
    			

				@Override
				public void onNothingSelected(AdapterView<?> arg0) 
				{
				
					
				}
				public void toastState(String str)
				{
					//String desc = ""+st.name();
					Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
				}
				
			});
    		
    		//Faculty fetched to spinner from Databse
    		Cursor cfaculty = dbHelper.getAllFaculty("MCA");
    		startManagingCursor(cfaculty);
    		
    		String[] from1 = new String[]{DatabaseHelper.facultyName,"_id"};
    		int[] to1 = new int[]{android.R.id.text1};
    		
    		SimpleCursorAdapter adp1 = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, cfaculty,from1,to1);
    		adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		spinFaculty.setAdapter(adp1);
    		
    		//Subject fetched to spinner from Databse
    		Cursor csub = dbHelper.getAllSubject("MCA", ""+4);
    		startManagingCursor(csub);
    		
    		String[] from3 = new String[]{DatabaseHelper.Subname,"_id"};
    		int[] to3 = new int[]{android.R.id.text1};
    		
    		SimpleCursorAdapter adp3 = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, csub,from3,to3);
    		adp3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		spinSubject.setAdapter(adp3);

    	}
    	catch(Exception e){}
    }
    
    
}




Is This A Good Question/Topic? 0
  • +

Replies To: To get The Spinner Value

#2 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: To get The Spinner Value

Posted 14 April 2011 - 09:40 AM

First, show the part of the code that is necessary to help you. Secondly, if you get any Exceptions, post them.

Also, if you have a Problem, use Google and the Android-Developer page. See this Example-File, it should cover your Problem, too.

Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1