我有一个自定义CursorAdapter,可以从我的数据库中检索数据并将其设置为listiew。 Listview有4列和复选框。我单击复选框以在列表视图中选择项目的ID(在我的适配器中使用setOnCheckedChangeListener)。并将选定的ID存储在Set中,该ID由Activity创建。 我需要在属于Activity(在onClick方法中)的按钮中使用这些保存的id来发送短信,电子邮件和拨打电话。 问题是我无法从Set中检索数据。我的错误在哪里?
我的自定义CursorAdapter:
public class NotifyAdapter extends CursorAdapter {
public static final String TAG = NotifyAdapter.class.getSimpleName();
NotifyActivity notifyActivity=new NotifyActivity();
ListView listView;
Cursor c;
public NotifyAdapter(Context context, Cursor c, ListView listView, int flags) {
super(context, c, 0);
this.listView = listView;
this.c = c;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_item_5col_notifications_send, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tv_name = (TextView) view.findViewById(R.id.tv_notifications_full_name);
ImageView iv_sms_notify = (ImageView) view.findViewById(R.id.iv_sms_notify);
ImageView iv_email_notify = (ImageView) view.findViewById(R.id.iv_email_notify);
ImageView iv_call_notify = (ImageView) view.findViewById(R.id.iv_call_notify);
final CheckBox cb_notify = (CheckBox) view.findViewById(R.id.chb_person_notify);
final int id = cursor.getInt(cursor.getColumnIndex(DBHelper.ID_COLUMN));
String name = cursor.getString(cursor.getColumnIndex(DBHelper.NAME_COLUMN));
int sms_notify_checked = cursor.getInt(cursor.getColumnIndex(DBHelper.SMS_NOTIFICATION_COLUMN));
int email_notify_checked = cursor.getInt(cursor.getColumnIndex(DBHelper.EMAIL_NOTIFICATION_COLUMN));
int call_notify_checked = cursor.getInt(cursor.getColumnIndex(DBHelper.CALL_NOTIFICATION_COLUMN));
tv_name.setText(name);
if (sms_notify_checked == 1) {
iv_sms_notify.setImageResource(R.drawable.ic_check_black_18dp);
} else iv_sms_notify.setImageResource(R.drawable.ic_close_black_18dp);
if (email_notify_checked == 1) {
iv_email_notify.setImageResource(R.drawable.ic_check_black_18dp);
} else iv_email_notify.setImageResource(R.drawable.ic_close_black_18dp);
if (call_notify_checked == 1) {
iv_call_notify.setImageResource(R.drawable.ic_check_black_18dp);
} else iv_call_notify.setImageResource(R.drawable.ic_close_black_18dp);
cb_notify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (cb_notify.isChecked()) {
notifyActivity.addToCheckedItemSet(id);
Log.d(TAG, "clicked on item: " + id);
notifyActivity.readSet();
}
if (!cb_notify.isChecked())
{
notifyActivity.readSet();
if (notifyActivity.isCheckSetContainsId(id)) {
notifyActivity.deleteFromCheckedItemSet(id);
Log.d(TAG, "deleted: " + id);
notifyActivity.readSet();
}
我的活动与listview填充适配器:
public class NotifyActivity extends MenuActivity implements View.OnClickListener/*,
android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor>*/ {
public static final String TAG = NotifyActivity.class.getSimpleName();
ListView listView;
Button btn_send_message;
Button btn_cancel_send_message;
CheckBox cb_notify;
Intent intent;
DBHelper dbHelper;
SQLiteDatabase database;
NotifyAdapter notifyAdapter;
Set<Integer> checkedItemsIdSet = new HashSet<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notify);
setToolbar();
cb_notify = (CheckBox) findViewById(R.id.chb_person_notify);
btn_send_message = (Button) findViewById(R.id.btn_send_message);
btn_cancel_send_message = (Button) findViewById(R.id.btn_cancel_send_message);
btn_send_message.setOnClickListener(this);
btn_cancel_send_message.setOnClickListener(this);
listView = (ListView) findViewById(R.id.lv_notifications_send);
dbHelper = new DBHelper(this);
database = dbHelper.getWritableDatabase();
Cursor cursor = database.query(DBHelper.TABLE_NAME,
null,
DBHelper.RELATIONSHIP_COLUMN + " = ?", new String[]{getRelationshipValue()},
null, null, null, null);
notifyAdapter = new NotifyAdapter(this, cursor, listView, 0);
listView.setAdapter(notifyAdapter);
listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
/*
getSupportLoaderManager().initLoader(0, null, this);
getSupportLoaderManager().getLoader(0).forceLoad();*/
}
@Override
protected void onStart() {
super.onStart();
Cursor cursor = database.query(DBHelper.TABLE_NAME,
null,
DBHelper.RELATIONSHIP_COLUMN + " = ?", new String[]{getRelationshipValue()},
null, null, null, null);
notifyAdapter.changeCursor(cursor);
}
private void setToolbar() {
getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.white));
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.tv_title_toolbar);
TextView toolbarTitle_small = (TextView) toolbar.findViewById(R.id.tv_small_title_toolbar);
toolbarTitle.setText(getRelationshipValue());
toolbarTitle_small.setText("NOTIFY");
toolbar.setBackgroundColor(getResources().getColor(R.color.yellow));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(getResources().getColor(R.color.yellow));
}
}
public Set<Integer> getCheckedItemsIdSet() {
return checkedItemsIdSet;
}
public void addToCheckedItemSet(int id) {
checkedItemsIdSet.add(id);
}
public boolean isCheckSetContainsId(int id) {
if (checkedItemsIdSet.contains(id))
return true;
else return false;
}
public void deleteFromCheckedItemSet(int id) {
checkedItemsIdSet.remove(id);
}
public void readSet() {
for (int s : checkedItemsIdSet) {
Log.d("My set list content: ", String.valueOf(s));
}
}
private String getRelationshipValue() {
Intent intent = getIntent();
String relationship_value = intent.getStringExtra("Relationship");
return relationship_value;
}
private String message() {
String message = "test";
return message;
}
private void sendSMS(int id) {
dbHelper = new DBHelper(this);
database = dbHelper.getWritableDatabase();
SmsManager smsManager = SmsManager.getDefault();
String smsNotificationEnable = "1";
Cursor cursor = database.query(
DBHelper.TABLE_NAME,
new String[]{DBHelper.PHONE_COLUMN},
DBHelper.RELATIONSHIP_COLUMN + " = ? AND " + DBHelper.ID_COLUMN + " = ? AND " + DBHelper.SMS_NOTIFICATION_COLUMN + " = ?",
new String[]{getRelationshipValue(), String.valueOf(id), smsNotificationEnable},
null, null, null, null);
if (cursor.moveToFirst()) {
String destinationAddress = cursor.getString(cursor.getColumnIndex(DBHelper.PHONE_COLUMN));
smsManager.sendTextMessage(destinationAddress, null, message(), null, null);
} else Log.d(TAG, "No sms cursor");
}
private void sendEmail(int id) {
dbHelper = new DBHelper(this);
database = dbHelper.getWritableDatabase();
String emailNotificationEnable = "1";
Cursor cursor = database.query(
DBHelper.TABLE_NAME,
new String[]{DBHelper.EMAIL_COLUMN},
DBHelper.RELATIONSHIP_COLUMN + " = ? AND " + DBHelper.ID_COLUMN + " = ? AND " + DBHelper.EMAIL_NOTIFICATION_COLUMN + " = ?",
new String[]{getRelationshipValue(), String.valueOf(id), emailNotificationEnable},
null, null, null, null);
if (cursor.moveToFirst()) {
String to = cursor.getString(cursor.getColumnIndex(DBHelper.EMAIL_COLUMN));
String subject = "KTK information";
String message = message();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client"));
} else Log.d(TAG, "No email cursor");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_send_message: {
Log.d(TAG,"on click started");
readSet();
for (int checkedId : checkedItemsIdSet) {
Log.d(TAG,"on click"+checkedId);
sendSMS(checkedId);
sendEmail(checkedId);
}
}
case R.id.btn_cancel_send_message:
startActivity(new Intent(this, RespondMapActivity.class));
}
}
答案 0 :(得分:0)
答案 - 使所有方法和变量与设置静态相关。