我正在尝试搜索ListView使用TextView,在编写代码之后,它正在记录我所做的事情,但没有过滤我希望它过滤的List。这是我的代码:
public class LabDetailActivity extends ActionBarActivity {
private LabAdapter labListAdapter;
private List<LabItem> labItem;
private TextView searchText;
private SearchView mSearchView;
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.assigned_lab_fragments);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Labs");
searchText = (TextView) findViewById(R.id.labText);
list = (ListView) findViewById(android.R.id.list);
labItem = new ArrayList<LabItem>();
labListAdapter = new LabAdapter(this, labItem);
list.setAdapter(labListAdapter);
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new ListItemClickListener());
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
/*labListAdapter.getFilter().filter(charSequence.toString());
list.setAdapter(labListAdapter);*/
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
int textLength = charSequence.length();
List<LabItem> labItemss = new ArrayList<LabItem>();
for (LabItem ll : labItem){
if(textLength <= ll.getName().length()){
if (ll.getName().toLowerCase().contains(charSequence.toString().toLowerCase())){
labItemss.add(ll);
}
}
}
Log.d("SIZE OF SEARCH", String.valueOf(labItemss.size()));
labListAdapter = new LabAdapter(LabDetailActivity.this, labItemss);
list.setAdapter(labListAdapter);
}
@Override
public void afterTextChanged(Editable editable) {
/*Log.d("NEW TAGS", "*** Search value changed: " + editable.toString());
if (editable.toString().length() >= 3)
labListAdapter.getFilter().filter(editable.toString());*/
//labListAdapter.getFilter().filter(editable.toString());
//labListAdapter.notifyDataSetChanged();
}
});
}
private class ListItemClickListener implements ListView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
}
private void RetrieveData(){
Uri uri2 = Uri.parse("content://" + getString(R.string.LEONE_AUTHORITY) + "/olabs");
Log.d("URL PRINT", uri2.toString());
final Cursor cursor = getContentResolver().query(uri2, null, null, null, "_id");
Log.d("CURSOR DATA:", cursor.toString());
JSONArray array = new JSONArray();
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
JSONObject object = new JSONObject();
try {
object.put("lab_number", cursor.getString(cursor.getColumnIndex("lab_case_number")));
object.put("lab_name", cursor.getString(cursor.getColumnIndex("lab_case_name")));
array.put(object);
Log.d("ARRAY", String.valueOf(array.length()));
} catch (JSONException e) {
e.printStackTrace();
}
}
parseJsonLabs(array);
cursor.close();
}
}
private void parseJsonLabs(JSONArray array){
Log.d("CASESARRAY", array.toString());
try {
labItem.clear();
for (int i = 0; i < array.length(); i++) {
JSONObject cases = (JSONObject) array.get(i);
LabItem items = new LabItem();
items.setName(cases.getString("lab_number"));
items.setCase(cases.getString("lab_name"));
labItem.add(items);
}
labListAdapter.notifyDataSetChanged();
} catch (JSONException e) {
// TODO: handle exception
Log.e("ARRAYTEST", "ERROR:", e);
}
}
@Override
protected void onResume() {
super.onResume();
RetrieveData();
}
}
然后,适配器代码在这里 的适配器
public class LabAdapter extends BaseAdapter implements Filterable{
private Activity activity;
private LayoutInflater inflater;
private List<LabItem> labItem;
private TextView labName, labCase;
public LabAdapter(Activity activity, List<LabItem> labItem){
this.activity = activity;
this.labItem = labItem;
}
@Override
public int getCount() {
return labItem.size();
}
@Override
public Object getItem(int position) {
return labItem.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.custom_lab_activity_list, null);
labName = (TextView) convertView.findViewById(R.id.custom_lab_name);
labCase = (TextView) convertView.findViewById(R.id.custom_lab_case);
LabItem labs = labItem.get(position);
labName.setText(labs.getName());
labCase.setText(labs.getCase());
return convertView;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
Log.d("SEARCHTEXT2", "**** PERFORM FILTERING for: " + charSequence);
charSequence = charSequence.toString().toLowerCase();
FilterResults filts = new FilterResults();
if (charSequence == null || charSequence.length() == 0){
filts.values = labItem;
filts.count = labItem.size();
} else {
// We perform filtering operation
List<LabItem> labrs = new ArrayList<LabItem>();
for (LabItem l : labrs){
if (l.getName().startsWith(charSequence.toString()) || l.getCase().startsWith(charSequence.toString()))
labItem.add(l);
}
filts.values = labItem;
filts.count = labItem.size();
//filts.count = labItem.size();
}
return filts;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
// Now we have to inform the adapter about the new list filtered
Log.d("SEARCHTEXT1", "**** PUBLISHING RESULTS for: " + charSequence);
if (filterResults.count == 0)
notifyDataSetInvalidated();
else {
labItem = (List<LabItem>) filterResults.values;
notifyDataSetChanged();
}
}
};
}
public void notifyDataSetChanged(){
super.notifyDataSetChanged();
}
}
Item类就在这里 项目类
public class LabItem {
String labCase, labName;
public LabItem() {
}
public LabItem(String labCase, String labName){
super();
this.labCase = labCase;
this.labName = labName;
}
public String getName(){
return labName;
}
public void setName(String name){
this.labName = name;
}
public String getCase(){
return labCase;
}
public void setCase(String cases){
this.labCase = cases;
}
}
我不知道我是不是很好地更新ListView或者其他东西。当我输入一个我看到计数,但如果我添加另一个字母。 LIST 将会消失。任何帮助,将不胜感激。提前谢谢。
答案 0 :(得分:1)
几个逻辑错误:我这里的代码在我这里工作
public LabAdapter(Activity activity, List<LabItem> labItem){
this.activity = activity;
this.labItem = labItem;
}
@Override
public int getCount() {
return labItem.size();
}
@Override
public LabItem getItem(int position) { //instead of Object make it Labitem
return labItem.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.activity_stack, null);
labName = (TextView) convertView.findViewById(R.id.textView1);
labCase = (TextView) convertView.findViewById(R.id.textView2);
LabItem labs = labItem.get(position);
labName.setText(labs.getName());
labCase.setText(labs.getCase());
return convertView;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
Log.d("SEARCHTEXT2", "**** PERFORM FILTERING for: " + charSequence);
charSequence = charSequence.toString().toLowerCase();
FilterResults filts = new FilterResults();
if (charSequence == null || charSequence.length() == 0){
filts.values = labItem;
filts.count = labItem.size();
} else {
// We perform filtering operation
List<LabItem> labrs = new ArrayList<LabItem>();
for (LabItem l : labItem){ //you are going to search in labItem that contains the data and not the empty list labrs
if (l.getName().startsWith(charSequence.toString()) || l.getCase().startsWith(charSequence.toString())){
Log.e("(l.getName().startsWith(charSequence.toString())","" + charSequence);
labrs.add(l); // add to the new list**
}
}
filts.values = labrs; //set values to the new list**
filts.count = labrs.size();
//filts.count = labItem.size();
}
return filts;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
// Now we have to inform the adapter about the new list filtered
Log.d("SEARCHTEXT1", "**** PUBLISHING RESULTS for: " + charSequence);
List<LabItem> filtered = (List<LabItem>) filterResults.values;
labItem = filtered; // set the new data as you want with the new set you've received.**
notifyDataSetChanged();
}
};
}
public void notifyDataSetInvalidated()
{
super.notifyDataSetInvalidated();
}
然后在 LabDetailActivity.class 中将其更改为
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
labListAdapter.getFilter().filter(charSequence.toString());
Log.d("NEW TAGS", "*** Search value changed: " + labListAdapter.getCount());
list.invalidate();
labListAdapter.notifyDataSetChanged();
list.setAdapter(labListAdapter);
}
修改:
P.S。清除文本后,此代码仍然不完美 不会将列表设置回来。我留给你。希望这能解决你的问题 你一直面临的问题。 如果有效,请告诉我。