sys_debug
sys_debug

Reputation: 4003

android custom TextView in ListView

I created a regular list view with custom adapter and all is well. It is showing the name of the object (exactly what I want). I also want to show couple of other information. So from my research, I found out that I will need to create a custom text view to handle the requirement. I tried doing a basic one where I change the background color to anything other than black, but not working. I really don't know why. Here is the code for custom adapter:

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
    // TODO Auto-generated method stub
    if(arg1==null)
        arg1 = layoutInflator.inflate(R.layout.simplerow ,arg2, false);
    TextView name = (TextView)arg1.findViewById(android.R.id.text1);
    name.setText(discountObjectArray[arg0].name);
    return arg1;
}

and here is the code in simplerow.xml for the simple custom textview:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/customTextView" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:padding="10dp"
 android:textSize="16sp" 
android:textDirection="rtl"
android:background="@color/thankYouLabelColor">
</TextView>

with simple change of android.R.layout.simple_list_item_1 to R.layout.simplerow the logcat shows error in line 49 which is:

name.setText(discountObjectArray[arg0].name);

Any idea why this would not work?

Upvotes: 1

Views: 1846

Answers (3)

Hariharan
Hariharan

Reputation: 24853

From this change android.R.id.text1 to R.id.customTextView

TextView name = (TextView)arg1.findViewById(android.R.id.text1);

that is...

TextView name = (TextView)arg1.findViewById(R.id.customTextView);

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

You have this

      android:id="@+id/customTextView"   

Replace this

     TextView name = (TextView)arg1.findViewById(android.R.id.text1);

by

     TextView name = (TextView)arg1.findViewById(R.id.customTextView);

Here's the list of R.id from the the android package

http://developer.android.com/reference/android/R.id.html

You are referring to the one in the android package android.R.id.text1 while you have in xml id of textview as customTextView.

Look at the id attribute here

http://developer.android.com/guide/topics/ui/declaring-layout.html

Upvotes: 2

Mikel
Mikel

Reputation: 1621

The problem is you need to change the id of the Textview, so change:

TextView name = (TextView)arg1.findViewById(android.R.id.text1);

with:

TextView name = (TextView)arg1.findViewById(R.id.customTextView);

Upvotes: 1

Related Questions