我的主ListView
中有一个Activity
,ActionBar
中有一个搜索选项。但是当我尝试搜索某些内容时,它会给我一个错误的列表。
这是我的mainActivity类:
public class MainActivity extends AppCompatActivity{
FloatingActionButton fab;
DBHelper myDb;
ArrayList<AccountDetails> rList;
ListView listView;
RemainderAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
fab = (FloatingActionButton) findViewById(R.id.fab);
listView = (ListView) findViewById(R.id.listView);
myDb = new DBHelper(this);
showViews();
onClickViews();
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ActivityFour.class));
}
});
}
private void onClickViews() {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int po, long id) {
Intent i = new Intent(MainActivity.this, ActivityFive.class);
i.putExtra("position", po);
startActivity(i);
}
});
}
private void showViews() {
rList = new ArrayList<AccountDetails>();
rList = myDb.getAllRows();
if (rList.size() != 0) {
adapter = new RemainderAdapter(this, rList);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
} else {
Toast.makeText(this,"There is no item in the list",Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_item,menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String newText)
{
Toast.makeText(getApplicationContext(), "TextChange..." + newText, Toast.LENGTH_SHORT).show();
adapter.getFilter().filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
Toast.makeText(getApplicationContext(), "SUBMIT..." + query, Toast.LENGTH_SHORT).show();
invalidateOptionsMenu();
adapter.getFilter().filter(query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
return super.onCreateOptionsMenu(menu);
}}
//这是我的DBHelper类
public class DBHelper extends SQLiteOpenHelper{
private static final String DB_NAME = "my_db";
private static final String TABLE_NAME = "reminders_web";
private static final int DATABASE_VERSION = 1;
public static final String KEY_ROWID = "_id";
public static final String ACC_NAME = "a_name";
public static final String USER_NAME = "u_name";
public static final String PWD = "password";
public static final String WEB = "w_site";
public static final String DESC = "desc";
private SQLiteDatabase db;
public static final String CREATE_TABLE="CREATE TABLE " + TABLE_NAME + "("
+ KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT," + ACC_NAME + " TEXT,"+USER_NAME + " TEXT,"
+ PWD + " TEXT," + WEB + " TEXT," + DESC + " TEXT "+ ")";
public DBHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION );
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String a_name,String u_name,String password,String web,String desc) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ACC_NAME,a_name);
contentValues.put(USER_NAME,u_name);
contentValues.put(PWD, password);
contentValues.put(WEB, web);
contentValues.put(DESC, desc);
long result=db.insert(TABLE_NAME,null,contentValues);
db.close();
if(result==-1)
return false;
else
return true;
}
public ArrayList<AccountDetails> getAllRows(){
ArrayList<AccountDetails> rList = new ArrayList<AccountDetails>();
SQLiteDatabase db = this.getReadableDatabase();
String[] col = new String[]{KEY_ROWID,ACC_NAME,USER_NAME,PWD,WEB,DESC};
Cursor cursor = db.query(TABLE_NAME, col, null, null, null, null, ACC_NAME);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
AccountDetails details = new AccountDetails();
details.setaName(cursor.getString(cursor.getColumnIndex(ACC_NAME)));
details.setuName(cursor.getString(cursor.getColumnIndex(USER_NAME)));
details.setPwd(cursor.getString(cursor.getColumnIndex(PWD)));
details.setwSite(cursor.getString(cursor.getColumnIndex(WEB)));
details.setDesc(cursor.getString(cursor.getColumnIndex(DESC)));
rList.add(details);
} while (cursor.moveToNext());
}
db.close();
return rList;
}
public boolean editRow(int id,String a_name, String u_name,String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ACC_NAME,a_name);
contentValues.put(USER_NAME,u_name);
contentValues.put(PWD, password);
long result=db.update(TABLE_NAME, contentValues, KEY_ROWID + "="+id, null);
db.close();
if(result==-1)
return false;
else
return true;
}
public void deleteRow(AccountDetails rDetails) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, ACC_NAME + " = ?", new String[]{String.valueOf(rDetails.getaName())});
db.close();
}}
//这是我的自定义适配器类
public class RemainderAdapter extends BaseAdapter implements Filterable{
private Context context;
int i=0;
private ArrayList<AccountDetails> rList;
private TextView aName,uName,circle;
ArrayList<AccountDetails> suggArray= new ArrayList<AccountDetails>();
private ArrayList<AccountDetails> originalData = null;
RemainderAdapter(Context context, ArrayList<AccountDetails> rList) {
this.context = context;
this.rList = rList;
this.suggArray = rList;
this.originalData=rList;
}
@Override
public int getCount() {
return suggArray.size();
}
@Override
public Object getItem(int position) {
return suggArray.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
aName = (TextView) convertView.findViewById(R.id.aName);
uName = (TextView) convertView.findViewById(R.id.uName);
circle=(TextView) convertView.findViewById(R.id.circle);
}
aName.setText(suggArray.get(position).getaName().toString());
uName.setText(suggArray.get(position).getuName().toString());
circle.setText(suggArray.get(position).getaName().toString().substring(0, 1));
return convertView;
}
public Filter getFilter() {
return mFilter;
}
public Filter mFilter = new Filter() {
@Override
protected FilterResults performFiltering(final CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final ArrayList<AccountDetails> nlist = new ArrayList<AccountDetails>();
String filterableString ;
for (AccountDetails accountDetails : rList ) {
filterableString = accountDetails.getaName().toString();
if (filterableString.toLowerCase().contains(filterString)) {
Log.e("Values", "Model Value : "+filterableString + " , String Value : " + constraint);
nlist.add(accountDetails);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
suggArray = (ArrayList<AccountDetails>) results.values;
notifyDataSetChanged();
}
};
}
答案 0 :(得分:0)
这里有一些逻辑问题。
rList
。您需要为此声明一个单独的建议清单。final ArrayList<AccountDetails> nlist = new ArrayList<AccountDetails>(originalData.size());
i
不应该是有效的位置。 originalData.get(i)
; 所以它会是这样的。
解决方案
第一
清除ArrayList
的新AccountDetails
。 ArrayList<AccountDetails> suggArray= new ArrayList<AccountDetails>();
Al将rList
替换为suggArray
,以适应适配器中的任何位置(构造函数除外)。
第二次
ArrayList<AccountDetails> suggArray= new ArrayList<AccountDetails>();
<强>第三强>
像这样改变循环。
for (AccountDetails accountDetails : rList ) {
filterableString = accountDetails.getaName().toString();
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(new AccountDetails(accountDetails.getaName().toString(),accountDetails.getuName().toString(),
accountDetails.getPwd().toString(),accountDetails.getwSite().toString(),accountDetails.getDesc().toString()));
}
}
最后它看起来像这样。
public class RemainderAdapter extends BaseAdapter implements Filterable{
private Context context;
private ArrayList<AccountDetails> rList;
private TextView aName,uName;
ArrayList<AccountDetails> suggArray= new ArrayList<AccountDetails>();
private ArrayList<AccountDetails> originalData = null;
RemainderAdapter(Context context, ArrayList<AccountDetails> rList) {
this.context = context;
this.rList = rList;
this.suggArray = rList;
this.originalData=rList;
}
@Override
public int getCount() {
return suggArray.size();
}
@Override
public Object getItem(int position) {
return suggArray.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
aName = (TextView) convertView.findViewById(R.id.aName);
uName = (TextView) convertView.findViewById(R.id.uName);
}
aName.setText(suggArray.get(position).getaName().toString());
uName.setText(suggArray.get(position).getuName().toString());
return convertView;
}
public Filter getFilter() {
return mFilter;
}
public Filter mFilter = new Filter() {
@Override
protected FilterResults performFiltering(final CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final ArrayList<AccountDetails> nlist = new ArrayList<AccountDetails>();
String filterableString ;
for (AccountDetails accountDetails : rList ) {
filterableString = accountDetails.getaName().toString();
Log.e("Values","Model Value : "filterableString +" , String Value : "+constraint); // Check Here
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(accountDetails);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
suggArray = (ArrayList<AccountDetails>) results.values;
notifyDataSetChanged();
}
};
}