Click here to Skip to main content
15,884,099 members
Home / Discussions / Android
   

Android

 
AnswerRe: Live GPS Tracking Pin
Richard MacCutchan17-Feb-16 4:29
mveRichard MacCutchan17-Feb-16 4:29 
AnswerRe: Live GPS Tracking Pin
David Crow17-Feb-16 7:12
David Crow17-Feb-16 7:12 
QuestionUnable to use Listadapter along with getfilter() for dynamic search filter? Pin
A KA15-Feb-16 5:48
A KA15-Feb-16 5:48 
SuggestionRe: Unable to use Listadapter along with getfilter() for dynamic search filter? Pin
David Crow15-Feb-16 9:12
David Crow15-Feb-16 9:12 
QuestionNative vs Hybrid App Development Pin
Member 1232849715-Feb-16 3:13
Member 1232849715-Feb-16 3:13 
QuestionAndroid: cannot open drawer inside onReceivedError Pin
Bazzer13-Feb-16 11:09
Bazzer13-Feb-16 11:09 
QuestionConsuming WCF web service from android Pin
Member 1232072611-Feb-16 0:26
Member 1232072611-Feb-16 0:26 
QuestionPopulate Spinner from Sqlite and display the associated record of selected or populated item Pin
Varma Lanke8-Feb-16 19:46
Varma Lanke8-Feb-16 19:46 
My Database Helper Code is Here

C#
public class DataHelper extends SQLiteOpenHelper {
    public static final String DB_NAME="myDb";
    public static final String Table_Pro="student";


  public static final String Create_Pro="Create table if not exists "+Table_Pro+"(id integer primary key autoincrement,pname not null unique,age int,qual text)";

    public static final String Delete_Pro="Drop table if exists "+Table_Pro;



    public DataHelper(Context context) {
        super(context, DB_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
      db.execSQL(Create_Pro);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(Delete_Pro);
       // db.execSQL("DROP TABLE IF EXISTS "+Table_Pro);
        onCreate(db);

    }
    public void insertProvince(String pname,int age,String qual)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        db.beginTransaction();
        ContentValues values;
        try
        {
            values=new ContentValues();
            values.put("pname",pname);
            values.put("age",age);
            values.put("qual",qual);
            db.insert(Table_Pro, null, values);
            db.setTransactionSuccessful();

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            db.endTransaction();
            db.close();
        }


    }
    public ArrayList<String> getAllProvinces()
    {
        ArrayList<String> list=new ArrayList<String>();
        SQLiteDatabase db=this.getReadableDatabase();
        db.beginTransaction();

        try
        {
            String selectQuery = " SELECT * FROM "+ Table_Pro;

                    Cursor cursor=db.rawQuery(selectQuery,null);
            if(cursor.getCount()>0)
            {
                while (cursor.moveToNext())
                {
                    String pname=cursor.getString(cursor.getColumnIndex("pname"));
                    list.add(pname);
                }
            }
            db.setTransactionSuccessful();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            db.endTransaction();
            db.close();
        }
        return list;

    }
   public Cursor getAllData()
    {

        SQLiteDatabase db = getReadableDatabase();
        Cursor res=db.rawQuery("select * from "+Table_Pro,null);
        return res;
    }

}



Main Activity is here

