lundi 1 juin 2015

Layout w/ Spinner, after saving spinner value on Sqlite, how to display it as a default value use the same layout & cursor based

Layout with Spinner, after saving the spinner value on the Sqlite, how to display/view it as a default value using the same layout I'm using a cursor based approach.

In my code, the clubspinner is saved ok dan I have checked the value is changed when view the database, but when viewing again using the same layout with spinner , it is still displaying the 'first' value of spinner as a default.

In my code the default value of spinner is 'persija'. I have chosen the spinner value and changed it to 'bolton' successfully.

But when restart the application, it is still took the default 'persija' value.

I'm new on android, and have a basic question of spinner : using "(android.R.layout.simple_spinner_dropdown_item), do we have the capability of displaying the 'saved' value from sqlite database instead of just the first default value of spinner ?

DetailForm.java :

package com.lm.mobilesalesdb12;

import android.view.View.OnClickListener;  
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.content.Intent;
import java.util.ArrayList;
import java.util.List;

public class DetailForm extends Activity {

    Spinner clubspinner;
    Button btn_save;

    EditText outletno=null;
    EditText name=null;
    EditText joindate=null;
    EditText poscode=null;
    EditText ctctper=null;
    EditText phone=null;
    EditText hp=null;
    EditText area=null;
    EditText subarea=null;
    String   club=null; 

    AlmagHelper helper=null;
    String almagId=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_form);

        helper=new AlmagHelper(this);

        outletno=(EditText)findViewById(R.id.outletno);
        name=(EditText)findViewById(R.id.name);
        joindate=(EditText)findViewById(R.id.joindate);
        poscode=(EditText)findViewById(R.id.poscode);
        ctctper=(EditText)findViewById(R.id.ctctper);
        phone=(EditText)findViewById(R.id.phone);
        hp=(EditText)findViewById(R.id.hp);
        area=(EditText)findViewById(R.id.area);
        subarea=(EditText)findViewById(R.id.subarea);

        clubspinner=(Spinner)findViewById(R.id.clubspinner);

        club = clubspinner.getSelectedItem().toString();

        Button save=(Button)findViewById(R.id.btn_save);

        addListenerOnButton();

        save.setOnClickListener(onSave);

        addListenerOnSpinnerItemSelection();


        almagId=getIntent().getStringExtra(Dboutlet.ID_EXTRA);

        if (almagId!=null) {
            load();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        helper.close();
    }   


    private void load() {

        Cursor c=helper.getById(almagId);

        c.moveToFirst();

        outletno.setText(helper.getOutletno(c));
        name.setText(helper.getName(c));
        joindate.setText(helper.getJoindate(c));

        club = clubspinner.getSelectedItem().toString();

        poscode.setText(helper.getPoscode(c));
        ctctper.setText(helper.getCtctper(c));
        phone.setText(helper.getPhone(c));
        hp.setText(helper.getHp(c));
        area.setText(helper.getClub(c));
        subarea.setText(helper.getSubarea(c));

        clubspinner=(Spinner)findViewById(R.id.clubspinner);

        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.club1_arrays,android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        clubspinner.setAdapter(adapter);

        c.close();

    }

    public void addListenerOnSpinnerItemSelection() {
                     Spinner clubspinner = (Spinner) findViewById(R.id.clubspinner);
                     clubspinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
                      }

    //get the selected dropdown list value

    public void addListenerOnButton() {

        clubspinner = (Spinner) findViewById(R.id.clubspinner);

        btn_save = (Button) findViewById(R.id.btn_save);

        btn_save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(DetailForm.this,
                        "On Button Click : " + 
                        "\n" + String.valueOf(clubspinner.getSelectedItem()) ,
                        Toast.LENGTH_LONG).show();
            }

        });

    }

    private final View.OnClickListener onSave=new View.OnClickListener() {
        public void onClick(View v) {

            if (almagId==null) {

                club = clubspinner.getSelectedItem().toString();

                helper.insert(outletno.getText().toString(),
                                            name.getText().toString(), 
                                            joindate.getText().toString(),
                                            club,
                                            poscode.getText().toString(), 
                                            ctctper.getText().toString(),
                                            phone.getText().toString(),
                                            hp.getText().toString(), 
                                            area.getText().toString(),
                                            subarea.getText().toString());

            }
            else {

                club = clubspinner.getSelectedItem().toString(); 

                helper.update(almagId, outletno.getText().toString(),
                                            name.getText().toString(), 
                                            joindate.getText().toString(),
                                            club,
                                            poscode.getText().toString(), 
                                            ctctper.getText().toString(),
                                            phone.getText().toString(),
                                            hp.getText().toString(), 
                                            area.getText().toString(),
                                            subarea.getText().toString());
            }

            finish();

        };

    };  

   }      

AlmagHelper.java :

package com.lm.mobilesalesdb12;

import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

class AlmagHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME="masteroutletae.db";
    private static final int SCHEMA_VERSION=1;

    public AlmagHelper(Context context) {
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE tblmsdboutlet (_id INTEGER PRIMARY KEY, outletno INT UNIQUE, name TEXT, joindate DATE, club TEXT, poscode INT, ctctper TEXT, phone TEXT, hp TEXT, area TEXT, subarea TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // no-op, since will not be called until 2nd schema
        // version exists
    }

    public Cursor getAll(String orderBy) {
        return(getReadableDatabase()
                        .rawQuery("SELECT _id, outletno, name, joindate, club, poscode, ctctper, phone, hp, area, subarea FROM tblmsdboutlet ORDER BY "+orderBy,
                                            null));


    }

    public Cursor getById(String id) {
        String[] args={id};

        return(getReadableDatabase()
                        .rawQuery("SELECT _id, outletno, name, joindate, club, poscode, ctctper, phone, hp, area, subarea FROM tblmsdboutlet WHERE _ID=?",
                                            args));
    }

    public void insert(String outletno, String name, String joindate, String club, String poscode, String ctctper,
                                         String phone, String hp, String area, String subarea) {



        ContentValues cv=new ContentValues();

        cv.put("outletno", outletno);
        cv.put("name", name);
        cv.put("joindate", joindate);
        cv.put("club", club);
        cv.put("poscode", poscode);
        cv.put("ctctper", ctctper);
        cv.put("phone", phone);
        cv.put("hp", hp);
        cv.put("area", area);
        cv.put("subarea", subarea);



        getWritableDatabase().insert("tblmsdboutlet", "outletno", cv);
//      getWritableDatabase().insert("tblmsdboutlet", "name", cv);


    }

    public void update(String id, String outletno, String name, String joindate, String club, String poscode, String ctctper,
             String phone, String hp, String area, String subarea) {
        ContentValues cv=new ContentValues();
        String[] args={id};

        cv.put("outletno", outletno);
        cv.put("name", name);
        cv.put("joindate", joindate);
        cv.put("club", club);
        cv.put("poscode", poscode);
        cv.put("ctctper", ctctper);
        cv.put("phone", phone);
        cv.put("hp", hp);
        cv.put("area", area);
        cv.put("subarea", subarea);

        getWritableDatabase().update("tblmsdboutlet", cv, "_ID=?",
                                                                 args);
    }



    public String getOutletno(Cursor c) {
        return(c.getString(1));
    }


    public String getName(Cursor c) {
    return(c.getString(2));
    }

    public String getJoindate(Cursor c) {
        return(c.getString(3));
    }

    public String getClub(Cursor c) {
        return(c.getString(4));
    }

    public String getPoscode(Cursor c) {
        return(c.getString(5));
    }

    public String getCtctper(Cursor c) {
        return(c.getString(6));
    }


    public String getPhone(Cursor c) {
        return(c.getString(7));
    }

    public String getHp(Cursor c) {
        return(c.getString(8));
    }

    public String getArea(Cursor c) {
        return(c.getString(9));
    }

    public String getSubarea(Cursor c) {
        return(c.getString(10));
    }

}

CustomOnItemSelectedListener,java:

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

    Spinner clubspinner;


    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {


        Toast.makeText(parent.getContext(), 
                "On Item Select : \n" + parent.getItemAtPosition(pos).toString(),
                Toast.LENGTH_LONG).show();
    }

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

    }

}

DetailLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:id="@+id/widget60"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://ift.tt/nIICcg">


<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    >

    <TableRow>
        <TextView android:text="Outletno:" />
        <EditText android:id="@+id/outletno" 
                  android:inputType="number"
        />
    </TableRow>

    <TableRow>
        <TextView android:text="Name:" />
        <EditText android:id="@+id/name" 

        />
    </TableRow>


    <TableRow>
        <TextView android:text="Joindate:" />
        <EditText android:id="@+id/joindate" />


    </TableRow>


    <TableRow>
        <TextView android:text="Club:" />
        <Spinner 
        android:id="@+id/clubspinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:entries="@array/club1_arrays"
        android:prompt="@string/club1_prompt"
        />

    </TableRow>

    <TableRow>
        <TextView android:text="Poscode:" />
        <EditText android:id="@+id/poscode" 
                  android:inputType="number"

        />
    </TableRow>


    <TableRow>
        <TextView android:text="Ctctper:" />
        <EditText android:id="@+id/ctctper" 


        />
    </TableRow>


    <TableRow>
        <TextView android:text="Phone:" />
        <EditText android:id="@+id/phone" 
                  android:inputType="number"
        />
    </TableRow>

    <TableRow>
        <TextView android:text="Hp:" />
        <EditText android:id="@+id/hp" 
                  android:inputType="number"   
        />
    </TableRow>

    <TableRow>
        <TextView android:text="Area:" />
        <EditText android:id="@+id/area" />
    </TableRow>


    <TableRow>
        <TextView android:text="Subarea:" />
        <EditText android:id="@+id/subarea" />
    </TableRow>

    <Button android:id="@+id/btn_save"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Save"
    />

</TableLayout>  

</ScrollView>

String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Dboutlet</string>


    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="detail_form">DetailForm</string>

    <string name="outletno">Enter Rollno: </string>
    <string name="name">Enter Name: </string>
    <string name="joindate">Enter Marks: </string>
    <string name="club">Enter Rollno: </string>
    <string name="ctctper">Enter Name: </string>
    <string name="phone">Enter Marks: </string>
    <string name="hp">Enter Rollno: </string>
    <string name="area">Enter Name: </string>
    <string name="subarea">Enter Marks: </string>

    <string name="view">View</string>
    <string name="view_all">View All</string>

    <string name="club1_prompt">Choose a club</string>

    <string-array name="club1_arrays">
                <item>Persija </item>
                <item>Chelsea</item>
                <item>PSG</item>
                <item>Arsenal</item>
                <item>Bolton</item>
    </string-array> 



</resources>

Aucun commentaire:

Enregistrer un commentaire