What is missing from the custom listAdapter I created?

Asked

Viewed 68 times

2

I’m looking to insert a listview into a Fragment. When I use the default layout "android.R.layout.simple_list_item_1" works smoothly, but I want to create a different layout with two text views per line.

Fragment - Excerpts

import android.app.ListFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;


public class Trechos extends Fragment {

       ListView list;
    private String[] trechos = {"a", "b"};
    private int [] posicao = {1,2};

    @Override
    //Método para inflar o layout
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState){


        //Variável view to tipo View infla o layout a ser usado
        View view = inflater.inflate(R.layout.trechos, container, false);

        //retorna a variável view
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        list = (ListView) getView().findViewById(R.id.list);

        Classe adapter = new Classe(getContext(), posicao, trechos);

        list.setAdapter(adapter);
    }
}

XML of Fragment snippets

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


<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list">

</ListView>

</LinearLayout>

Custom adapter

package com.example.gabriel.shock;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class Classe extends ArrayAdapter<String> {

    private final Context context;
    private String [] trechos;
    private int [] posicao;

    public Classe(Context context, int [] posicao, String [] trechos) {


        super(context, R.layout.row_layout, trechos);

        this.context = context;
        this.posicao = posicao;
        this.trechos = trechos;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(context);

        View rootView = inflater.inflate(R.layout.row_layout, null, true);

        TextView txtPosicao = (TextView) rootView.findViewById(R.id.posicao);

        TextView txtTrecho = (TextView) rootView.findViewById(R.id.trechos);

        txtPosicao.setText(posicao[position]);

        txtTrecho.setText(trechos[position]);

        return rootView;
    }
}

The layout of each row

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow>

        <TextView
            android:textSize="20sp"
            android:textStyle="bold"
            android:padding="5dp"
            android:id="@+id/posicao"
            android:layout_width="50dp"
            android:layout_height="50dp" />


        <TextView
            android:textStyle="italic"
            android:textSize="20sp"
            android:padding="5dp"
            android:id="@+id/trechos"
            android:layout_width="match_parent"
            android:layout_height="50dp" />

    </TableRow>

</TableLayout>

When I try to run with the custom listview the crash app:

06-21 21:35:05.115 6716-6716/com.example.gabriel.shock E/test: Exception
06-21 21:35:05.135 6716-6716/com.example.gabriel.shock E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         android.content.res.Resources$NotFoundException: String resource ID #0x1
                                                                             at android.content.res.Resources.getText(Resources.java:242)
                                                                             at android.widget.TextView.setText(TextView.java:3805)
                                                                             at com.example.gabriel.shock.Classe.getView(Classe.java:41)
                                                                             at android.widget.AbsListView.obtainView(AbsListView.java:2207)
                                                                             at android.widget.ListView.makeAndAddView(ListView.java:1845)
                                                                             at android.widget.ListView.fillDown(ListView.java:678)
                                                                             at android.widget.ListView.fillFromTop(ListView.java:739)
                                                                             at android.widget.ListView.layoutChildren(ListView.java:1661)
                                                                             at android.widget.AbsListView.onLayout(AbsListView.java:2037)
                                                                             at android.view.View.layout(View.java:14188)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:4466)
                                                                             at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
                                                                             at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
                                                                             at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
                                                                             at android.view.View.layout(View.java:14188)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:4466)
                                                                             at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1695)
                                                                             at android.view.View.layout(View.java:14188)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:4466)
                                                                             at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
                                                                             at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
                                                                             at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
                                                                             at android.view.View.layout(View.java:14188)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:4466)
                                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
                                                                             at android.view.View.layout(View.java:14188)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:4466)
                                                                             at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
                                                                             at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
                                                                             at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
                                                                             at android.view.View.layout(View.java:14188)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:4466)
                                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
                                                                             at android.view.View.layout(View.java:14188)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:4466)
                                                                             at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2189)
                                                                             at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1953)
                                                                             at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1145)
                                                                             at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4907)
                                                                             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
                                                                             at android.view.Choreographer.doCallbacks(Choreographer.java:579)
                                                                             at android.view.Choreographer.doFrame(Choreographer.java:548)
                                                                             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
                                                                             at android.os.Handler.handleCallback(Handler.java:725)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                             at android.os.Looper.loop(Looper.java:153)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:511)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                                                                             at dalvik.system.NativeStart.main(Native Method)

Thanks for the help.

1 answer

1


You’re passing ints for the setText of TextView on the line

txtPosicao.setText(posicao[position]);

This causes it to be called the method setText(int resId), which will attempt to define as text the TextView whatever’s in the file R.java associated with that ID. As, in your case, there was nothing (and look that you were lucky-- it would be a coincidence difficult to debug, if there were), gave this error.

Exchange for

txtPosicao.setText(String.valueOf(posicao[position]));

that will call the method setText suitable and the problem goes away.

  • 1

    Wow that inattention kk, thanks guy I’ll change here.

  • very common error, mainly because there is no warning from the IDE while writing the code.

  • Man was that, thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.