1. Check out our companion app, Forums for Android! Download from Google Play

Problems in extending from ListView

Discussion in 'Application Development' started by vidhu.0007, Jun 17, 2010.

  1. vidhu.0007

    vidhu.0007 New Member
    Thread Starter
    5

    Jun 15, 2010
    4
    0
    5
    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 (Text):
    1.  
    2. public class MyList extends ListView{
    3.  
    4.     private Paint marginPaint;
    5.     private Paint linePaint;
    6.     int paperColor;
    7.     private float margin;
    8.    
    9.     public MyList(Context context) {
    10.         super(context);
    11.         init();
    12.         // TODO Auto-generated constructor stub
    13.     }
    14.     public MyList(Context context, AttributeSet atts,int ds)
    15.     {
    16.         super(context,atts,ds);
    17.     init();
    18.     }
    19.    
    20.     public MyList(Context context, AttributeSet atts)
    21.     {
    22.         super(context,atts);
    23.         init();
    24.     }
    25.    
    26.    
    27.     private void init()
    28.     {
    29.     final Resources myResources=getResources();
    30.    
    31.     marginPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
    32.     marginPaint.setColor(myResources.getColor(R.color.notepad_margin));
    33.     linePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
    34.     linePaint.setColor(myResources.getColor(R.color.notepad_lines));
    35.     paperColor=myResources.getColor(R.color.notepad_paper);
    36.     margin=myResources.getDimension(R.dimen.notepad_margin);
    37.     }
    38.    
    39.     //draw the back ground screen
    40.     public void onDraw(Canvas canvas)
    41.     {
    42.        
    43.     canvas.drawColor(paperColor);
    44.     canvas.drawLine(0, 0, getMeasuredHeight(), 0, linePaint);
    45.     canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint);
    46.     canvas.drawLine(margin,0, margin, getMeasuredHeight(),  marginPaint);
    47.    
    48.     canvas.save();
    49.     canvas.translate(margin, 0);
    50.    
    51.     super.onDraw(canvas);
    52.     canvas.restore();
    53.        
    54.     }
    55.  

    My Activity class goes like this:
    Code (Text):
    1.  
    2. public class ToDoList extends Activity {
    3.     /** Called when the activity is first created. */
    4.     @Override
    5.     public void onCreate(Bundle savedInstanceState) {
    6.         super.onCreate(savedInstanceState);
    7.         setContentView(R.layout.main);
    8.         final EditText myEditText= (EditText) findViewById(R.id.myEditText);
    9.         ListView myListView= (ListView)findViewById(R.id.myListView);
    10.      int resId=R.layout.mylist_item;
    11.        
    12.         final ArrayList<String> items=new ArrayList<String>();
    13.         final ArrayAdapter<String> aa=new ArrayAdapter<String>(this,resId,items);
    14.        
    15.      myListView.setAdapter(aa);
    16.      
    17.      myEditText.setOnKeyListener(new OnKeyListener(){
    18.  
    19.         public boolean onKey(View v, int keyCode, KeyEvent event) {
    20.             // TODO Auto-generated method stub
    21.             if(event.getAction()==KeyEvent.ACTION_DOWN)
    22.             {
    23.             //    if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER)
    24.                 //{
    25.                     items.add(0, myEditText.getText().toString());
    26.                     aa.notifyDataSetChanged();
    27.                     myEditText.setText("");
    28.                     return true;
    29.                 //}
    30.             }
    31.             return false;
    32.         }
    33.          
    34.      }); }}
    35.  
    The resources mylist_item.xml is below:
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="UTF-8"?>
    3. <com.todolist.MyList
    4. xmlns:android="http://schemas.android.com/apk/res/android"
    5. android:layout_width="fill_parent"
    6. android:layout_height="fill_parent"
    7. android:textColor="@color/notepad_text"
    8. />
    9.  
    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.
     

    Advertisement

Share This Page

Loading...