阵列适配器未正常运行

Array Adapter is not functioning Correctly

我正在使用数组适配器根据用户输入填充列表视图。然而,阵列适配器将不起作用并使应用程序崩溃。我不知道为什么这样做,如果有人能解释原因,我将不胜感激。下面是我的 class 和我得到的错误。

我的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());


                for (int i = 0; i < 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();

            }
        });


    }
}

错误 错误:(62, 53) 错误:无法推断 ArrayAdapter<>

的类型参数

改变

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, sLog);

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, sLog);

您需要传递上下文。由于这是在闭包内,因此您需要明确 getActivity() 因为 this 不会为您提供上下文。

因为仅使用 this 指的是 View.OnClickListener class 而不是 activity class。 ArrayAdapter<> 需要 context 作为第一个参数,activity 可以在此处提供而不是 View.OnClickListener

所以这样试试:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(AddScore.this, android.R.layout.simple_list_item_1, sLog);
Log.d(TAG, "**************************");
lv.setAdapter(arrayAdapter);