ToDoList - Notepad View
In chapter 4, the code snippet is shown to view our todolist in notepad view. I implemented the code but am not able to view the list in notepad view. Please point out the problem.
This is my current java code:
package todo.list;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.graphics.Canvas;
import android.graphics.Paint;
public class ToDoListActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView)findViewById(R.id.list);
final EditText myText = (EditText)findViewById(R.id.edittext);
final ArrayList<String> toDoItems = new ArrayList<String>();
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this, R.layout.todolist_item, toDoItems);
myListView.setAdapter(aa);
myText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN)
if(keyCode == KeyEvent.KEYCODE_ENTER) {
toDoItems.add(myText.getText().toString());
aa.notifyDataSetChanged();
myText.setText("");
return true;
}
return false;
}
});
}
public class ToDoListItemView extends TextView {
public ToDoListItemView(Context context, AttributeSet ats, int ds) {
super(context, ats, ds);
init();
}
public ToDoListItemView(Context context) {
super(context);
init();
}
public ToDoListItemView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private Paint marginPaint;
private Paint linePaint;
private int paperColor;
private float margin;
private void init() {
Resources myResources = getResources();
marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
marginPaint.setColor(myResources.getColor(R.color. notepad_margin));
linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint.setColor(myResources.getColor(R.color.no tepad_lines));
paperColor = myResources.getColor(R.color.notepad_paper);
margin = myResources.getDimension(R.dimen.notepad_margin);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(paperColor);
canvas.drawLine(0, 0, getMeasuredHeight(), 0, linePaint);
canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint);
canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);
canvas.save();
canvas.translate(margin, 0);
super.onDraw(canvas);
canvas.restore();
}
}
}
In the book it is not told where to write the ToDoListItemView class. Please tell me whether I have written it in the right place, or I have to create another java file and write it there. The above code works fine except that I don't get a notepad view, and when I press ENTER, "the application stops working" error occurs.
|