2017-08-07 142 views
-1

我完全失去了,不知道爲什麼我的ListView沒有被填充。在我將數據插入到我的ArrayList中的方法中,它表示我的adapter符號無法解析,但據我所知,我在onCreate中正確聲明瞭適配器。ArrayList和ListView沒有被填充

我是在假設正確,如果我有一個ListView我的活動,建立了一個ArrayList填滿我的適配器的內部,再配合該適配器的ListViewListView將在我的活動自動填充每當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" /> 

回答

0

每次更新模型通過您的適配器觀察,需要調用

adapter.notifyDataSetChanged() 

所以你的情況,

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

對不起,我是新來的編碼和一切,但由模型你的意思是'ArrayList'?我不確定我應該在哪裏調用這個函數。 –

+0

是的,因爲您的適配器的物品根據你的ArrayList中,每次你添加到該ArrayList中,你的適配器應通知,一個新的項目已經通過adapter.notifyDataSetChanged() –

+0

另外,我認爲你應該添加到您的ArrayList添加,而不是實際的適配器。我已經更新了我的答案。 –