mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-03 18:56:44 -04:00
Android text search now displays recent searches from inital database implementation
This commit is contained in:
parent
fdf3ff1f40
commit
c83655d29c
@ -1,5 +1,6 @@
|
||||
package org.kiwix.kiwixmobile;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.view.MenuItemCompat;
|
||||
@ -10,14 +11,26 @@ import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.kiwix.kiwixmobile.utils.DatabaseHelper;
|
||||
import org.kiwix.kiwixmobile.views.AutoCompleteAdapter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class SearchActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
|
||||
|
||||
private ListView mListView;
|
||||
|
||||
private AutoCompleteAdapter mAdapter;
|
||||
private AutoCompleteAdapter mAutoAdapter;
|
||||
|
||||
private ArrayAdapter<String> mDefaultAdapter;
|
||||
|
||||
private SearchActivity context;
|
||||
|
||||
private DatabaseHelper mDatabaseHelper;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -28,10 +41,17 @@ public class SearchActivity extends AppCompatActivity implements AdapterView.OnI
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
getSupportActionBar().setHomeButtonEnabled(true);
|
||||
|
||||
|
||||
mListView = (ListView) findViewById(R.id.search_list);
|
||||
mAdapter = new AutoCompleteAdapter(this);
|
||||
mListView.setAdapter(mAdapter);
|
||||
mListView.setOnItemClickListener(this);
|
||||
mDatabaseHelper = new DatabaseHelper(this);
|
||||
ArrayList<String> a = mDatabaseHelper.getRecentSearches();
|
||||
mDefaultAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
|
||||
mListView.setAdapter(mDefaultAdapter);
|
||||
mDefaultAdapter.addAll(a);
|
||||
mDefaultAdapter.notifyDataSetChanged();
|
||||
context = this;
|
||||
mAutoAdapter = new AutoCompleteAdapter(context);
|
||||
mListView.setOnItemClickListener(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -57,22 +77,28 @@ public class SearchActivity extends AppCompatActivity implements AdapterView.OnI
|
||||
|
||||
@Override
|
||||
public boolean onQueryTextChange(String s) {
|
||||
mAdapter.getFilter().filter(s);
|
||||
if (s.equals("")) {
|
||||
mListView.setAdapter(mDefaultAdapter);
|
||||
} else {
|
||||
mListView.setAdapter(mAutoAdapter);
|
||||
mAutoAdapter.getFilter().filter(s);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
MenuItemCompat.setOnActionExpandListener(searchMenuItem,
|
||||
new MenuItemCompat.OnActionExpandListener() {
|
||||
@Override
|
||||
public boolean onMenuItemActionExpand(MenuItem item) {
|
||||
return false;
|
||||
}
|
||||
new MenuItemCompat.OnActionExpandListener() {
|
||||
@Override
|
||||
public boolean onMenuItemActionExpand(MenuItem item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMenuItemActionCollapse(MenuItem item) {
|
||||
finish();
|
||||
return true;
|
||||
@Override
|
||||
public boolean onMenuItemActionCollapse(MenuItem item) {
|
||||
finish();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return true;
|
||||
@ -80,7 +106,8 @@ public class SearchActivity extends AppCompatActivity implements AdapterView.OnI
|
||||
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
String title = mAdapter.getItemRaw(position);
|
||||
String title = ((TextView) view).getText().toString();
|
||||
mDatabaseHelper.insertSearch(title);
|
||||
sendMessage(title);
|
||||
}
|
||||
|
||||
|
87
src/org/kiwix/kiwixmobile/utils/DatabaseHelper.java
Normal file
87
src/org/kiwix/kiwixmobile/utils/DatabaseHelper.java
Normal file
@ -0,0 +1,87 @@
|
||||
package org.kiwix.kiwixmobile.utils;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.database.DatabaseUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
|
||||
public class DatabaseHelper extends SQLiteOpenHelper {
|
||||
|
||||
public static final String DATABASE_NAME = "Kiwix.db";
|
||||
public static final String CONTACTS_TABLE_NAME = "recentsearches";
|
||||
public static final String CONTACTS_COLUMN_ID = "id";
|
||||
public static final String CONTACTS_COLUMN_SEARCH = "search";
|
||||
|
||||
public DatabaseHelper(Context context)
|
||||
{
|
||||
super(context, DATABASE_NAME , null, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
// TODO Auto-generated method stub
|
||||
db.execSQL(
|
||||
"create table " + CONTACTS_TABLE_NAME +
|
||||
" (id integer primary key, search text)"
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
// TODO Auto-generated method stub
|
||||
db.execSQL("DROP TABLE IF EXISTS " + CONTACTS_TABLE_NAME);
|
||||
onCreate(db);
|
||||
}
|
||||
|
||||
public boolean insertSearch (String search)
|
||||
{
|
||||
SQLiteDatabase db = this.getWritableDatabase();
|
||||
ContentValues contentValues = new ContentValues();
|
||||
contentValues.put(CONTACTS_COLUMN_SEARCH, search);
|
||||
db.insert(CONTACTS_TABLE_NAME, null, contentValues);
|
||||
return true;
|
||||
}
|
||||
|
||||
public Cursor getData(int id){
|
||||
SQLiteDatabase db = this.getReadableDatabase();
|
||||
Cursor res = db.rawQuery( "select * from " + CONTACTS_TABLE_NAME + " where id="+id+"", null );
|
||||
return res;
|
||||
}
|
||||
|
||||
public int numberOfRows(){
|
||||
SQLiteDatabase db = this.getReadableDatabase();
|
||||
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
|
||||
return numRows;
|
||||
}
|
||||
|
||||
public Integer deleteSearches (Integer id)
|
||||
{
|
||||
SQLiteDatabase db = this.getWritableDatabase();
|
||||
return db.delete(CONTACTS_TABLE_NAME,
|
||||
"id = ? ",
|
||||
new String[] { Integer.toString(id) });
|
||||
}
|
||||
|
||||
public ArrayList<String> getRecentSearches()
|
||||
{
|
||||
ArrayList<String> array_list = new ArrayList<String>();
|
||||
|
||||
//hp = new HashMap();
|
||||
SQLiteDatabase db = this.getReadableDatabase();
|
||||
Cursor res = db.rawQuery( "select * from " + CONTACTS_TABLE_NAME, null );
|
||||
res.moveToLast();
|
||||
|
||||
while(res.isBeforeFirst() == false){
|
||||
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_SEARCH)));
|
||||
res.moveToPrevious();
|
||||
}
|
||||
return array_list;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user