-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttributeSearchAdapter.java
149 lines (130 loc) · 4.73 KB
/
AttributeSearchAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.puz.salestakingorder.adapters;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.puz.salestakingorder.R;
import com.puz.salestakingorder.models.AttributesItem;
public class AttributeSearchAdapter extends BaseAdapter implements Filterable{
private LayoutInflater mInflater;
private Context context;
private ArrayList<AttributesItem> attributeItems;
private ArrayList<AttributesItem> filteredItems;
private SparseBooleanArray mSelectedItemsIds;
private long selectedId=-1;
private int setPosition1= 22;
public AttributeSearchAdapter(Context context, ArrayList<AttributesItem> items){
this.context=context;
this.attributeItems=items;
this.filteredItems=items;
mSelectedItemsIds=new SparseBooleanArray();
mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return filteredItems.size();
}
@Override
public Object getItem(int position) {
return filteredItems.get(position);
}
@Override
public long getItemId(int position) {
return filteredItems.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview_attribute_search_item, parent,false);
holder=new ViewHolder();
holder.textViewProductTitle=(TextView)convertView.findViewById(R.id.textViewTitle);
holder.radioButtonSelectedProduct=(RadioButton)convertView.findViewById(R.id.radioButtonSelectedProduct);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
holder.textViewProductTitle.setText(filteredItems.get(position).getName());
holder.textViewProductTitle.setTag(filteredItems.get(position).getId());
if(filteredItems.get(position).getId() == selectedId) {
holder.radioButtonSelectedProduct.setChecked(true);
}
else{
holder.radioButtonSelectedProduct.setChecked(false);
}
return convertView;
}
private static class ViewHolder{
RadioButton radioButtonSelectedProduct;
TextView textViewProductTitle;
}
public void selectView(int position, boolean value) {
if (value)
mSelectedItemsIds.put(position, value);
else
mSelectedItemsIds.delete(position);
notifyDataSetChanged();
}
public int getSelectedCount() {
return mSelectedItemsIds.size();
}
public void toggleSelection(int position) {
selectView(position, !mSelectedItemsIds.get(position));
notifyDataSetChanged();
}
public SparseBooleanArray getSelectedIds() {
return mSelectedItemsIds;
}
public void setSelected(long id){
selectedId=id;
notifyDataSetChanged();
}
public long getSelected(){
return selectedId;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResult) {
filteredItems = (ArrayList<AttributesItem>)filterResult.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults();
if(charSequence == null || charSequence.length() == 0)
{
results.values = attributeItems;
results.count = attributeItems.size();
}
else
{
ArrayList<AttributesItem> filterResultsData = new ArrayList<AttributesItem>();
for(AttributesItem data : attributeItems)
{
if(data.getName().toLowerCase().contains(charSequence.toString().toLowerCase())
){
filterResultsData.add(data);
}
}
results.values = filterResultsData;
Log.i("filtereddata", filterResultsData.toString());
results.count = filterResultsData.size();
}
return results;
}
};
}
}