I want to save checkbox
state in listview
when I open new activity and save value of the checkbox
. When I close activity where is checkbox
listview
my data does not save. I use ormlite in my project and save chechbox
value in table. But the value does not save.
Here is my code:
1) Adapter:
public class DeadlineAdapter extends ArrayAdapter<Deadline> {
Context context;
List<Deadline> deadlines;
int layoutResourceId;
public DeadlineAdapter(Context context, int resource, List<Deadline> objects) {
super(context, resource, objects);
this.context = context;
this.layoutResourceId = resource;
this.deadlines = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View rootView = convertView;
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (rootView == null){
rootView = layoutInflater.inflate(R.layout.deadline_item_row, null);
TextView deadlineNameTextView = (TextView)rootView.findViewById(R.id.deadlineNameTextView);
TextView deadlineStartTextView = (TextView)rootView.findViewById(R.id.deadlineStartTextView);
TextView deadlineEndTextView = (TextView)rootView.findViewById(R.id.deadlineEndTextView);
CheckBox deadlineCheckBox = (CheckBox)rootView.findViewById(R.id.deadlineCheckBox);
final Deadline deadline = deadlines.get(position);
deadlineNameTextView.setText(deadline.getDeadlineName());
deadlineEndTextView.setText(deadline.getDeadlineTo());
deadlineStartTextView.setText(deadline.getDeadlineFrom());
deadlineCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
deadline.setDone(true);
} else {
deadline.setDone(false);
}
}
});
deadlineCheckBox.setChecked(deadline.isDone());
}
return rootView;
}
}
2) Activity with checkbox
listview
. How to save checkbox
value in database in listview
and send data to new Activity.
public class DeadlineListActivity extends ActionBarActivity {
ListView deadlineListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deadline_list);
initListView();
}
public void initListView(){
deadlineListView = (ListView)findViewById(R.id.deadlineListView);
}
@Override
protected void onStart() {
super.onStart();
setupDeadlineListView(deadlineListView);
}
public void setupDeadlineListView(ListView listView){
List<Deadline> dl = DatabaseManager.getInstance().getAllDeadlineList();
final List<Deadline> deadlines = new ArrayList<Deadline>();
for (Deadline d : dl){
deadlines.add(d);
}
DeadlineAdapter adapter = new DeadlineAdapter(getApplicationContext(), R.layout.deadline_item_row, deadlines);
listView.setAdapter(adapter);
listView.setItemsCanFocus(true);
deadlineListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Deadline deadline = deadlines.get(position);
Intent intent = new Intent(DeadlineListActivity.this, CreateDeadlineActivity.class);
intent.putExtra(Constants.KEY_ID, deadline.getDeadlineId());
intent.putExtra(Constants.CB_RESULT, deadline.isDone());
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_deadline_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.add_button){
Intent intent = new Intent(DeadlineListActivity.this, CreateDeadlineActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
}
3) Activity where I get chechbox
data from listview
and save to database.
public class CreateDeadlineActivity extends ActionBarActivity {
EditText deadlineEditText;
Button fromButton;
Button toButton;
Button saveButton;
Date fromDate;
Date toDate;
String deadlineStr = "";
String fromStr;
String toStr;
Deadline deadline;
boolean isdeadlineSelected;
private DatePickerFragment datePickerFragmentTo;
private DatePickerFragment datePickerFragmentFrom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_deadline);
initFindViewById();
initDate();
setupDeadline();
toButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datePickerFragmentTo.show(getFragmentManager(), "Set date");
}
});
fromButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datePickerFragmentFrom.show(getFragmentManager(), "Set date");
}
});
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (deadline != null) {
updateDeadline();
} else {
createDeadline();
}
}
});
}
public void initFindViewById(){
deadlineEditText = (EditText)findViewById(R.id.deadlineEditText);
toButton = (Button)findViewById(R.id.toButton);
fromButton = (Button)findViewById(R.id.fromButton);
saveButton = (Button)findViewById(R.id.saveButton);
}
public void initDate(){
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
fromDate = new Date();
toDate = new Date();
initDatePicker(dateFormat);
}
public void initDatePicker(SimpleDateFormat dateFormat){
datePickerFragmentTo = new DatePickerFragment();
datePickerFragmentTo.setDateFormat(dateFormat);
datePickerFragmentTo.setDependentDate(toDate);
datePickerFragmentTo.setDialogDateTextView(toButton);
datePickerFragmentFrom = new DatePickerFragment();
datePickerFragmentFrom.setDateFormat(dateFormat);
datePickerFragmentFrom.setDependentDate(fromDate);
datePickerFragmentFrom.setDialogDateTextView(fromButton);
}
private void setupDeadline() {
Bundle bundle = getIntent().getExtras();
if (bundle != null && bundle.containsKey(Constants.KEY_ID)) {
int wishListId = bundle.getInt(Constants.KEY_ID);
boolean cbResult = bundle.getBoolean(Constants.CB_RESULT);
isdeadlineSelected = cbResult;
deadline = DatabaseManager.getInstance().getDeadlineId(wishListId);
deadlineEditText.setText(deadline.getDeadlineName());
fromButton.setText(deadline.getDeadlineFrom());
toButton.setText((deadline.getDeadlineTo()));
deadline.setDone(cbResult);
Log.d("SETUP_RESULT", deadline.getDeadlineId() + " " + deadline.getDeadlineName() +
" " + deadline.getDeadlineFrom() + " " + deadline.getDeadlineTo() +
" " + deadline.isDone());
Log.d("CHECK_BOX_REUSLT", cbResult + "");
}
}
public void createDeadline(){
ToString();
if (deadlineStr != null && fromStr != null && toStr != null) {
Deadline deadline = new Deadline();
deadline.setDeadlineName(deadlineStr);
deadline.setDeadlineFrom(fromStr);
deadline.setDeadlineTo(toStr);
deadline.setDone(isdeadlineSelected);
DatabaseManager.getInstance().createDeadline(deadline);
Log.d("ROW_IN_DATABASE", deadline.getDeadlineId() + " " + deadline.getDeadlineName() +
" " + deadline.getDeadlineFrom() + " " + deadline.getDeadlineTo() +
" " + deadline.isDone());
finish();
} else {
Toast.makeText(getApplicationContext(), "Enter all values", Toast.LENGTH_LONG).show();
}
}
private void updateDeadline() {
if (deadline != null) {
ToString();
if (!deadlineStr.equals("") && !fromStr.equals("from") && !toStr.equals("to")){
deadline.setDeadlineName(deadlineStr);
deadline.setDeadlineFrom(fromStr);
deadline.setDeadlineTo(toStr);
deadline.setDone(isdeadlineSelected);
DatabaseManager.getInstance().updateDeadline(deadline);
Log.d("ROW_IN_DATABASE", deadline.getDeadlineId() + " " + deadline.getDeadlineName() +
" " + deadline.getDeadlineFrom() + " " + deadline.getDeadlineTo() +
" " + deadline.isDone());
finish();
} else {
Toast.makeText(getApplicationContext(), "Enter correct date!", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "Enter all values", Toast.LENGTH_LONG).show();
}
}
public void ToString(){
Log.d("BUTTONS_TEXT", toButton.getText().toString() + " " + fromButton.getText().toString());
if (!deadlineEditText.getText().toString().isEmpty() &&
!toButton.getText().toString().isEmpty() &&
!fromButton.getText().toString().isEmpty()){
findViewByIdToString();
}
else {
Toast.makeText(getApplicationContext(), "Enter all values", Toast.LENGTH_LONG).show();
}
}
public void findViewByIdToString(){
deadlineStr = deadlineEditText.getText().toString();
fromStr = fromButton.getText().toString();
toStr = toButton.getText().toString();
Log.d("TO_STRING", deadlineStr + " " + fromStr + " " + toStr);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_create_deadline, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Aucun commentaire:
Enregistrer un commentaire