Решение этой проблемы через базу данных:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Справочник стран"
android:textSize="14sp"
android:gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp">
</TextView>
<Button
android:id="@+id/btnAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Все записи"
android:layout_marginTop="5dp">
</Button>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SMS Send"
android:id="@+id/smssend"/>
</LinearLayout>
MainActivity.java
import android.app.ActionBar;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener {
final String LOG_TAG = "mLogs";
String name[] = { "MTSOne", "MTS Two", "Kyivstar", "Life" };
String ip1[] = { "192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4" };
String socket1[] = { "1000", "2000", "3000", "4000" };
Button btnAll, smssend;
Spinner spinner;
DBHelper dbHelper;
SQLiteDatabase db;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAll = (Button) findViewById(R.id.btnAll);
btnAll.setOnClickListener(this);
smssend = (Button) findViewById(R.id.smssend);
smssend.setOnClickListener(this);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> ok1Adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, name);
ok1Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(ok1Adapter);
dbHelper = new DBHelper(this);
// подключаемся к базе
db = dbHelper.getWritableDatabase();
// проверка существования записей
Cursor c = db.query("mytable", null, null, null, null, null, null);
if (c.getCount() == 0) {
ContentValues cv = new ContentValues();
// заполним таблицу
for (int i = 0; i < 4; i++) {
cv.put("name", name[i]);
cv.put("ip1", ip1[i]);
cv.put("socket1", socket1[i]);
Log.d(LOG_TAG, "id = " + db.insert("mytable", null, cv));
}
}
c.close();
dbHelper.close();
// эмулируем нажатие кнопки btnAll
onClick(btnAll);
}
public void onClick(View v) {
// подключаемся к базе
db = dbHelper.getWritableDatabase();
// данные с экрана
String sName = spinner.getSelectedItem().toString();
// переменные для query
String selection = null;
String[] selectionArgs = null;
// курсор
Cursor c = null;
// определяем нажатую кнопку
switch (v.getId()) {
// Все записи
case R.id.btnAll:
Log.d(LOG_TAG, "--- Все записи ---");
c = db.query("mytable", null, null, null, null, null, null);
break;
case R.id.smssend:
selection = "name = ?";
selectionArgs = new String[] { sName };
c = db.query("mytable", null, selection, selectionArgs, null, null, null);
if (c.moveToFirst()) {
int ip1ColIndex = c.getColumnIndex("ip1");
int socket1ColIndex = c.getColumnIndex("socket1");
// int ip2ColIndex = c.getColumnIndex("ip2");
// int socket2ColIndex = c.getColumnIndex("socket2");
// int apnColIndex = c.getColumnIndex("apn");
sendSmsMessage("+380676711761", c.getString(ip1ColIndex) + "*"
+ c.getString(socket1ColIndex) + "*" );
Toast.makeText(MainActivity.this, "SMS Sent", Toast.LENGTH_SHORT).show(); }
else {
Toast toast = Toast.makeText(MainActivity.this, "Такого об'єкту не знайдено", Toast.LENGTH_LONG);
toast.show();
}
break;
}
if (c != null) {
if (c.moveToFirst()) {
String str;
do {
str = "";
for (String cn : c.getColumnNames()) {
str = str.concat(cn + " = "
+ c.getString(c.getColumnIndex(cn)) + "; ");
}
Log.d(LOG_TAG, str);
} while (c.moveToNext());
}
c.close();
} else
Log.d(LOG_TAG, "Cursor is null");
dbHelper.close();
}
class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
// конструктор суперкласса
super(context, "myDB", null, 1);
}
public void onCreate(SQLiteDatabase db) {
Log.d(LOG_TAG, "--- onCreate database ---");
// создаем таблицу с полями
db.execSQL("create table mytable ("
+ "id integer primary key autoincrement," + "name text,"
+ "ip1 integer," + "socket1 text" + ");");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
private void sendSmsMessage(String address, String message) {
SmsManager SMSMgr = SmsManager.getDefault();
SMSMgr.sendTextMessage(address, null, message, null, null);
}
}
new ArrayAdapter<Operator>(this, android.R.layout.simple_list_item_1, operators);