这是我的代码我在片段中有一个列表视图,使用光标读取短信我在适配器中设置了复选框,在主布局的列表视图外面有3个按钮我需要以下内容
1.当我点击一个按钮时,它会在列表中显示复选框
2.当我选择并滚动列表时,所选项目不会消失
3.选择所有按钮以在列表中选择全部4.删除按钮以删除所选项目立即更新的剩余消息。
public class TabFragment extends Fragment
{
ListView mlistView;
ArrayList<Message> sms = new ArrayList<Message>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_sms, container, false);
mlistView = (ListView) view.findViewById(R.id.SMSList);
Button sdel=(Button)view.findViewById(R.id.sdelete);
populateMessageList();
sdel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return view;
}
public void populateMessageList() {
fetchInboxMessages();
if (fetchInboxMessages() != null) {
mlistView.setAdapter(new datalist(getActivity().getApplicationContext(),this));
}
}
public ArrayList<Message> fetchInboxMessages() {
Uri muriSms = Uri.parse("content://sms/inbox");
Cursor mcursor = getActivity().getContentResolver().query(muriSms,
new String[] { "_id", "address", "date", "body" }, null, null,
null);
mcursor.moveToFirst();
while (mcursor.moveToNext()) {
Message mMessage = new Message();
mMessage.setmAddress(mcursor.getString(mcursor
.getColumnIndex("address")));
mMessage.setmBody(mcursor.getString(mcursor.getColumnIndex("body")));
mMessage.setmDate(mcursor.getString(mcursor.getColumnIndex("date")));
sms.add(mMessage);
}
return sms;
}
class datalist extends BaseAdapter {
LayoutInflater inflater = null;
Boolean selected;
Boolean[] checkboxstate;
public datalist(Context applicationContext, TabFragment tabFragment) {
inflater = LayoutInflater.from(applicationContext);
} public datalist(Context applicationContext, int simple_list_item_multiple_choice, ArrayList<Message> sms) {
}
@Override
public int getCount() {
return sms.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row;
row = inflater.inflate(R.layout.sms_adapter, parent, false);
ImageView img1 = (ImageView) row.findViewById(R.id.icon2);
TextView txt1 = (TextView) row.findViewById(R.id.text1);
TextView txt2 = (TextView) row.findViewById(R.id.text2);
CheckBox cb=(CheckBox)row.findViewById(R.id.check);
Long timestamp = Long.parseLong(sms.get(position).getmDate());
Calendar mcalendar = Calendar.getInstance();
mcalendar.setTimeInMillis(timestamp);
DateFormat mformatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
txt1.setText(sms.get(position).getmBody());
txt2.setText("Sent by" + sms.get(position).getmAddress() + "\n" + mformatter.format(mcalendar.getTime()));
/* if(selected==false)
cb.setVisibility(row.GONE);
else*/
cb.setVisibility(row.VISIBLE);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
sms.get(position).setSelected(isChecked);
if(isChecked){
buttonView.setSelected(true);
}else {
buttonView.setSelected(false);
}
}
});
return row;
}
}
public class Message {
private String mAddress;
private String mBody;
private String mDate;
private int icon;
boolean selected;
public void setmAddress(String mAddress) {
this.mAddress = mAddress;
}
public void setmBody(String mBody) {
this.mBody = mBody;
}
public void setmDate(String mDate) {
this.mDate = mDate;
}
public String getmBody() {
return mBody;
}
public String getmAddress() {
return mAddress;
}
public String getmDate() {
return mDate;
}
public boolean isSelected() {
return selected;
}
public int getIcon(){
return icon;} public void setSelected(boolean selected) {
this.selected = selected;
}}}