vendredi 29 avril 2016

Set two spinner from database, second spinner according to first one

There are two spinner named sp_name and sp_vadya. I am not able to set the second spinner value according to the first spinner which comes from database. Here is my code, more detail is in java code what i want to do. Help to set spinner.

Thanks in advance.

Ghosh.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://ift.tt/nIICcg"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="#ddeeff"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" >

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="Name"
             android:textAppearance="?android:attr/textAppearanceLarge" />

            <Spinner
                android:id="@+id/sp_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tv_vadya"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="Vadya"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <Spinner
                android:id="@+id/sp_vadya"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tv_rachna"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="Rachna"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>
    </LinearLayout>

Ghosh.java

public class Ghosh extends Activity {

                Button btn_deleteghosh, btn_updateghosh;
                Spinner sp_name, sp_vadya;
                EditText edt_rachna;
                ArrayList<String> arlst_name, arlst_vadya, arlst_rachna, arlst_sid;
                ArrayAdapter<String> arad_vadya, arad_rachna, arad_name;
                Database database;

                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.ghosh);

                    btn_deleteghosh = (Button) findViewById(R.id.btn_deleteghosh);
                    btn_updateghosh = (Button) findViewById(R.id.btn_updateghosh);

                    edt_rachna = (EditText) findViewById(R.id.edt_rachna);

                    sp_name = (Spinner) findViewById(R.id.sp_name);
                    sp_vadya = (Spinner) findViewById(R.id.sp_vadya);

                    // ===========For VADYA============
                    arlst_vadya = new ArrayList<String>();

                    arlst_vadya.add("VANSHI");
                    arlst_vadya.add("AANAK");
                    arlst_vadya.add("SANKH");
                    arlst_vadya.add("PARNA");
                    arlst_vadya.add("TRIBHUJ");
                    arlst_vadya.add("SEXOPHONE");
                    arlst_vadya.add("None");

                    ArrayAdapter<String> arad_vadya = new ArrayAdapter<String>(Ghosh.this,
                            android.R.layout.simple_spinner_item, arlst_vadya);

                    sp_vadya.setAdapter(arad_vadya);

                    loaddata();



     *//here load data function will be for the fetching the data from the database

            // if in database there r 3 records
            //1) name: abc     vadya:TRIBHUJ
            //2) name:pqr      vadya:None
            //3) name:xyz      Vadya:SANKH

            // now if i will select name pqr then sp_vadya will have to set automatically by none. That is my exact problem.*


                    sp_name.setOnItemSelectedListener(new OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view,
                                int pos, long id) {
                            // TODO Auto-generated method stub

                             sp_vadya.setSelection(arlst_vadya.indexOf(pos));

                            edt_rachna.setText(arlst_rachna.get(pos).toString());

                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) {
                            // TODO Auto-generated method stub

                        }
                    });

                    btn_updateghosh.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                            Intent i = new Intent(Ghosh.this, UpdateGhoshActivity.class);
                            startActivity(i);

                        }
                    });

                    btn_deleteghosh.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                        }
                    });

                }

                public void loaddata() {
                    arlst_name = new ArrayList<String>();
                    arlst_sid = new ArrayList<String>();
                    arlst_vadya = new ArrayList<String>();
                    arlst_rachna = new ArrayList<String>();

                    try {

                        database = new Database(Ghosh.this);
                        database.open();

                        Cursor c = database.getGhoshdetail();

                        if (c.moveToFirst()) {
                            do {
                                arlst_sid.add(c.getString(0));
                                arlst_vadya.add(c.getString(1));
                                arlst_rachna.add(c.getString(2));
                                arlst_name.add(c.getString(3));

                                Log.d("sid in ghosh", c.getString(0));
                                Log.d("vadya in ghosh", c.getString(1));
                                Log.d("rachna in ghosh", c.getString(2));
                                Log.d("name in ghosh", c.getString(3));

                            } while (c.moveToNext());
                            if (c != null && c.isClosed()) {
                                c.close();
                            }

                            ArrayAdapter<String> arad_name = new ArrayAdapter<String>(
                                    Ghosh.this, android.R.layout.simple_spinner_item,
                                    arlst_name);
                            sp_name.setAdapter(arad_name);

                            c.close();
                            database.close();
                        }

                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }

            }

Aucun commentaire:

Enregistrer un commentaire