android - Array Adapter is not functioning Correctly -


i using array adapter populate listview user input. array adpter not work , crashes app. don't no why , appreciate if explain why. below class , error get.

my class

import android.content.intent; import android.os.bundle; import android.app.activity; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.listview; import android.widget.toast;  public class addscore extends appcompatactivity {     private listview lv;     private edittext player;     private edittext description;     private edittext winner;     private edittext game;     private button btn3;     private string tag = "hannah";      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_add_score);         // mysqlitehelper db = new mysqlitehelper(this);         lv = (listview) findviewbyid(r.id.lv);         player = (edittext) findviewbyid(r.id.player);         description = (edittext) findviewbyid(r.id.description);         winner = (edittext) findviewbyid(r.id.winner);         game = (edittext) findviewbyid(r.id.game);         btn3 = (button) findviewbyid(r.id.button3);           btn3.setonclicklistener(new view.onclicklistener() {              @override              public void onclick(view view) {                 list<string> slog = new arraylist<>();                 mysqlitehelper helper = new mysqlitehelper(view.getcontext());                 list slogs = helper.getallscores();                 slog.add(player.gettext().tostring().trim());                 slog.add(description.gettext().tostring().trim());                 slog.add(winner.gettext().tostring().trim());                 slog.add(game.gettext().tostring().trim());                   (int = 0; < slog.size(); i++) {                     score score = new score();                     slog.add(score.tostring());                 }                  //listview listview = (listview) findviewbyid(r.id.lv);                 arrayadapter<string> arrayadapter = new arrayadapter<>(this, android.r.layout.simple_list_item_1, slog);                 log.d(tag, "**************************");                 lv.setadapter(arrayadapter);                 score score = new score();                 arrayadapter.add(string.valueof(score));                  toast.maketext(addscore.this, "button clicked", toast.length_short).show();              }         });       } } 

error error:(62, 53) error: cannot infer type arguments arrayadapter<>

because using this refers view.onclicklistener class not activity class. , arrayadapter<> requires context first argument can provided here activity , not view.onclicklistener.

so try this:

arrayadapter<string> arrayadapter = new arrayadapter<>(addscore.this, android.r.layout.simple_list_item_1, slog); log.d(tag, "**************************"); lv.setadapter(arrayadapter); 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -