0

I need to show custom items on a list but after trying many different ways to solve the problem I'm still stuck with this error:

> java.lang.RuntimeException: Unable to start activity   
> ComponentInfo{quinto3.et37.agendadeviajes/quinto3.et37.agendadeviajes.Principal}:
> java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams
> cannot be cast to android.widget.AbsListView$LayoutParams

Here is the principal activity

public class Principal extends Activity {

    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;

    private ArrayAdapter<TipoDeViaje> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.agenda_principal);
        ListView lista = (ListView) findViewById(R.id.lista);
        View footer = ((LayoutInflater)     this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_add_button,     (ViewGroup) getWindow().getDecorView().getRootView(), false);
        lista.addFooterView(footer, null, false);
        llenarLista();
        registerForContextMenu(lista);
    }

    private void llenarLista() {
        ListView lista = (ListView) findViewById(R.id.lista);
        TipoDeViajeDAO tManager = new TipoDeViajeDAO(this);
        ArrayList<TipoDeViaje> tipoList = tManager.traerTodos(this);
        mAdapter = new AdapterTipoDeViaje(this,tipoList);
        lista.setAdapter(mAdapter);
    }

    public void footerClick(View v) {
        Toast.makeText(this, "pls", Toast.LENGTH_SHORT).show();
        Intent i = new Intent(this, EditTipoDeViaje.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        llenarLista();
    }

Here is my custom adapter

public class AdapterTipoDeViaje extends ArrayAdapter<TipoDeViaje> {
    public AdapterTipoDeViaje(Context context, ArrayList<TipoDeViaje> tipos) {
        super(context, 0, tipos);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        TipoDeViaje t = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.lista_fila, parent,     false);
        }
        // Lookup view for data population
        TextView desdeText = (TextView) convertView.findViewById(R.id.desde_text);
        TextView hastaText = (TextView) convertView.findViewById(R.id.hasta_text);
        // Populate the data into the template view using the data object
        desdeText.setText(t.getDesde().getNombre());
        hastaText.setText(t.getHasta().getNombre());
        // Return the completed view to render on screen
        return convertView;
    }

And here is my lista_fila.xml (the layout I want to use for the row)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/desde_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="desde"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/hasta_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hasta"
        android:textSize="30sp" />

    <ImageButton
        android:id="@+id/boton_agregar_registro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/boton_agregar_registro"
        android:src="@drawable/ic_add_viaje" />

</LinearLayout>

Here is agenda_principal

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/lista"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

And here is footer_add_button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/footer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="horizontal"
    android:paddingTop="7dip" >
    <ImageButton
         android:id="@+id/footer_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dip"
        android:gravity="center"
        android:onClick="footerClick"
        android:src="@drawable/ic_add_button" 
        android:contentDescription="@string/desc_footer_button"/>
</LinearLayout>

1 Answer 1

1

The problem is the footer inflation.

Try this :

View footer = LayoutInflater.from(this).inflate(R.layout.footer_add_button, lista, false);
lista.addFooterView(footer);
Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting thsi error now: The method inflate(int, ViewGroup, boolean) in the type LayoutInflater is not applicable for the arguments (int, ListView, null)
Oups sorry for the mistake ;) good to know you find the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.