我的checkbox
在过滤搜索后更改位置,我不明白为什么
我将在列表中再检查一个已过滤的项目:
现在我将清除过滤器,结果将是这样的:
过滤器和checkbox
工作正常,只有当我过滤结果并点击checkbox
仍然在顶部?
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choosegroup);
getAllContacts(this.getContentResolver());
friends = (ListView) findViewById(R.id.listView);
ma = new MyAdapter();
friends.setAdapter(ma);
friends.setOnItemClickListener(this);
friends.setItemsCanFocus(false);
friends.setTextFilterEnabled(true);
// adding
toolbar = (Toolbar) findViewById(R.id.tool_bar4);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent i = getIntent();
groupname = i.getStringExtra("name");
search = (EditText) findViewById(R.id.editText12);
but = (Button) toolbar.findViewById(R.id.buttonz);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder checkedcontacts = new StringBuilder();
System.out.println("..." + ma.mCheckStates.size());
for (int i = 0; i < phno1.size(); i++) {
if (ma.mCheckStates.get(i) == true) {
checkedcontacts.append(phno1.get(i).toString());
checkedcontacts.append("\n");
} else {
// System.out.println("Not Checked.." + phno1.get(i).toString());
}
}
Toast.makeText(Choosegroup.this, checkedcontacts, Toast.LENGTH_LONG).show();
}
});
friends.setEmptyView(findViewById(R.id.emptyElement5));
friends.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable s) {
Log.d("tag", "*** Search value changed: " + s.toString());
ma.getFilter().filter(s.toString());
ma.notifyDataSetChanged();
}
});
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
ma.toggle(arg2);
}
public void getAllContacts(ContentResolver cr) {
String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, sortOrder);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("..."+phoneNumber);
name1.add(name);
phno1.add(phoneNumber);
}
phones.close();
}
class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener
{ private SparseBooleanArray mCheckStates;
private Context mContext;
List<String> mOriginalValues; // Original Values
LayoutInflater mInflater;
TextView tv1,tv;
CheckBox cb;
MyAdapter()
{
mCheckStates = new SparseBooleanArray();
mInflater = (LayoutInflater)Choosegroup.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return name1.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return name1.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
class ViewHolder {
protected TextView tv1,tv;
protected CheckBox cb;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView==null){
convertView = mInflater.inflate(R.layout.groupuser, null);
viewHolder = new ViewHolder();
viewHolder.tv = (TextView) convertView.findViewById(R.id.textView32);
viewHolder.tv1 = (TextView) convertView.findViewById(R.id.textView33);
viewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv.setText(name1.get(position));
viewHolder.tv1.setText(phno1.get(position));
viewHolder.cb.setTag(position);
viewHolder.cb.setChecked(mCheckStates.get(position));
viewHolder.cb.setOnCheckedChangeListener(this);
return convertView;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
System.out.println("hello...");
ma.notifyDataSetChanged();
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
public android.widget.Filter getFilter() {
return new android.widget.Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
List<String> FilteredArrList = new ArrayList<String>();
if (mOriginalValues == null)
{
System.out.println("");
mOriginalValues = new ArrayList<String>(name1); // saves the original data in mOriginalValues
}
/********
*
* 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 = mOriginalValues.size();
results.values = mOriginalValues;
}
else
{
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mOriginalValues.size(); i++)
{
String data = mOriginalValues.get(i);
if (data.toLowerCase().startsWith(constraint.toString().toLowerCase()))
{
FilteredArrList.add(mOriginalValues.get(i));
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
name1 = (List<String>) results.values; // has the filtered values
ma.notifyDataSetChanged();
}
};
}
}
}
答案 0 :(得分:1)