0

I am attempting to create a ListView to display values entered via an EditText. I am using an ArrayList and ArrayAdapter but I am afraid I don't fully understand how they work.

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.listView1, num1);

I am unsure why I am unable to use android.R.id.listView1 where listView1 is the id of my list view in the activity. Is this not the resourceid that the adapter needs to list off my ArrayList?

Below is my full method and delcarations. Sorry if I am being vague in my questions, I don't fully know which terminology to use for what and I don't intend to cause confusion.

    public ArrayAdapter<String> adapter;
ArrayList<String> allScores = new ArrayList<>();
ListView listScores = (ListView)findViewById(R.id.listView1);

public void onButtonClick(View V){
    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

    TextView output2 = (TextView) findViewById(R.id.custName); //TEST FOR ARRAY LIST DISPLAY
    if (blankCheck.equals("")) {
        Toast blankError = Toast.makeText(getApplicationContext(), "YOU CANT SKIP HOLES JERK", Toast.LENGTH_LONG);
        blankError.show();
    } else {
        //savedScores.add(input1.getText().toString());//Save input into array list
        int num1 = Integer.parseInt(input1.getText().toString()); //Get input from text box
        int sum = num1 + score2;
        score2 = sum;

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.listView1, num1);

        output1.setText("Your score is : " + Integer.toString(sum));
        input1.setText(""); //Clear input text box

    }
};

For some background information on my intentions, I want the user to enter integers in an EditText, save these values in an ArrayList, and then populate a ListView line-by-line with the values the user entered. Thank you for the help.

Pallavi Tapkir
  • 781
  • 7
  • 28
Raymond Nguyen
  • 65
  • 2
  • 10
  • It might help you... https://stackoverflow.com/questions/19079400/arrayadapter-in-android-to-create-simple-listview – Mohd Saquib Aug 04 '17 at 05:21

2 Answers2

0

Try This way.

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

ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(itemsAdapter);
DAS
  • 442
  • 1
  • 6
  • 21
0

here you have to use setadapter of listview not put in the adapter

try this

  adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,allscores);
  listscores.setAdapter(adapter)

if you want to add input things than make an Arraylist and simply pass it in adapter's third parameter

jigar savaliya
  • 474
  • 1
  • 8
  • 21
  • Thank you! This seemed to work, but now I am encountering another problem. My app instantly crashes when started. I parsed through the code and found that it occurs when I am creating my "ListView". I am not sure why this is. – Raymond Nguyen Aug 05 '17 at 01:55
  • Hey Jigar. I actually was able to find a solution to my problem. I was unaware that I had to initialize my `ArrayList` inside of my onCreate method. I am going to mess around with this and hopefully get it to work. Thank you for everything! – Raymond Nguyen Aug 05 '17 at 06:35