1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
| package com.example.sms;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.os.Build;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity {
EditText editText;
ListView listView;
ArrayList<String> grups;
ArrayAdapter<String> adapter;
AlertDialog.Builder message;
sms sqh;
SQLiteDatabase sqdb;
ContentValues cv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
message = new AlertDialog.Builder(MainActivity.this);
listView = (ListView) findViewById(R.id.listView1);
cv=new ContentValues();
editText = (EditText) findViewById(R.id.editText1);
//Создаём пустой массив для хранения имен групп
grups = new ArrayList<String>();
//Создаём адаптер ArrayAdapter, чтобы привязать массив к ListView
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, grups);
//Привяжем массив через адаптер к ListView
listView.setAdapter(adapter);
// Инициализируем наш класс-обёртку
sqh = new sms(this);
//База нам нужна для записи и чтения
sqdb = sqh.getWritableDatabase();
read_grups();
//прячем клавиатуру. butCalculate - это кнопка
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
// Прослушиваем нажатия клавиш
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_ENTER) {
if(editText.length()==0)return true;
if(grups.size()!=0){
String gr1, gr2;
gr2=editText.getText().toString();
gr2=gr2.toLowerCase();
for(int i=0;i<grups.size();i++){
gr1=grups.get(i).toString();
gr1=gr1.toLowerCase();
if(gr1.equalsIgnoreCase(gr2)){
message.setTitle(R.string.message2)
.setMessage(R.string.message1)
.setCancelable(false)
.setNegativeButton("ОК",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = message.create();
alert.show();
editText.setText("");
return true;
}
}
}
grups.add(0, editText.getText().toString());
adapter.notifyDataSetChanged();
ContentValues cv=new ContentValues();
cv.put(sms.NAME1, editText.getText().toString());
sqdb.insert(sms.TABLE_NAME1, sms.NAME1, cv);
editText.setText("");
//прячем клавиатуру. butCalculate - это кнопка
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
return true;
}
return false;
}
});
}
public void read_grups(){
Cursor cursor = sqdb.query(sms.TABLE_NAME1, new String[] {
sms.NAME1 },
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
null // The sort order
);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor
.getColumnIndex(sms.NAME1));
grups.add(0, name);
}
cursor.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container, false);
return rootView;
}
}
public void onClickB1(View v)
{
Intent intent = new Intent(MainActivity.this, arhiv.class);
startActivity(intent);
}
} |