Java
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {


    DataHelper dataHelper=new DataHelper(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dataHelper.insertProvince("John",24,"Btech");
        dataHelper.insertProvince("Bob",25,"Mtech");
        dataHelper.insertProvince("Rock",30,"MBA");
        dataHelper.insertProvince("Alice",45,"MCA");
        //dataHelper.insertProvince("Rock");
        dataHelper.insertProvince("Strike",40,"Masters");
        ArrayList<String> listpro=dataHelper.getAllProvinces();
        Spinner sp=(Spinner)findViewById(R.id.spinner_data);
        sp.setOnItemSelectedListener(this);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,listpro);
        sp.setAdapter(adapter);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
       String label = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), "Welcome: " + label,
                Toast.LENGTH_LONG).show();
        // Showing selected spinner item
        Cursor res = dataHelper.getAllData();
        if (res.getCount() == 0) {
            showMessage("Error", "Nothing Found");
            return;
        }
        StringBuffer buffer = new StringBuffer();
       // if (parent== findViewById(R.id.spinner_data))
        while (res.moveToNext()) {

            buffer.append("age: " + res.getString(2) + "\n");
            buffer.append("Qualification: " + res.getString(3) + "\n\n");
            showMessage("Data", buffer.toString());

        }
      /* if (parent== findViewById(R.id.spinner_data)){
            dataHelper = (DataHelper) parent.getItemAtPosition(position);
            tname.setText("Welcome: " + dataHelper.getName());
            tage.setText("Age: " + dataHelper.getAge().toString());
            tqual.setText("Qualification: " + dataHelper.getEmail());
        }*/

    }

public void showMessage(String title,String Message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(Message);
    builder.show();


}
    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}


From there i have to get the age and qualification of selected name or person
but this code gives all persons age and qualification

Can You please help me to get out from here


Thank You a lot

modified 9-Feb-16 1:51am.

AnswerRe: Populate Spinner from Sqlite and display the associated record of selected or populated item Pin
Richard MacCutchan8-Feb-16 21:53
mveRichard MacCutchan8-Feb-16 21:53 
SuggestionRe: Populate Spinner from Sqlite and display the associated record of selected or populated item Pin
David Crow9-Feb-16 2:14
David Crow9-Feb-16 2:14 
QuestionTutorialObjectTracking-master Pin
Member 122599845-Feb-16 2:25
Member 122599845-Feb-16 2:25 
AnswerRe: TutorialObjectTracking-master Pin
David Crow5-Feb-16 2:32
David Crow5-Feb-16 2:32 
GeneralRe: TutorialObjectTracking-master Pin
Member 122599845-Feb-16 2:47
Member 122599845-Feb-16 2:47 
GeneralRe: TutorialObjectTracking-master Pin
Richard MacCutchan5-Feb-16 6:25
mveRichard MacCutchan5-Feb-16 6:25 
GeneralRe: TutorialObjectTracking-master Pin
Member 122599846-Feb-16 4:10
Member 122599846-Feb-16 4:10 
GeneralRe: TutorialObjectTracking-master Pin
Richard MacCutchan6-Feb-16 4:12
mveRichard MacCutchan6-Feb-16 4:12 
GeneralRe: TutorialObjectTracking-master Pin
Member 122599846-Feb-16 4:30
Member 122599846-Feb-16 4:30 
GeneralRe: TutorialObjectTracking-master Pin
Richard MacCutchan6-Feb-16 5:23
mveRichard MacCutchan6-Feb-16 5:23 
GeneralRe: TutorialObjectTracking-master Pin
David Crow7-Feb-16 9:03
David Crow7-Feb-16 9:03 
GeneralRe: TutorialObjectTracking-master Pin
Member 1225998410-Feb-16 6:54
Member 1225998410-Feb-16 6:54 
SuggestionRe: TutorialObjectTracking-master Pin
David Crow10-Feb-16 6:58
David Crow10-Feb-16 6:58 
QuestionInsert Images in EditText as in Notes app in Android Mobiles Pin
Tirumaleswara Reddy.K4-Feb-16 2:16
Tirumaleswara Reddy.K4-Feb-16 2:16 
QuestionRe: Insert Images in EditText as in Notes app in Android Mobiles Pin
David Crow4-Feb-16 6:21
David Crow4-Feb-16 6:21 
AnswerRe: Insert Images in EditText as in Notes app in Android Mobiles Pin
Tirumaleswara Reddy.K4-Feb-16 18:44
Tirumaleswara Reddy.K4-Feb-16 18:44 
AnswerRe: Insert Images in EditText as in Notes app in Android Mobiles Pin
Richard MacCutchan4-Feb-16 21:39
mveRichard MacCutchan4-Feb-16 21:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.