Я пытаюсь создать приложение android для поиска контактов с использованием базы данных sqlite. Как импортировать контакты около 2000 (в файле, Excel или json или xml) или другую внешнюю базу данных в мою базу данных, когда приложение устанавливается впервые. Я получил этот код из Интернета, я внес некоторые изменения в соответствии с моим требованием.
Основная деятельность
package com.example.myapplication;
import android.app.SearchManager;
import com.example.myapplication.adapter.ContactListAdapter;
import com.example.myapplication.database.DatabaseHelper;
import com.example.myapplication.entities.Contact;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.SearchView;
import android.view.Menu;
import android.widget.ListView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listViewContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//loadData();
}
private void initView() {
listViewContact = findViewById(R.id.listViewContact);
registerForContextMenu(listViewContact);
}
private void loadData() {
DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());
List<Contact> contacts = databaseHelper.findAll();
if (contacts != null) {
listViewContact.setAdapter(new ContactListAdapter(getApplicationContext(), contacts));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.searchView).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
searchContact(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
searchContact(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
private void searchContact(String keyword) {
DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());
List<Contact> contacts = databaseHelper.search(keyword);
if (contacts != null) {
listViewContact.setAdapter(new ContactListAdapter(getApplicationContext(), contacts));
}
}
}
Помощник по базе данных (это мой класс DBhelper)
package com.example.myapplication.database;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.myapplication.entities.Contact;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
private static String dbName = "ts_telephone";
private static int dbVersion = 1;
private static String telephone = "telephone";
private static String idColumn = "Id";
private static String PrefixColumn = "Prefix";
private static String nameColumn = "name";
private static String designationColumn = "designation";
private static String departmentColumn = "department";
private static String sectionColumn = "section";
private static String addressColumn = "designation";
private static String std_codeColumn = "std_code";
private static String od_vpnColumn = "od_vpn";
private static String odColumn = "od";
private static String r_phoneColumn = "r_phone";
private static String r_vpnColumn = "r_vpn";
private static String cell_noColumn = "cell_no";
private static String faxColumn = "fax";
private static String emailColumn = "email";
private static String IntercomColumn = "Intercom";
//sl_no prefix name designation department section address std_code od_vpn od r_phone r_vpn cell_no fax email Intercom
public DatabaseHelper(Context context) {
super(context, dbName, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase ts_telephone) {
ts_telephone.execSQL("create table " + telephone + "(" +
idColumn + " integer primary key autoincrement, " +
PrefixColumn + " text, " +
nameColumn + " text, " +
designationColumn + " text, " +
departmentColumn + " text, " +
sectionColumn + " text, " +
addressColumn + " text, " +
std_codeColumn + " text, " +
od_vpnColumn + " text, " +
odColumn +"text,"+
r_phoneColumn + " text, " +
r_vpnColumn + " text, " +
cell_noColumn + " text," +
faxColumn + " text, " +
emailColumn + " text," +
IntercomColumn + " text " +
")"
);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("drop table if exists " + telephone);
onCreate(sqLiteDatabase);
}
public List<Contact> findAll() {
List<Contact> contacts = null;
try {
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + telephone, null);
if (cursor.moveToFirst()) {
contacts = new ArrayList<Contact>();
do {
Contact contact = new Contact();
contact.setId(cursor.getInt(0));
contact.setPrefix(cursor.getString(1));
contact.setName(cursor.getString(2));
contact.setDesignation(cursor.getString(3));
contact.setDepartment(cursor.getString(4));
contact.setSection(cursor.getString(5));
contact.setAddress(cursor.getString(6));
contact.setStd_code(cursor.getString(7));
contact.setOd_vpn(cursor.getString(8));
contact.setOd(cursor.getString(9));
contact.setR_phone(cursor.getString(10));
contact.setR_vpn(cursor.getString(11));
contact.setCell_no(cursor.getString(12));
contact.setFax(cursor.getString(13));
contact.setEmail(cursor.getString(14));
contact.setIntercom(cursor.getString(15));
contacts.add(contact);
} while (cursor.moveToNext());
}
} catch (Exception e) {
contacts = null;
}
return contacts;
}
public List<Contact> search(String keyword) {
List<Contact> contacts = null;
try {
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + telephone + " where " + nameColumn + " like ?" , new String[]{"%" + keyword + "%"});
if (cursor.moveToFirst()) {
contacts = new ArrayList<Contact>();
do {
Contact contact = new Contact();
contact.setId(cursor.getInt(0));
contact.setPrefix(cursor.getString(1));
contact.setName(cursor.getString(2));
contact.setDesignation(cursor.getString(3));
contact.setDepartment(cursor.getString(4));
contact.setSection(cursor.getString(5));
contact.setAddress(cursor.getString(6));
contact.setStd_code(cursor.getString(7));
contact.setOd_vpn(cursor.getString(8));
contact.setOd(cursor.getString(9));
contact.setR_phone(cursor.getString(10));
contact.setR_vpn(cursor.getString(11));
contact.setCell_no(cursor.getString(12));
contact.setFax(cursor.getString(13));
contact.setEmail(cursor.getString(14));
contact.setIntercom(cursor.getString(15));
contacts.add(contact);
} while (cursor.moveToNext());
}
} catch (Exception e) {
contacts = null;
}
return contacts;
}
public boolean create(Contact contact) {
boolean result = true;
try {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(PrefixColumn, contact.getPrefix());
contentValues.put(nameColumn, contact.getName());
contentValues.put(designationColumn, contact.getDesignation());
contentValues.put(departmentColumn, contact.getDepartment());
contentValues.put(sectionColumn, contact.getSection());
contentValues.put(addressColumn, contact.getAddress());
contentValues.put(std_codeColumn, contact.getStd_code());
contentValues.put(od_vpnColumn, contact.getOd_vpn());
contentValues.put(odColumn, contact.getOd());
contentValues.put(r_phoneColumn, contact.getR_phone());
contentValues.put(r_vpnColumn, contact.getR_vpn());
contentValues.put(cell_noColumn, contact.getCell_no());
contentValues.put(faxColumn, contact.getFax());
contentValues.put(emailColumn, contact.getEmail());
contentValues.put(IntercomColumn, contact.getIntercom());
result = sqLiteDatabase.insert(telephone, null, contentValues) > 0;
} catch (Exception e) {
result = false;
}
return result;
}
}
контакт. java
package com.example.myapplication.database;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.myapplication.entities.Contact;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
private static String dbName = "ts_telephone";
private static int dbVersion = 1;
private static String telephone = "telephone";
private static String idColumn = "Id";
private static String PrefixColumn = "Prefix";
private static String nameColumn = "name";
private static String designationColumn = "designation";
private static String departmentColumn = "department";
private static String sectionColumn = "section";
private static String addressColumn = "designation";
private static String std_codeColumn = "std_code";
private static String od_vpnColumn = "od_vpn";
private static String odColumn = "od";
private static String r_phoneColumn = "r_phone";
private static String r_vpnColumn = "r_vpn";
private static String cell_noColumn = "cell_no";
private static String faxColumn = "fax";
private static String emailColumn = "email";
private static String IntercomColumn = "Intercom";
//sl_no prefix name designation department section address std_code od_vpn od r_phone r_vpn cell_no fax email Intercom
public DatabaseHelper(Context context) {
super(context, dbName, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase ts_telephone) {
ts_telephone.execSQL("create table " + telephone + "(" +
idColumn + " integer primary key autoincrement, " +
PrefixColumn + " text, " +
nameColumn + " text, " +
designationColumn + " text, " +
departmentColumn + " text, " +
sectionColumn + " text, " +
addressColumn + " text, " +
std_codeColumn + " text, " +
od_vpnColumn + " text, " +
odColumn +"text,"+
r_phoneColumn + " text, " +
r_vpnColumn + " text, " +
cell_noColumn + " text," +
faxColumn + " text, " +
emailColumn + " text," +
IntercomColumn + " text " +
")"
);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("drop table if exists " + telephone);
onCreate(sqLiteDatabase);
}
public List<Contact> findAll() {
List<Contact> contacts = null;
try {
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + telephone, null);
if (cursor.moveToFirst()) {
contacts = new ArrayList<Contact>();
do {
Contact contact = new Contact();
contact.setId(cursor.getInt(0));
contact.setPrefix(cursor.getString(1));
contact.setName(cursor.getString(2));
contact.setDesignation(cursor.getString(3));
contact.setDepartment(cursor.getString(4));
contact.setSection(cursor.getString(5));
contact.setAddress(cursor.getString(6));
contact.setStd_code(cursor.getString(7));
contact.setOd_vpn(cursor.getString(8));
contact.setOd(cursor.getString(9));
contact.setR_phone(cursor.getString(10));
contact.setR_vpn(cursor.getString(11));
contact.setCell_no(cursor.getString(12));
contact.setFax(cursor.getString(13));
contact.setEmail(cursor.getString(14));
contact.setIntercom(cursor.getString(15));
contacts.add(contact);
} while (cursor.moveToNext());
}
} catch (Exception e) {
contacts = null;
}
return contacts;
}
public List<Contact> search(String keyword) {
List<Contact> contacts = null;
try {
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + telephone + " where " + nameColumn + " like ?" , new String[]{"%" + keyword + "%"});
if (cursor.moveToFirst()) {
contacts = new ArrayList<Contact>();
do {
Contact contact = new Contact();
contact.setId(cursor.getInt(0));
contact.setPrefix(cursor.getString(1));
contact.setName(cursor.getString(2));
contact.setDesignation(cursor.getString(3));
contact.setDepartment(cursor.getString(4));
contact.setSection(cursor.getString(5));
contact.setAddress(cursor.getString(6));
contact.setStd_code(cursor.getString(7));
contact.setOd_vpn(cursor.getString(8));
contact.setOd(cursor.getString(9));
contact.setR_phone(cursor.getString(10));
contact.setR_vpn(cursor.getString(11));
contact.setCell_no(cursor.getString(12));
contact.setFax(cursor.getString(13));
contact.setEmail(cursor.getString(14));
contact.setIntercom(cursor.getString(15));
contacts.add(contact);
} while (cursor.moveToNext());
}
} catch (Exception e) {
contacts = null;
}
return contacts;
}
public boolean create(Contact contact) {
boolean result = true;
try {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(PrefixColumn, contact.getPrefix());
contentValues.put(nameColumn, contact.getName());
contentValues.put(designationColumn, contact.getDesignation());
contentValues.put(departmentColumn, contact.getDepartment());
contentValues.put(sectionColumn, contact.getSection());
contentValues.put(addressColumn, contact.getAddress());
contentValues.put(std_codeColumn, contact.getStd_code());
contentValues.put(od_vpnColumn, contact.getOd_vpn());
contentValues.put(odColumn, contact.getOd());
contentValues.put(r_phoneColumn, contact.getR_phone());
contentValues.put(r_vpnColumn, contact.getR_vpn());
contentValues.put(cell_noColumn, contact.getCell_no());
contentValues.put(faxColumn, contact.getFax());
contentValues.put(emailColumn, contact.getEmail());
contentValues.put(IntercomColumn, contact.getIntercom());
result = sqLiteDatabase.insert(telephone, null, contentValues) > 0;
} catch (Exception e) {
result = false;
}
return result;
}
}