我有这样的问题。我有一个ListView,首先我使用适配器从SQLIte数据库填充它。然后我实现搜索,并希望将新的搜索数据放入此ListView(方法doMySearch),但我得到相同的结果 - 数据未更新。可以做些什么来解决它?这是代码:
package com.example.citycode;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends ActionBarActivity {
ListView lvData,lvData1;
DBHelper db;
SimpleCursorAdapter scAdapter;
Cursor cursor;
Cursor cursor1;
final String LOG_TAG = "myLogs";
final String LOG_TAG1 = "myLogs1";
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// открываем подключение к БД
db = new DBHelper(this);
db.open();
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
// получаем курсор
cursor = db.getAllData();
startManagingCursor(cursor);
// Формируем столбцы сопоставления
String[] from = new String[] { DBHelper.COLUMN_r_name, DBHelper.COLUMN_region, DBHelper.COLUMN_code};
int[] to = new int[] { R.id.city_name, R.id.region_name, R.id.city_code };
// создааем адаптер и настраиваем список
scAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from, to);
lvData = (ListView) findViewById(R.id.listdata);
lvData.setAdapter(scAdapter);
}
public void doMySearch(String query) {
lvData1 = (ListView) findViewById(R.id.listdata);
//Ищем совпадения
Log.d(LOG_TAG, "Before get the C");
cursor1 = db.fetchRecordsByQuery(query);
Log.d(LOG_TAG, "After get the C");
String t = Integer.toString(cursor1.getCount());
Log.d(LOG_TAG, t);
startManagingCursor(cursor1);
String[] from = new String[] { DBHelper.COLUMN_r_name, DBHelper.COLUMN_region, DBHelper.COLUMN_code};
int[] to = new int[] { R.id.city_name, R.id.region_name, R.id.city_code };
SimpleCursorAdapter scAdapter1;
scAdapter = new SimpleCursorAdapter(this,
R.layout.item, cursor1, from, to);
Log.d(LOG_TAG, "Setting the adapter");
//Обновляем адаптер
logCursor(cursor1);
lvData1.setAdapter(scAdapter);
Log.d(LOG_TAG, "Adapter was set");
}
//вывод в лог данных из курсора
void logCursor(Cursor c) {
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_TAG1, str);
} while (c.moveToNext());
}
} else
Log.d(LOG_TAG1, "Cursor is null");
}
@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);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_settings).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
protected void onDestroy() {
super.onDestroy();
// закрываем подключение при выходе
db.close();
}
}
答案 0 :(得分:0)
你试过scAdapter.notifyDataSetChanged();
吗?只要您希望视图反映数据中的更新,这就是常见的调用。
答案 1 :(得分:0)
目前,您将通过调用db.getAllData()
覆盖搜索结果。
由于doMySearch
几乎与onCreate
相同,请尝试删除doMySearch
并添加以下代码(替换onCreate
,添加onNewIntent
和{ {1}},执行搜索或以其他方式加载所有数据。
loadData
答案 2 :(得分:0)
尝试删除lvData1
并使用lvData
中的doMySearch
,然后删除:
lvData1 = (ListView) findViewById(R.id.listdata);
findViewById
中你已经onCreated
了。
答案 3 :(得分:0)
您可以尝试这样,
runOnUiThread(new Runnable() {
public void run() {
scAdapter.notifyDataSetChanged()
}
});
尝试这个,我评论了一些行“//评论Tharaka”并添加了一些行“// DO Tharaka”
public class MainActivity extends ActionBarActivity {
ListView lvData,lvData1;
DBHelper db;
SimpleCursorAdapter scAdapter;
Cursor cursor;
Cursor cursor1;
final String LOG_TAG = "myLogs";
final String LOG_TAG1 = "myLogs1";
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// открываем подключение к БД
db = new DBHelper(this);
db.open();
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
// получаем курсор
cursor = db.getAllData();
startManagingCursor(cursor);
// Формируем столбцы сопоставления
// Comment Tharaka String[] from = new String[] { DBHelper.COLUMN_r_name, DBHelper.COLUMN_region, DBHelper.COLUMN_code};
// Comment Tharaka int[] to = new int[] { R.id.city_name, R.id.region_name, R.id.city_code };
// создааем адаптер и настраиваем список
// Comment Tharaka scAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from, to);
// Comment Tharaka lvData = (ListView) findViewById(R.id.listdata);
// Comment Tharaka lvData.setAdapter(scAdapter);
}
public void doMySearch(String query) {
lvData1 = (ListView) findViewById(R.id.listdata);
//Ищем совпадения
Log.d(LOG_TAG, "Before get the C");
cursor1 = db.fetchRecordsByQuery(query);
Log.d(LOG_TAG, "After get the C");
String t = Integer.toString(cursor1.getCount());
Log.d(LOG_TAG, t);
startManagingCursor(cursor1);
String[] from = new String[] { DBHelper.COLUMN_r_name, DBHelper.COLUMN_region, DBHelper.COLUMN_code};
int[] to = new int[] { R.id.city_name, R.id.region_name, R.id.city_code };
SimpleCursorAdapter scAdapter1;
scAdapter = new SimpleCursorAdapter(this,
R.layout.item, cursor1, from, to);
//DO Tharaka
lvData.setAdapter(scAdarpter);
Log.d(LOG_TAG, "Setting the adapter");
//Обновляем адаптер
logCursor(cursor1);
lvData1.setAdapter(scAdapter);
Log.d(LOG_TAG, "Adapter was set");
}
//вывод в лог данных из курсора
void logCursor(Cursor c) {
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_TAG1, str);
} while (c.moveToNext());
}
} else
Log.d(LOG_TAG1, "Cursor is null");
}
@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);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_settings).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
protected void onDestroy() {
super.onDestroy();
// закрываем подключение при выходе
db.close();
}
}