未填充ArrayList和ListView

时间:2017-08-07 19:24:56

标签: android listview arraylist android-arrayadapter

我完全迷失了,不知道为什么我的ListView没有被填充。在我将数据插入ArrayList的方法中,它表示我的adapter符号无法解析,但据我所知,我已在onCreate中正确声明了适配器。

我是否正确假设我的活动中有ListView,请设置填充我的适配器的ArrayList,然后将该适配器绑定到我的ListView只要ListView收到信息,array会在我的活动中自动填充?

以下是我onCreate内的声明。

    ListView listView = (ListView) findViewById(R.id.dispScores);
    ArrayList<viewScores> savedScores = new ArrayList<viewScores>();
    ScoreAdapter adapter = new ScoreAdapter(this, savedScores);
    adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
    listView.setAdapter(adapter);

这些是我用来将数据添加到ArrayList

的方法
public View.OnClickListener onClickListener = new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        EditText input1 = (EditText) findViewById(R.id.scorePrompt);
        TextView output1 = (TextView) findViewById(R.id.textTotal);
        String blankCheck = input1.getText().toString(); //CHANGE INPUT IN scorePrompt TO STRING
        switch (view.getId()) {
            case R.id.buttTotal:
                    if (blankCheck.equals("")) {
                        Toast blankError = Toast.makeText(getApplicationContext(), "YOU CANT SKIP HOLES JERK", Toast.LENGTH_LONG);
                        blankError.show();
                        break;
                     } else {
                        int num1 = Integer.parseInt(input1.getText().toString()); //Get input from text box
                        int sum = num1 + score2;
                        score2 = sum;
                        output1.setText("Your score is : " + Integer.toString(sum));

                        //ATTEMPTING TO WRITE TO ARRAYLIST
                        viewScores addScore = new viewScores(input1.getText().toString());
                        adapter.notifyDataSetChanged();
                        adapter.add(addScore); //WHY DOESNT THIS WORK?
                        j++;
                        input1.setText(""); //Clear input text box
                        break;
                    }
            case R.id.allScores: //CHANGE THIS TO AN EDIT BUTTON, ADD A HOLE NUMBER COUNTER AT TOP OF SCREEN!!!!!
                output1.setText("you messed up");
                break;
            case R.id.editScore: //Need to set up Save Array before we can edit
                //CURRENTLY ONLY DISPLAYS THE LAST NUNMBER IN THE TEXTEDIT, NEED TO SET UP LISTVIEW!!!!!!
                for (int i=0; i < j; i++){
                //    output1.setText(savedScores.get(i));
                } break;
        }
    }
};

public class viewScores {
    public String hole;
    public viewScores(String holeNum) {
        this.hole = holeNum;
    }
}

public class ScoreAdapter extends ArrayAdapter<viewScores> {
    public ScoreAdapter(Context context, ArrayList<viewScores> scores) {
        super(context, 0, scores);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewScores score1 = getItem(position);
        if (convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
        }
        TextView ListView1 = (TextView) convertView.findViewById(R.id.dispScores);
        ListView1.setText(score1.hole);
        return convertView;
    }
}

应用程序没有崩溃,我可以在我的EditText中输入值。以下是ListView的XML。

 <ListView
    android:id="@+id/dispScores"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:layout_marginLeft="15dp"
    android:layout_marginStart="15dp"
    app:layout_constraintLeft_toLeftOf="parent"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintTop_creator="1"
    tools:layout_editor_absoluteY="244dp" />

1 个答案:

答案 0 :(得分:0)

每当您更新适配器观察到的模型时,都需要调用

adapter.notifyDataSetChanged()

所以在你的情况下,

viewScores addScore = new viewScores(input1.getText().toString());
savedScores.add(addScore);
adapter.notifyDataSetChanged()