gpt4 book ai didi

android - TextChangedListener 没有做任何事情

转载 作者:行者123 更新时间:2023-11-30 00:16:09 25 4
gpt4 key购买 nike

我正在尝试使用 TextChangedListener 在我的 ListView 上实现搜索功能。但是在 EditText 中添加了一些字符之后; ListView 变为空白。我在我的 ArrayAdapter 类中实现了过滤方法。

我正在从 JSON 获取数据。

这是我的代码:

用户列表.java

public class UserList extends AppCompatActivity {

private ListView listView;
private ArrayList<MyDataModel> list;
private MyArrayAdapter adapter;
private EditText search;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_list);

search = (EditText) findViewById(R.id.search);

//Array List for Binding Data from JSON to this List
list = new ArrayList<>();

//Binding that List to Adapter
adapter = new MyArrayAdapter(this, list);

//Getting List and Setting List Adapter
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);

listView.setTextFilterEnabled(true);
search.addTextChangedListener(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

String text = search.getText().toString().toLowerCase(Locale.getDefault());
adapter.filter(text);
}

@Override
public void afterTextChanged(Editable s) {
}
});


//Checking Internet Connection
if (InternetConnection.checkConnection(getApplicationContext())) {
new GetDataTask().execute();
} else {
Snackbar.make(findViewById(R.id.parentLayout),"Internet Connection Not Available", Snackbar.LENGTH_LONG).show();
}
}

//Creating Get Data Task for Getting Data From Web
class GetDataTask extends AsyncTask<Void, Void, Void> {

ProgressDialog dialog;
int jIndex;
int x;

@Override
protected void onPreExecute() {
super.onPreExecute();
//Progress Dialog for User Interaction
x=list.size();

if(x==0)
jIndex=0;
else
jIndex=x;

dialog = new ProgressDialog(UserList.this);
dialog.setTitle("Please Wait..."+x);
dialog.setMessage("Retrieving Data");
dialog.show();

}

@Nullable
@Override
protected Void doInBackground(Void... params) {

//Getting JSON Object from Web Using okHttp
JSONObject jsonObject = JSONParser.getDataFromWeb();

try {

if (jsonObject != null) {

if(jsonObject.length() > 0) {

JSONArray array = jsonObject.getJSONArray(Keys.KEY_CONTACTS);

//Check Length of Array...
int lenArray = array.length();
if(lenArray > 0) {
for( ; jIndex < lenArray; jIndex++) {

//Creating Every time New Object and adding to List
MyDataModel model = new MyDataModel();

JSONObject innerObject = array.getJSONObject(jIndex);
String name = innerObject.getString(Keys.KEY_NAME);

model.setName(name);
list.add(model);
}
}
}
} else {

}
} catch (JSONException je) {
Log.i(JSONParser.TAG, "" + je.getLocalizedMessage());
}
return null;
}


@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
dialog.dismiss();

//Checking if List size if more than zero then update ListView
if(list.size() > 0) {
adapter.notifyDataSetChanged();

} else {
Snackbar.make(findViewById(R.id.parentLayout), "No Data Found", Snackbar.LENGTH_LONG).show();
}

}
}
}

我已经在我的 ArrayAdapter 类中实现了 filter 方法。

这是我的 ArrayAdapter 类:

MyArrayAdapter.java

public class MyArrayAdapter extends ArrayAdapter<MyDataModel> implements Filterable{

List<MyDataModel> modelList;
Context context;
private LayoutInflater mInflater;
private ArrayList<MyDataModel> arrayList;

public MyArrayAdapter(Context context, List<MyDataModel> objects) {
super(context, 0, objects);
this.context = context;
this.mInflater = LayoutInflater.from(context);
modelList = objects;

this.arrayList = new ArrayList<MyDataModel>();
this.arrayList.addAll(modelList);
}

@Override
public MyDataModel getItem(int position) {
return modelList.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder vh;
if (convertView == null) {
View view = mInflater.inflate(R.layout.layout_row_view, parent, false);
vh = ViewHolder.create((RelativeLayout) view);
view.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}

MyDataModel item = getItem(position);

vh.textViewName.setText(item.getName());
return vh.rootView;
}


private static class ViewHolder {
public final RelativeLayout rootView;

public final TextView textViewName;

private ViewHolder(RelativeLayout rootView, TextView textViewName) {
this.rootView = rootView;
this.textViewName = textViewName;
}

public static ViewHolder create(RelativeLayout rootView) {
TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
return new ViewHolder(rootView, textViewName);
}
}

// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
modelList.clear();
if (charText.length() == 0) {
modelList.addAll(arrayList);
} else {
for (MyDataModel wp : arrayList) {
if (wp.getName().toLowerCase(Locale.getDefault()).contains(charText)) {
modelList.add(wp);
}
}
}
notifyDataSetChanged();
}

最佳答案

需要再创建一个arryList来过滤数据。

public class MyArrayAdapter extends ArrayAdapter<MyDataModel> implements Filterable{

List<MyDataModel> modelList;
Context context;
private LayoutInflater mInflater;
private ArrayList<MyDataModel> arrayList;

public MyArrayAdapter(Context context, List<MyDataModel> objects) {
super(context, 0, objects);
this.context = context;
this.mInflater = LayoutInflater.from(context);
modelList = objects;
this.arrayList = new ArrayList<MyDataModel>();
this.arraylist.addAll(modelList);
}

现在在 Filter 方法中你必须在 arrayList 的基础上进行比较

// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
modelList.clear();
if (charText.length() == 0) {
modelList.addAll(arrayList);
} else {
for (MyDataModel wp : arrayList) {
if
(wp.getName().toLowerCase(Locale.getDefault()).contains(charText)) {
modelList.add(wp);
}
}
}
notifyDataSetChanged();
}

关于android - TextChangedListener 没有做任何事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47254831/

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