gpt4 book ai didi

android - 使用 EditText 搜索 ListView

转载 作者:行者123 更新时间:2023-11-30 02:34:29 25 4
gpt4 key购买 nike

我正在尝试通过使用 TextView 的 ListView 进行搜索,在编写代码后,它正在注销我所做的事情,但没有过滤我希望它过滤的列表。这是我的代码:

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();
}
}

那么,Adapter代码就在这里适配器

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 还是什么。当我输入 a 时,我会看到计数,但如果我添加另一个字母。 LIST 将消失。任何帮助,将不胜感激。提前致谢。

最佳答案

一些逻辑错误:我得到了这段代码在我这边工作

    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. This code is still not perfect as after clearing the text it doesn't sets the list back. I leave it to you. Hope this solves your problem you've been facing. Let me know if it works.

关于android - 使用 EditText 搜索 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26794763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com