मैं एंड्रॉइड पर नौसिखिया हूं।कस्टम एडाप्टर फ़िल्टरिंग IndexOutOfBoundsException
फ़िल्टरिंग के दौरान मेरा कस्टम एडाप्टर अपवाद का कारण बनता है।
यहां मेरा कोड है। निजी वर्ग DeptAdapter फैली ArrayAdapter लागू करता फिल्टर {
private ArrayList<Dept> items;
private ArrayList<Dept> mOriginalValues;
public DeptAdapter(Context context, int textViewResourceId, ArrayList<Dept> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.item_listview_2line, null);
}
Dept d = items.get(position);
if (d != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
if (tt != null){
tt.setText(d.dept_nm);
}
if(bt != null){
bt.setText(d.dept_cd);
}
}
return v;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
items = (ArrayList<Dept>) results.values; // has the filtered values
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
ArrayList<Dept> FilteredArrList = new ArrayList<Dept>();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<Dept>(items); // saves the original data in mOriginalValues
}
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mOriginalValues.size(); i++) {
Dept d = mOriginalValues.get(i);
if (d.dept_cd.toLowerCase().startsWith(constraint.toString()) || d.dept_nm.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(d);
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
}
class Dept
{
String dept_cd;
String dept_nm;
public Dept(String dept_cd, String dept_nm)
{
this.dept_cd = dept_cd;
this.dept_nm = dept_nm;
}
public String toString()
{
return this.dept_nm+ "(" + this.dept_cd +")" ;
}
}
मुझे मदद किसी को भी .... मैं नहीं समझ सकता क्यों getView() विधि तो items.size()
कृपया मैं भी दो तरीकों में से कोड चाहते getCount() और GetItem (पूर्णांक स्थिति DeptAdapter वर्ग और logcat उत्पादन –
की पूरी कोड जोड़) –