我有一个listview被顶部的编辑文本过滤。但是当我点击搜索的项目时,我得到另一个项目被选中的结果。还有一些数据存储在我的数组中,这在列表视图中是不可见的,也会返回错误的数据。 在搜索C时,您将结果过滤为C.但是单击它结果就像位置不会更改其他数据但使用原始ArrayList适配器位置。
任何帮助将不胜感激。
我的项目:
public class Leave_master_items {
public String leave_id;
public String leave_Name;
public String is_hourly;
public Leave_master_items(String leave_Name) {
this.leave_Name = leave_Name;
}
public String getLeave_id() {
return leave_id;
}
public void setLeave_id(String leave_id) {
this.leave_id = leave_id;
}
public String getLeave_Name() {
return leave_Name;
}
public void setLeave_Name(String leave_Name) {
this.leave_Name = leave_Name;
}
public String getIs_hourly() {
return is_hourly;
}
public void setIs_hourly(String is_hourly) {
this.is_hourly = is_hourly;
}
}
我的适配器:
public class Leave_name_adapter extends BaseAdapter implements Filterable{
ArrayList<Leave_master_items> cm = new ArrayList<>();
Context context;
ArrayList<Leave_master_items> mStringFilterList;
ValueFilter valueFilter;
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
// return null;
}
static class ListViewHolder {
TextView leaveType;
}
public Leave_name_adapter(ArrayList<Leave_master_items> cm, Context context) {
this.cm = cm;
this.context = context;
mStringFilterList = cm;
}
@Override
public int getCount() {
return cm.size();
}
@Override
public Object getItem(int position) {
return cm.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ListViewHolder viewHolder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.leaves_listview, parent, false);
viewHolder = new ListViewHolder();
viewHolder.leaveType = (TextView) row.findViewById(R.id.leavestype);
row.setTag(viewHolder);
} else {
viewHolder = (ListViewHolder) row.getTag();
}
Leave_master_items l = (Leave_master_items) getItem(position);
viewHolder.leaveType.setText(l.getLeave_Name());
return row;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// cm.clear();
if (constraint != null && constraint.length() > 0) {
ArrayList<Leave_master_items> filterList = new ArrayList<Leave_master_items>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ((mStringFilterList.get(i).getLeave_Name().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Leave_master_items country = new Leave_master_items(mStringFilterList.get(i).getLeave_Name());
filterList.add(country);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
cm = (ArrayList<Leave_master_items>) results.values;
notifyDataSetChanged();
}
}
}
我的活动:
leave_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Leave_list().execute();
a.setVisibility(View.INVISIBLE);
b.setVisibility(View.VISIBLE);
v.setBackgroundResource(R.drawable.date_button_bg);
date.setBackgroundResource(R.drawable.leave_button_bg);
leave_btn.setTextColor(Color.parseColor("#293038"));
date.setTextColor(Color.parseColor("#F8F8F8"));
search_leave.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Leave_application.this.leave_name_adapter.getFilter().filter(arg0);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
});
private class Leave_list extends AsyncTask<String, Void, Boolean> {
protected void onPreExecute() {
leave_master_itemses.clear();
}
@Override
protected void onPostExecute(final Boolean success) {
leave_name_adapter=new Leave_name_adapter(leave_master_itemses,getApplicationContext());
listview_leave.setAdapter(leave_name_adapter);
listview_leave.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object a = parent.getAdapter().getItem(position);
String b = (String) parent.getItemAtPosition(position);
Leave_master_items leave_master_items=leave_master_itemses.get(position);
selectedFromList=leave_master_items.getLeave_id();
Log.d("Yourtag", selectedFromList);
// Log.d("Clicked item field", " "+ item.getColum(your colum index));
}
});
}
protected Boolean doInBackground(final String... args) {
try {
Login_json_parser jParser = new Login_json_parser();
String s = session.isWEBURL()+"?Function=" + session.isKEY_USERNAME() + "&Emp=" + session.iskey_emp_id();
Log.d("s", s);
JSONObject json1 = jParser.getJSONFromUrl(s);
Log.d("Inputfdfdtsale Stream", "json1");
try {
JSONObject object = (JSONObject) new JSONTokener(json1.toString()).nextValue();
Leave = object.getString("Leave");
Log.d("ew", Leave);
JSONArray jsonArray = new JSONArray(Leave);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = new JSONObject();
jsonObject=jsonArray.getJSONObject(i);
Leave_master_items leave_master_items = new Leave_master_items(null);
leave_master_items.setIs_hourly(jsonObject.getString("is_hourly"));
leave_master_items.setLeave_id(jsonObject.getString("leave_id"));
leave_master_items.setLeave_Name(jsonObject.getString("leave_Name"));
leave_master_itemses.add(leave_master_items);
}
}
catch(JSONException e){
e.printStackTrace();
}
}
catch (Exception e1) {
e1.printStackTrace();
}
return null;
}
}
答案 0 :(得分:0)
这里的问题是,当您开始在edittext中搜索某些内容时,您的列表内容会发生变化,您可能会使用旧列表来获取该项目。
在适配器类中为过滤后的列表创建一个setter,并在onItemCLick中使用该列表来获取要获取的位置。
答案 1 :(得分:0)
您必须要求适配器获取正确的对象。
而不是
Leave_master_items leave_master_items = leave_master_itemses.get(position);
尝试
Leave_master_items leave_master_items = listview_leave.getAdapter().getItem(position);
<强>更新强>
过滤后,您不必创建具有相同内容的新对象。只需使用已存在的对象。
而不是
Leave_master_items country = new Leave_master_items(mStringFilterList.get(i).getLeave_Name());
filterList.add(country);
待办事项
filterList.add(mStringFilterList.get(i));
<强> UPDATE2 强>
不要在getItemId()
@Override
public long getItemId(int position) {
return position;
}