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

Apps Custom ArrayAdapter for ListView

Julie95

Lurker
Hello,

I am currently developing my first Android Application and I'm having some trouble writing my own custom ArrayAdapter. I read many things about it but I can't make my application work.

So, here is my main.xml :
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/MainLayout"
    >
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout" android:orientation="horizontal">
        <EditText android:layout_weight="5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Type your quote here" android:id="@+id/editText" android:textSize="10dp"></EditText>
        <Button android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add Quote" android:id="@+id/button" android:textSize="10dp"></Button>
    </LinearLayout>
    <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/quotelist"/>
</LinearLayout>

I have an object Quote :
Code:
package com.supinfo.geekquote;

import java.util.Date;

public class Quote {
    private String strQuote;
    private int rating;
    private Date creationDate;
    
    public Quote()
    {
    }
    
    public String getStrQuote() {
        return strQuote;
    }
    public void setStrQuote(String strQuote) {
        this.strQuote = strQuote;
    }
    public int getRating() {
        return rating;
    }
    public void setRating(int rating) {
        this.rating = rating;
    }
    public Date getCreationDate() {
        return creationDate;
    }
    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.strQuote;
    }
}

And all I want to do is add Quotes to my ListView, but I want to change the background color one time on two.
So I tried to make my custom ArrayAdapter<Quote> class, but I get an error when I do that.

So here is my getView method :
Code:
@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        Quote quote = quoteList.get(position);
        if (quote != null) {
                TextView tv = (TextView) v.findViewById(R.id.quotelist);
                if (tv != null) {
                      tv.setText("Name: "+quote.getStrQuote());
                      tv.setTextColor(R.color.white);
                      if (position%2 == 0)
                      {
                          tv.setBackgroundColor(R.color.black);
                      }
                      else
                      {
                          tv.setBackgroundColor(R.color.grey);
                      }
                }
        }
        return v;
    }

So I have an exception (Java.lang.NullPointerException) at this line :
TextView tv = (TextView) v.findViewById(R.id.quotelist);
(I guess it's because R.id.quotelist is a ListView, but I don't have any TextView, I just want to create new ones and add them to ListView.

So I don't really understand how I'm supposed to do and I would be very thankful if someone can help me

Thank you
 
Back
Top Bottom