• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problems in extending from ListView

vidhu.0007

Lurker
Jun 15, 2010
4
0
Hi All,

I want to create my own Mylist_view by extending from ListView and implementing its methods. My code is not running properly.
The MyList class goes like this:
Code:
public class MyList extends ListView{

    private Paint marginPaint;
    private Paint linePaint;
    int paperColor;
    private float margin;
    
    public MyList(Context context) {
        super(context);
        init();
        // TODO Auto-generated constructor stub
    }
    public MyList(Context context, AttributeSet atts,int ds)
    {
        super(context,atts,ds);
    init();
    }
    
    public MyList(Context context, AttributeSet atts)
    {
        super(context,atts);
        init();
    }
    
    
    private void init()
    {
    final 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.notepad_lines));
    paperColor=myResources.getColor(R.color.notepad_paper);
    margin=myResources.getDimension(R.dimen.notepad_margin);
    }
    
    //draw the back ground screen
    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();
        
    }


My Activity class goes like this:
Code:
public class ToDoList extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText myEditText= (EditText) findViewById(R.id.myEditText);
        ListView myListView= (ListView)findViewById(R.id.myListView);
     int resId=R.layout.mylist_item;
        
        final ArrayList<String> items=new ArrayList<String>();
        final ArrayAdapter<String> aa=new ArrayAdapter<String>(this,resId,items);
        
     myListView.setAdapter(aa);
     
     myEditText.setOnKeyListener(new OnKeyListener(){

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if(event.getAction()==KeyEvent.ACTION_DOWN)
            {
            //    if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER)
                //{
                    items.add(0, myEditText.getText().toString());
                    aa.notifyDataSetChanged();
                    myEditText.setText("");
                    return true;
                //}
            }
            return false;
        }
         
     }); }}

The resources mylist_item.xml is below:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<com.todolist.MyList
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/notepad_text"
/>

I tried debugging the above code. The "EditText" resource is loading properly. But when I click on the down arrow key, mylist_item is loaded, i.e. MyList constructor is called. But after it executes the MyList::init() method, the application is terminated. It never reaches the Mylist:OnDraw(Canvas) method.

I am new to android. Cant figure out why it is not executing completely. Maybe I need to implement some more methods of ListView class.

Please help.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones