我的ListView
有TextView
和CheckBox
。我正在通过电话联系人填充ListView
。我可以选择一些CheckBoxes
并保存更改。现在当我回到我的列表时,我想在顶部显示检查Checkbox
es的那些行。
package com.sample.demo.demolist;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import com.sample.demo.ContactLogBean;
import com.sample.demo.R;
public class AutoRecordContactListAdapter extends BaseAdapter {
private ArrayList<ContactLogBean> mPhoneContacts;
private Context mContext;
private LayoutInflater mInfalter;
public AutoRecordContactListAdapter(Context context, ArrayList<ContactLogBean> phoneContacts) {
this.mContext = context;
this.mPhoneContacts = phoneContacts;
mInfalter = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mPhoneContacts.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mInfalter.inflate(R.layout.auto_record_contacts_list_item, null);
holder = new ViewHolder();
holder.contactName = (TextView) convertView.findViewById(R.id.auto_record_contact_name);
holder.contactCheckBox = (CheckBox) convertView.findViewById(R.id.auto_record_contact_checkbox);
holder.contactCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.contactName.setText(mPhoneContacts.get(position).getmUserName());
if(mPhoneContacts.get(position).getmIsFiltered().equalsIgnoreCase("true")) {
holder.contactCheckBox.setChecked(true);
} else {
holder.contactCheckBox.setChecked(false);
}
return convertView;
}
public class ViewHolder {
TextView contactName;
CheckBox contactCheckBox;
}
}
如何在顶部显示选定CheckBox
个es的行?帮助我。
答案 0 :(得分:2)
通过
在ArrayList
mPhoneContacts 的开头添加所选联系人
mPhoneContacts.add(0, object);
并且不要忘记删除同一个对象的其中一个副本,否则它会在ListView
中显示2个条目。