- package com.example.mysmsreceiver;
- import android.os.Bundle;
- import android.app.Activity;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.view.Menu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.AdapterView.OnItemSelectedListener;
- public class MainActivity extends Activity {
- private SMSDB helper;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- helper = new SMSDB(this);
- final SQLiteDatabase db = helper.getReadableDatabase();
- Cursor c = db.query ("smstb", new String[]{"mobile"}, null, null, "mobile", null, null);
- c.moveToFirst();
- CharSequence[] list = new CharSequence[c.getCount()];
- for(int i = 0; i < list.length; i++)
- {
- list[i] = c.getString(0);
- c.moveToNext();
- }
- c.close();
- Spinner spinner = (Spinner)this.findViewById(R.id.Spinner01);
- spinner.setAdapter(new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,list));
- spinner.setOnItemSelectedListener(new OnItemSelectedListener()
- {
- public void onItemSelected(AdapterView<?> parent, View view,
- int position, long id) {
- // TODO Auto-generated method stub
- String mobile = ((Spinner)parent).getSelectedItem().toString();
-
- Cursor c = db.query("smstb", new String[]{"message"}, "mobile='"+mobile+"'", null, null, null, null);
- c.moveToFirst();
- CharSequence[] list = new CharSequence[c.getCount()];
- for(int i = 0; i < list.length; i++)
- {
- list[i] = c.getString(0);
- c.moveToNext();
- }
- c.close();
- ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(MainActivity.this,android.R.layout.simple_list_item_1,list);
- ListView listView = (ListView)findViewById(R.id.ListView01);
- listView.setAdapter(adapter);
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
-
- }
-
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }
複製代碼 |