Hi, I'm working on the to-do-list application. For some reason, when I
save the edited text, the changes won't appear in the main screen
(TaskList.java) until I click on something. Does anyone know what
could be causing this?
Thanks!
TaskList.java
package cornell.cs2046.tasks;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class TaskList extends Activity{
// private ArrayList<Task> taskList;
// private TaskAdapter mTaskAdapter;
private String lv_items[] = { "Android", "iPhone", "BlackBerry",
"AndroidPeople", "J2ME", "Listview",
"ArrayAdapter",
"ListItem",
"Us", "UK", "India" };
public static class Task {
String title;
String details;
boolean isCompleted;
public Task(String title, String details, boolean isCompleted)
{
this.title = title;
this.details = details;
this.isCompleted = isCompleted;
}
}
private class TaskAdapter extends ArrayAdapter<Task> {
private Task[] tasks;
public TaskAdapter(Context context, int textViewResourceId,
Task[] tasks) {
super(context, textViewResourceId, tasks);
this.tasks = tasks;
}
@Override
public View getView(int position, View convertView, ViewGroup
parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater li =
(LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v =
li.inflate(android.R.layout.simple_list_item_multiple_choice, null);
holder=new ViewHolder();
holder.checkbox= (CheckedTextView)
v.findViewById(android.R.id.text1);
v.setTag(holder);
}
else {
holder= (ViewHolder) v.getTag();
}
holder.checkbox.setText(tasks[position].title);
holder.checkbox.setChecked(tasks[position].isCompleted);
return v;
/*
* TODO:
*
* -Set the text of the ListView row to the task's title.
* -If this task is completed:
* -Mark the corresponding ListView item as checked.
* -Put a strike through the title of the task.
*/
}
}
static class ViewHolder {
CheckedTextView checkbox;
}
// Items on the option menu.
private static final int MENU_ITEM_ADD = Menu.FIRST;
private static final int MENU_ITEM_DELETE_OLD = Menu.FIRST + 1;
// Items on the context menu.
private static final int CONTEXT_MENU_ITEM_MARK_COMPLETED =
Menu.FIRST;
private static final int CONTEXT_MENU_ITEM_MODIFY = Menu.FIRST +
1;
private static final int CONTEXT_MENU_ITEM_DELETE = Menu.FIRST +
2;
private ListView mTasks;
// Modify these to change the tasks which appear in the list.
private Task[] taskArray = new Task[] {
new Task("Task 1", "More information for task 1.", false),
new Task("Task 2", "More information for task 2.", true),
new Task("Task 3", "More information for task 3.", false)
};
private EditText titleField;
private EditText detailField;
private CheckBox complete;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task_list);
mTasks = (ListView) findViewById(R.id.tasks);
titleField = (EditText) findViewById(R.id.titleEntry);
detailField = (EditText) findViewById(R.id.detailsEntry);
mTasks.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mTasks.setAdapter(new TaskAdapter(this,
android.R.layout.simple_list_item_multiple_choice, taskArray));
// mTasks.setTextFilterEnabled(true);
/** mTasks.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View
view, int
position,
long id) {
}
});*/
mTasks.setOnCreateContextMenuListener(new
View.OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) menuInfo;
Task task = (Task)
mTasks.getAdapter().getItem(info.position);
if (task == null) {
return;
}
menu.setHeaderTitle(task.title);
menu.add(menu.NONE, CONTEXT_MENU_ITEM_DELETE,
menu.NONE, R.string.delete_task);
menu.add(menu.NONE, CONTEXT_MENU_ITEM_MODIFY,
menu.NONE, R.string.modify_task);
menu.add(menu.NONE, CONTEXT_MENU_ITEM_MARK_COMPLETED,
menu.NONE, R.string.mark_complete_task);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras= intent.getExtras();
final String title= intent.getStringExtra("title");
// AlertDialog.Builder builder= new
AlertDialog.Builder(TaskList.this);
final String detail= intent.getStringExtra("details");
final boolean check= intent.getBooleanExtra("isCompleted",
false);
final int position= intent.getIntExtra("position", 0);
Task t1= new Task(title, detail, check);
taskArray[position]=t1;
//complete.setChecked(check);
// titleField= (EditText) findViewById(R.id.titleEntry);
//
builder.setTitle(title).setMessage(detail).setNegativeButton("Close",
null).create().show();
// titleField.setText(title);
// detailField.setText(detail);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case CONTEXT_MENU_ITEM_MODIFY:
Task t1= taskArray[(int) info.id];
Intent i1= new Intent(this, TaskEdit.class);
i1.putExtra("title", t1.title);
i1.putExtra("isCompleted", t1.isCompleted);
i1.putExtra("details", t1.details);
i1.putExtra("position", (int) info.id);
startActivityForResult(i1,0);
return true;
}
return super.onContextItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem add= menu.add(menu.NONE, MENU_ITEM_ADD, menu.NONE,
R.string.add_task);
add.setIcon(android.R.drawable.ic_menu_add);
MenuItem delete= menu.add(menu.NONE, MENU_ITEM_DELETE_OLD,
menu.NONE, R.string.delete_old_tasks);
delete.setIcon(android.R.drawable.ic_menu_delete);
// TODO: Populate the options menu as displayed in the
screenshot.
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected(item);
switch (item.getItemId()){
case MENU_ITEM_ADD:
Intent i1= new Intent(this, TaskEdit.class);
i1.putExtra("title", "default");
i1.putExtra("isCompleted", false);
i1.putExtra("details", "default");
startActivity(i1);
break;
case MENU_ITEM_DELETE_OLD:
AlertDialog.Builder builder1= new
AlertDialog.Builder(TaskList.this);
builder1.setTitle("Menu item delete
old").setMessage("Test1").setNegativeButton("Close",
null).create().show();
break;
}
return false;
}
}
TaskEdit.java
package cornell.cs2046.tasks;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class TaskEdit extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.task_edit);
Button save= (Button) findViewById(R.id.save);
Button delete= (Button) findViewById(R.id.delete);
Intent i1= getIntent();
Bundle b= i1.getExtras();
final String title= b.getString("title");
final String detail= b.getString("details");
final boolean isCompleted= b.getBoolean("isCompleted");
final int position=b.getInt("position");
EditText titleField= (EditText) findViewById(R.id.titleEntry);
titleField.setText(title);
EditText detailField= (EditText)
findViewById(R.id.detailsEntry);
detailField.setText(detail);
save.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
AlertDialog.Builder builder= new
AlertDialog.Builder(TaskEdit.this);
EditText title1= (EditText)
findViewById(R.id.titleEntry);
String title= title1.getText().toString();
EditText details1= (EditText)
findViewById(R.id.detailsEntry);
String detail= details1.getText().toString();
CheckBox complete= (CheckBox)
findViewById(R.id.checkbox);
Boolean check= complete.isChecked();
Intent i1= new Intent();
i1.putExtra("title", title);
i1.putExtra("details", detail);
i1.putExtra("isCompleted",check );
i1.putExtra("position", position)
; setResult(Activity.RESULT_OK, i1);
//
builder.setTitle(title).setMessage(detail).setNegativeButton("Close",
null).create().show();
finish();
}
});
delete.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
finish();
}
});
}
/*
* TODO:
*
* Initialize the UI so that the form fields contain the task
information that was included in
* the Intent that launched this activity.
*/
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en