Problem overriding method


Last Updated:

  1. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    I've created a class called InboxAdapter that extends ArrayAdapter
    Code (Text):
    1. private class InboxAdapter extends ArrayAdapter<MessageSummary> {
    I'm attempting to override the getView(int, View, ViewGroup) method
    Code (Text):
    1.     @Override
    2.     public View getView(int position, View convertView, ViewGroup parent) {
    but I get the following complaint from Eclipse:
    It says the quick fix is to remove the @Override. Why won't it let me do this? Is getView no longer a method in ArrayAdapter? (API 2.1)

    Thanks for your help
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Did you add

    super.getView(position, convertView, parent);

    to the getView() method?
     
  3. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    I should probably do that


    However, the reason Eclipse was complaining is because it didn't like a Private class inside the same file as my Activity class. I had to change the class to Final instead of Private
     
  4. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Was the private class inside your main public class? You should never have to classes entirely seperate from eachtoher in the same file... There is NEVER a need to do this. Always place your secondary classes inside your main class.
     
  5. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    Ah I wasn't correct in my previous post. I'm still having this problem. Yes the two classes are separate. I've stripped the code down. Here it is:

    Code (Text):
    1. package com.mydomain.app.activity;
    2.  
    3.  
    4. import java.util.ArrayList;
    5.  
    6. import com.mydomain.app.utils.MessageSummary;
    7.  
    8. import android.app.ListActivity;
    9. import android.content.Context;
    10. import android.os.Bundle;
    11. import android.view.LayoutInflater;
    12. import android.view.View;
    13. import android.widget.ArrayAdapter;
    14. import android.widget.TextView;
    15.  
    16. public class ActivityInbox extends ListActivity {
    17.    
    18.     private InboxAdapter m_Adapter;
    19.  
    20.     @Override
    21.     protected void onCreate(Bundle savedInstanceState) {
    22.         super.onCreate(savedInstanceState);
    23.         setContentView(R.layout.layout_inbox);
    24.     }
    25.  
    26. }
    27.  
    28. final class InboxAdapter extends ArrayAdapter<MessageSummary> {
    29.  
    30.     private ArrayList<MessageSummary> items;
    31.  
    32.     public InboxAdapter(Context context, int textViewResourceId, ArrayList<MessageSummary> items) {
    33.             super(context, textViewResourceId, items);
    34.             this.items = items;
    35.     }
    36.  
    37.     @Override
    38.     public View getView(int position, View convertView, ViewGroup parent) {
    39.             super.getView(position, convertView, parent);
    40.        
    41.             View v = convertView;
    42.             if (v == null) {
    43.                 LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    44.                 v = vi.inflate(R.layout.layout_inbox_item, null);
    45.             }
    46.             MessageSummary msgSum = items.get(position);
    47.             if (msgSum != null) {
    48.                     TextView tt = (TextView) v.findViewById(R.id.toptext);
    49.                     TextView bt = (TextView) v.findViewById(R.id.bottomtext);
    50.                     if (tt != null) {
    51.                           tt.setText("Name: "+msgSum.getMessage());
    52.                     }
    53.                     if(bt != null) {
    54.                           bt.setText("Status: "+ msgSum.getSummary());
    55.                     }
    56.             }
    57.             return v;
    58.     }
    59. }
     
  6. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Try this:

    Code (Text):
    1. public abstract View getView(int position, View convertView, ViewGroup parent)
     
  7. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    I think that your InboxAdapter should just be "public class" and should be inside your ActivityInbox class. You shouldn't need the call to super.getview.


    Then in oncreate make sure you set the list adapter before setting the layout.
     
    andywhoa likes this.
  8. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Wow.. good call, I completely missed the fact that the second class was outside of the activity class... That's what I get for being up late last night and being half asleep at work... the keyword "final" isntead of public should have given it away! lol

    Basically, cp1 is right. You need to put the InboxAdapter class inside your activity class and change it back to "public" from "final".

    With this said, I do believe that super.getView(...); is required... I thought when you override a method, you had to call super.whatever in order to retain the original info as well. I could be wrong, I just remember reading that somwhere... I have always done it anyway and never had a case where it has given me any troubles, so... I guess I could still be wrong...
     
    andywhoa likes this.
  9. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    Ok guys, thanks for all your help
     
  10. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    That's what we are hear for. haha

    Did you get it working?
     
  11. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    I'm not 100% sure about the super call -- try it both ways :)
     
  12. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    +1 and let us know the results. I am curious now. haha
     
  13. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    Yup, everything is working out :D Though instead of Public, I made the class Private
     
  14. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    I have a new question in the same area of code...

    Here is my extended ArrayAdapter


    Code (Text):
    1.    private class InboxAdapter extends ArrayAdapter<MessageSummary> {
    2.  
    3.         private ArrayList<MessageSummary> items;
    4.  
    5.         public InboxAdapter(Context context, int textViewResourceId, ArrayList<MessageSummary> items) {
    6.                 super(context, textViewResourceId, items);
    7.                 this.items = items;
    8.         }
    9.  
    10.         @Override
    11.         public View getView(int position, View convertView, ViewGroup parent) {
    12.            
    13.                 View v = convertView;
    14.                 if (v == null) {
    15.                     LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    16.                     v = vi.inflate(R.layout.layout_inbox_item, null);
    17.                 }
    18.                 MessageSummary msgSum = items.get(position);
    19.                 if (msgSum != null) {
    20.                         TextView txtSubject = (TextView) v.findViewById(R.id.toptext);
    21.                         TextView txtSummary = (TextView) v.findViewById(R.id.bottomtext);
    22.                         if (txtSubject != null) {
    23.                               txtSubject.setText(msgSum.getSubject());
    24.                         }
    25.                         if(txtSummary != null) {
    26.                               txtSummary.setText(msgSum.getSummary());
    27.                         }
    28.                 }
    29.                 return v;
    30.         }
    31.     }

    I'd like to make the background color of individual rows be different when a message is unread (msgSum.getIsRead()). I've tried doing v.setBackGroundColor("#999999"); in this case, but that does not do what I need. How can I accomplish this?
     
  15. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Easiest way to do it would be to make a new layout file for each of the two list views (not the list view activities just the listviews. Here is an example of a listview layout for one of my apps:

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <TextView
    4.     android:layout_width="fill_parent"
    5.         android:background="@color/white"
    6.     android:layout_height="fill_parent"
    7.     xmlns:android="http://schemas.android.com/apk/res/android"
    8.     android:id="@+id/label"
    9.     android:textSize="12pt"
    10.     android:textColor="@color/grey">
    11. </TextView>
    12.  
    What that file is, is the "style" of each row in the list view
    That's the entire contents of the xml file which we will call row.xml.


    After you have the "style" for your list views, you will want to create an array of strings in your strings.xml file containing the labeles for each row.

    Next, in your ListActivity, define a class array like String[] items;
    In your onCreate() method of your ListActivity, initialize the array, like so:

    items = getResources().getStringArray(R.array.menu_entries);
    where menu_entries is the array of strings you definied in your strings.xml file.

    Lastly, whenever you want to swap the colors, you just change the layout of the listview like so:

    InboxActivity.setListAdapter(new ArrayAdapter<String>(this,R.layout.row,R.id.label,items));

    Where R.id.label points to the id of your TextView you setup in the row.xml file.

    Simple as that.
     

Share This Page

Loading...