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

Apps Problem overriding method

D

Deleted User

Guest
I've created a class called InboxAdapter that extends ArrayAdapter
Code:
private class InboxAdapter extends ArrayAdapter<MessageSummary> {
I'm attempting to override the getView(int, View, ViewGroup) method
Code:
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
but I get the following complaint from Eclipse:
The method getView(int, View, ViewGroup) of type InboxAdapter must override a superclass method

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
 
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

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.
 
Upvote 0
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:
package com.mydomain.app.activity;


import java.util.ArrayList;

import com.mydomain.app.utils.MessageSummary;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ActivityInbox extends ListActivity {
	
	private InboxAdapter m_Adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_inbox);
	}

}

final class InboxAdapter extends ArrayAdapter<MessageSummary> {

    private ArrayList<MessageSummary> items;

    public InboxAdapter(Context context, int textViewResourceId, ArrayList<MessageSummary> items) {
            super(context, textViewResourceId, items);
            this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    		super.getView(position, convertView, parent);
    	
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.layout_inbox_item, null);
            }
            MessageSummary msgSum = items.get(position);
            if (msgSum != null) {
                    TextView tt = (TextView) v.findViewById(R.id.toptext);
                    TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                    if (tt != null) {
                          tt.setText("Name: "+msgSum.getMessage());
                    }
                    if(bt != null) {
                          bt.setText("Status: "+ msgSum.getSummary());
                    }
            }
            return v;
    }
}
 
Upvote 0
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.

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...
 
  • Like
Reactions: Deleted User
Upvote 0
I have a new question in the same area of code...

Here is my extended ArrayAdapter


Code:
   private class InboxAdapter extends ArrayAdapter<MessageSummary> {

        private ArrayList<MessageSummary> items;

        public InboxAdapter(Context context, int textViewResourceId, ArrayList<MessageSummary> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        	
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.layout_inbox_item, null);
                }
                MessageSummary msgSum = items.get(position);
                if (msgSum != null) {
                        TextView txtSubject = (TextView) v.findViewById(R.id.toptext);
                        TextView txtSummary = (TextView) v.findViewById(R.id.bottomtext);
                        if (txtSubject != null) {
                              txtSubject.setText(msgSum.getSubject());
                        }
                        if(txtSummary != null) {
                              txtSummary.setText(msgSum.getSummary());
                        }
                }
                return v;
        }
    }


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?
 
Upvote 0
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:
<?xml version="1.0" encoding="utf-8"?>
<TextView
	android:layout_width="fill_parent"
        android:background="@color/white"
	android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android" 
	android:id="@+id/label"
	android:textSize="12pt"
	android:textColor="@color/grey">
</TextView>

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.
 
Upvote 0

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