· 8 years ago · May 15, 2018, 06:14 PM
1activity main
2
3
4<?xml version="1.0" encoding="utf-8"?>
5<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
6 xmlns:app="http://schemas.android.com/apk/res-auto"
7 xmlns:tools="http://schemas.android.com/tools"
8 android:orientation="vertical"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent"
11 tools:context="com.example.vladimir.thirdfinal.MainActivity">
12
13 <EditText
14 android:id="@+id/fname"
15 android:layout_width="match_parent"
16 android:layout_height="wrap_content" />
17
18 <EditText
19 android:id="@+id/lname"
20 android:layout_width="match_parent"
21 android:layout_height="wrap_content" />
22
23 <EditText
24 android:id="@+id/num"
25 android:layout_width="match_parent"
26 android:layout_height="wrap_content" />
27
28 <Button
29 android:id="@+id/btn"
30 android:layout_width="match_parent"
31 android:layout_height="wrap_content"
32 android:text="Upload"
33 android:onClick="addToContactList"/>
34
35 <android.support.v7.widget.RecyclerView
36 android:id="@+id/rv"
37 android:layout_width="match_parent"
38 android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
39
40
41</LinearLayout>
42
43
44
45main activity
46
47package com.example.vladimir.thirdfinal;
48
49import android.content.ContentValues;
50import android.database.Cursor;
51import android.database.sqlite.SQLiteDatabase;
52import android.support.v7.app.AppCompatActivity;
53import android.os.Bundle;
54import android.support.v7.widget.LinearLayoutManager;
55import android.support.v7.widget.RecyclerView;
56import android.util.Log;
57import android.view.View;
58import android.widget.EditText;
59
60public class MainActivity extends AppCompatActivity {
61
62 private ContactListAdapter mAdapter;
63 private SQLiteDatabase mDb;
64 private EditText mncn;
65 private EditText mncs;
66 private EditText mncnum;
67
68 private final static String LOG_TAG = MainActivity.class.getSimpleName();
69
70 @Override
71 protected void onCreate(Bundle savedInstanceState) {
72 super.onCreate(savedInstanceState);
73 setContentView(R.layout.activity_main);
74
75 RecyclerView crv;
76
77 crv = (RecyclerView) this.findViewById(R.id.rv);
78 mncn =(EditText) this.findViewById(R.id.fname);
79 mncs =(EditText) this.findViewById(R.id.lname);
80 mncnum =(EditText) this.findViewById(R.id.num);
81
82 crv.setLayoutManager(new LinearLayoutManager(this));
83
84 ContactDBHelper dbHelper = new ContactDBHelper(this);
85
86 mDb = dbHelper.getWritableDatabase();
87
88 Cursor cursor = getAllContacts();
89
90 mAdapter = new ContactListAdapter(this , cursor);
91
92 crv.setAdapter(mAdapter);
93 }
94
95 public void addToContactList(View view) {
96 if(mncn.getText().length() == 0 ||
97 mncs.getText().length() == 0 ||
98 mncnum.getText().length() == 0) {
99 return;
100 }
101 int phone = 0;
102 try {
103 phone = Integer.parseInt(mncnum.getText().toString());
104 }catch(NumberFormatException ex){
105 Log.e(LOG_TAG,"Failed to convert"+ex.getMessage());
106 }
107
108 addNewContact(mncn.getText().toString(),phone);
109
110 mAdapter.swapCursor(getAllContacts());
111
112 mncnum.clearFocus();
113
114 mncn.getText().clear();
115 mncs.getText().clear();
116 mncnum.getText().clear();
117 }
118
119 private Cursor getAllContacts() {
120 return mDb.query(
121 ContactContract.ContactEntry.TABLE_NAME,
122 null,
123 null,
124 null,
125 null,
126 null,
127 null
128 );
129 }
130
131 private long addNewContact(String name,String surename,int phone)
132 {
133 ContentValues cv = new ContentValues();
134 cv.put(ContactContract.ContactEntry.COLUMN_CONTACT_NAME, name);
135 cv.put(ContactContract.ContactEntry.COLUMN_CONTACT_SURENAME, surename);
136 cv.put(ContactContract.ContactEntry.COLUMN_CONTACT_NUMBER, phone);
137
138 return mDb.insert(ContactContract.ContactEntry.TABLE_NAME, null, cv);
139 }
140}
141
142
143contact xml
144
145
146
147<?xml version="1.0" encoding="utf-8"?>
148<LinearLayout
149 xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
150 android:layout_height="match_parent"
151 android:orientation="vertical">
152
153 <TextView
154 android:id="@+id/txtv1"
155 android:layout_width="wrap_content"
156 android:layout_height="wrap_content"
157 android:text="TextView" />
158
159 <TextView
160 android:id="@+id/txtv2"
161 android:layout_width="wrap_content"
162 android:layout_height="wrap_content"
163 android:text="TextView" />
164
165 <TextView
166 android:id="@+id/txtv3"
167 android:layout_width="wrap_content"
168 android:layout_height="wrap_content"
169 android:text="TextView" />
170
171</LinearLayout>
172
173
174contactContract
175
176
177package com.example.vladimir.thirdfinal;
178
179import android.provider.BaseColumns;
180
181/**
182 * Created by Vladimir on 15.05.2018.
183 */
184
185public class ContactContract {
186
187 public static final class ContactEntry implements BaseColumns {
188 public static final String TABLE_NAME="contactlist";
189 public static final String COLUMN_CONTACT_NAME = "contactName";
190 public static final String COLUMN_CONTACT_SURENAME = "contactSurename";
191 public static final String COLUMN_CONTACT_NUMBER = "contactNumber";
192 }
193
194}
195
196
197contact db helper
198
199package com.example.vladimir.thirdfinal;
200
201import android.content.Context;
202import android.database.sqlite.SQLiteDatabase;
203import android.database.sqlite.SQLiteOpenHelper;
204
205
206
207/**
208 * Created by Vladimir on 15.05.2018.
209 */
210
211public class ContactDBHelper extends SQLiteOpenHelper {
212
213 private static final String DATABASE_NAME = "contacts.db";
214 private static final int DATABASE_VERSION = 1;
215
216 public ContactDBHelper(Context context) {
217
218 super(context,DATABASE_NAME,null,DATABASE_VERSION);
219
220 }
221
222 @Override
223 public void onCreate(SQLiteDatabase sqLiteDatabase) {
224
225 final String SQL_CREATE_CONTACTS_TABLE = "CREATE TABLE " + ContactContract.ContactEntry.TABLE_NAME + " (" +
226 ContactContract.ContactEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
227 ContactContract.ContactEntry.COLUMN_CONTACT_NAME + " TEXT NOT NULL," +
228 ContactContract.ContactEntry.COLUMN_CONTACT_SURENAME + " TEXT NOT NULL," +
229 ContactContract.ContactEntry.COLUMN_CONTACT_NUMBER + "INTEGER NOT NULL" +
230 "); ";
231
232 sqLiteDatabase.execSQL(SQL_CREATE_CONTACTS_TABLE);
233
234 }
235
236 @Override
237 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i,int i1) {
238 sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + ContactContract.ContactEntry.TABLE_NAME);
239 onCreate(sqLiteDatabase);
240 }
241
242}
243
244contact list adapter
245
246
247package com.example.vladimir.thirdfinal;
248
249import android.content.Context;
250import android.database.Cursor;
251import android.support.v7.widget.RecyclerView;
252import android.view.LayoutInflater;
253import android.view.View;
254import android.view.ViewGroup;
255import android.widget.TextView;
256
257/**
258 * Created by Vladimir on 15.05.2018.
259 */
260
261public class ContactListAdapter extends RecyclerView.Adapter<ContactViewHolder> {
262
263 private Cursor mCursor;
264 private Context mContext;
265
266 public ContactListAdapter(Context context,Cursor cursor) {
267 this.mCursor = cursor;
268 this.mContext = context;
269 }
270
271 @Override
272 public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
273 LayoutInflater inflanter = LayoutInflater.from(mContext);
274 View view = inflanter.inflate(R.layout.contact,parent,false);
275 return new ContactViewHolder(view);
276 }
277
278 @Override
279 public void onBindViewHolder(ContactViewHolder holder, int position) {
280 if(!mCursor.moveToPosition(position))
281 return;
282
283 String name = mCursor.getString(mCursor.getColumnIndex(ContactContract.ContactEntry.COLUMN_CONTACT_NAME));
284 String surname = mCursor.getString(mCursor.getColumnIndex(ContactContract.ContactEntry.COLUMN_CONTACT_SURENAME));
285 String num = mCursor.getString(mCursor.getColumnIndex(ContactContract.ContactEntry.COLUMN_CONTACT_NUMBER));
286
287 holder.nametv.setText(name);
288 holder.surenametv.setText(surname);
289 holder.numtv.setText(num);
290 }
291
292 @Override
293 public int getItemCount() {
294 return mCursor.getCount();
295 }
296
297 public void swapCursor(Cursor newCursor) {
298 if(mCursor != null) mCursor.close();
299 mCursor=newCursor;
300 if (newCursor != null) {
301 this.notifyDataSetChanged();
302 }
303 }
304
305 class ContactViewHolder extends RecyclerView.ViewHolder {
306 TextView nametv;
307 TextView surenametv;
308 TextView numtv;
309
310 public ContactViewHolder(View itemView) {
311 super(itemView);
312
313 nametv = (TextView) itemView.findViewById(R.id.txtv1);
314 surenametv = (TextView) itemView.findViewById(R.id.txtv2);
315 numtv = (TextView) itemView.findViewById(R.id.txtv3);
316 }
317 }
318}