[英]How to sort the listview using autocomplete textview like the default contacts listview?
I Have a doubt in my autocomplete textview. 我对自动完成的textview有疑问。 Here i am trying to filter the listview based on the text that is typed in the autocomplete textview. 在这里,我试图根据自动完成文本视图中键入的文本来过滤列表视图。 I am getting the values from json parsing. 我从json解析中获取值。 The auto complete textview shows all its values correctly in the drop down list. 自动完成文本视图会在下拉列表中正确显示其所有值。 But in the listview i see no changes in it. 但是在列表视图中,我看不到任何更改。
Below is my adapter code for the listview: 以下是我的列表视图适配器代码:
public class EventListAdapter extends BaseAdapter implements Filterable {
public static LayoutInflater inflator = null;
private ArrayList<EventsBean> mOriginalvalues = new ArrayList<EventsBean>();
private ArrayList<EventsBean> mDisplayvalues;
ImageLoader imageloader;
public String datenew,datetime,date_text_value,timenew;
public int date_text,year;
public String time,month,description;
public EventListAdapter( ArrayList<EventsBean> mEventarraylist,Activity activity) {
super();
//this.context = context;
this.mOriginalvalues = mEventarraylist;
inflator =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageloader=new ImageLoader(activity.getApplicationContext());
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mOriginalvalues.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View holder;
holder = inflator.inflate(R.layout.activity_list_items, parent, false);
TextView txt_name = (TextView)holder.findViewById(R.id.textname);
TextView txt_owner_name = (TextView)holder.findViewById(R.id.ownername);
TextView txt_time = (TextView)holder.findViewById(R.id.date);
TextView txt_date = (TextView)holder.findViewById(R.id.txt_date_value);
TextView txt_month = (TextView)holder.findViewById(R.id.txt_month_value);
TextView txt_year = (TextView)holder.findViewById(R.id.txt_year_value);
ImageView userimg = (ImageView)holder.findViewById(R.id.imageView1);
txt_name.setText(mOriginalvalues.get(position).getName());
txt_owner_name.setText(mOriginalvalues.get(position).getOwner_name());
String url = mOriginalvalues.get(position).getSource();
date_text_value = mOriginalvalues.get(position).getStart_time();
parseDateFromString(date_text_value);
txt_date.setText(String.valueOf(date_text));
txt_month.setText(month);
txt_year.setText(String.valueOf(year));
Log.i("TEST", "Date:" + date_text_value);
Log.i("TAG", "Country:" + mOriginalvalues.get(position).getCountry());
imageloader.DisplayImage(url, userimg);
txt_time.setText(timenew);
//userimg.getFitsSystemWindows();
return holder;
}
@SuppressLint("SimpleDateFormat")
public Date parseDateFromString(String aDateString){
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Calendar c = Calendar.getInstance();
Date date= new Date();
try {
date= inputFormat.parse(aDateString);
System.out.println(date);
SimpleDateFormat day = new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat time = new SimpleDateFormat("hh:mm a", Locale.getDefault());
SimpleDateFormat month_date = new SimpleDateFormat("MMM");
c.setTime(inputFormat.parse(aDateString));
System.out.println(day.format(date));
datenew = day.format(date).toString();
date_text = c.get(Calendar.DAY_OF_MONTH);
month = month_date.format(c.getTime());
year = c.get(Calendar.YEAR);
System.out.println("Year = " + c.get(Calendar.YEAR));
System.out.println("Month = " + month);
System.out.println("Day = " + date_text);
System.out.println(time.format(date));
timenew = time.format(date).toString();
} catch (ParseException e) {
Log.i("TAG", "DateFormat Pasring Error:" + e.getMessage());
}
return date;
}
@SuppressLint("DefaultLocale")
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// TODO Auto-generated method stub
mOriginalvalues = (ArrayList<EventsBean>) results.values; // has the filtered values
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// TODO Auto-generated method stub
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
ArrayList<EventsBean> FilteredArrList = new ArrayList<EventsBean>();
if (mDisplayvalues == null) {
mDisplayvalues = new ArrayList<EventsBean>(mOriginalvalues); // saves the original data in mOriginalValues
System.out.println("Display Value:" + mDisplayvalues.size());
}
/********
*
* If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
********/
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = mDisplayvalues.size();
results.values = mDisplayvalues;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mDisplayvalues.size(); i++) {
EventsBean data = mDisplayvalues.get(i);
System.out.println("Display Value 2:" + mDisplayvalues.size());
if (data.getLocation_city().toLowerCase().startsWith(constraint.toString())
|| data.getLocation_country().toLowerCase().startsWith(constraint.toString())
|| data.getLocation_state().toLowerCase().startsWith(constraint.toString()))
{
FilteredArrList.add(new EventsBean(mDisplayvalues.get(i).getLocation_city(),mDisplayvalues.get(i).getLocation_country(),
mDisplayvalues.get(i).getLocation_state()));
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
}
I am getting a nullpointer error everytime i try to write some text in my autocomplete textview in my getCount() of my adapter. 每次尝试在适配器的getCount()的自动完成文本视图中写入一些文本时,都会出现nullpointer错误。 I have 3 different values in my autocomplete textview state,country and city which are stored in the arraylist. 我的自动完成文本视图状态,国家和城市中有3个不同的值,它们存储在arraylist中。 I have seen lot of examples on net but they all have autocomplete textview values having only one value but i have three how do i filter the listview with them?? 我在网上看到了很多示例,但是它们都具有仅具有一个值的自动完成的textview值,但是我有三个如何使用它们过滤listview的?
Pleae help me in solving up my nullpointer error. 请帮助我解决我的nullpointer错误。
I could solve my problem by making the changes in the getView(). 我可以通过在getView()中进行更改来解决我的问题。 I was getting NullPointer due to wrong getView() contents. 由于getView()内容错误,我得到了NullPointer。 but it works fine now. 但现在工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.