Error showing list with Arrayadapter

Asked

Viewed 71 times

0

I’m not getting this list to be shown. I’m using a Custon Arrayadapter, but it’s not rolling. Thanks. Doing the debug, it says "Frame not availeble". Could someone give me a light?

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<EventK> eventK = new ArrayList<>();

    /*cria arraylist de eventos
    *(nome do evento, data, hora, distancia, preço 1|2, pessoas confirmadas
    */

    eventK.add(new EventK("Cosmic Boys - Curitiba PR", "2017-02-14" , "10h00", "0|0",
            "1,5km", R.drawable.event_1, 1));
    eventK.add(new EventK("Cosmic Boys - Curitiba PR", "2017-02-14" , "10h00", "0|0",
            "1,5km", R.drawable.event_1, 2));
    eventK.add(new EventK("Cosmic Boys - Curitiba PR", "2017-02-14" , "10h00", "0|0",
            "1,5km", R.drawable.event_1, 3));
    eventK.add(new EventK("Cosmic Boys - Curitiba PR", "2017-02-14" , "10h00", "0|0",
            "1,5km", R.drawable.event_1, 4));
    eventK.add(new EventK("Cosmic Boys - Curitiba PR", "2017-02-14" , "10h00", "0|0",
            "1,5km", R.drawable.event_1, 5));
    eventK.add(new EventK("Cosmic Boys - Curitiba PR", "2017-02-14" , "10h00", "0|0",
            "1,5km", R.drawable.event_1, 6));
    eventK.add(new EventK("Cosmic Boys - Curitiba PR", "2017-02-14" , "10h00", "0|0",
            "1,5km", R.drawable.event_1, 7));

    EventAdapter eventAdapter = new EventAdapter(this, eventK);

    ListView listView = (ListView)findViewById(R.id.list_events);
    listView.setAdapter(eventAdapter);
}

}

Here is the Adapter:

public class EventAdapter extends ArrayAdapter<EventK> {

public EventAdapter(Context context, ArrayList<EventK> eventK) {
    super(context, 0, eventK);
}

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

    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.event_item, parent, false);
    }

    EventK currentEvent = getItem(position);

    TextView nameTextView = (TextView) listItemView.findViewById(R.id.event_name);
    if (currentEvent != null) {
        nameTextView.setText(currentEvent.getEventName());

        //TextView hourTextView = (TextView) listItemView.findViewById(R.id.event_hour);
        //hourTextView.setText(currentEvent.getEventHour());

        TextView distanceTextView = (TextView) listItemView.findViewById(R.id.event_distance);
        distanceTextView.setText(currentEvent.getEventDistance());

        TextView priceTextView = (TextView) listItemView.findViewById(R.id.event_price);
        priceTextView.setText(currentEvent.getEventPrice());

        ImageView thumbImgView = (ImageView) listItemView.findViewById(R.id.event_thumb);
        thumbImgView.setImageResource(currentEvent.getEventThumb());

        TextView confirmedGestsTextView = (TextView) listItemView.findViewById(R.id.confirmes_event_gests);
        confirmedGestsTextView.setText(currentEvent.getConfirmedGuests());

    }
    return listItemView;

}

}
  • the error is giving on which line?

  • says you’re on the line: thumbImgView.setImageResource(currentEvent.getEventThumb());

  • It does not recognize the getResources method();

  • Good! It worked. Thanks!

1 answer

1


Use the method setImageDrawable() passing as parameter the getResource().getDrawable(). See how it should look:

Resources resources = context.getResources();
image.setImageDrawable(resources.getDrawable(currentEvent.ge‌​tEventThumb()));

See more details of the method in the documentation.

Browser other questions tagged

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