1

I have a listActivity that displays via an adapter an xml feed fetched from the web, adn the layout file activity_list_feed.xml :

`<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:transcriptMode="normal"
    />`

In the graphic editor i cannot drag a button into this layout, and when i try to hardcode as per this file :

`<?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" >
<Button
    android:id="@+id/addBtn"
    android:text="Add New Item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="addItems"/>
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:transcriptMode="normal"
    />
</LinearLayout>`

i get a compile error message :

java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView

How can i add a button on top of the list because i want to refresh the pull.thank you.

Yvon Huynh
  • 453
  • 3
  • 16

2 Answers2

0

The code that you provided actually works in its current state in Android Studio:

Make sure that in your java code, you are referencing the correct item. Make sure that you're setting the overall layout to be that layout, and THEN doing findViewById(R.layout.addBtn).

Also, try changing the Android Version in your IDE to 22 (as I have it set in the top right corner of the picture). That may solve your error.

Alex K
  • 8,269
  • 9
  • 39
  • 57
0

I managed to add the button in the editor and this without setting to API 22 as per your picture. However the program didn't compile still.

The message error "ArrayAdapter requires the resource ID to be a TextView" meant i didn't provide the right argument to the adapter.

According to this answer : "ArrayAdapter requires the resource ID to be a TextView" xml problems it appears that the choice of the constructor is important, since i wanted to add a button in the view, i must use the constructor with 4 arguments (the additional argument being the id of the view): http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context,int,int,java.util.List)

Using this constructor solved the problem.

Community
  • 1
  • 1
Yvon Huynh
  • 453
  • 3
  • 16