I’m trying to pass ArrayList of numbers to a textView in ArrayAdapter, but the app goes not responding

Here in MainActivity.java, i have created a ArrayList which has integer stored into it. When the app is run, it immediately stops responding. Dont know where I’m doing mistake and has a doubt whether TextView doesnt display integer directly.

MainActivity.java:

     public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            ArrayList<Numbers> numbers = new ArrayList<>();
            numbers.add(new Numbers(1));
            numbers.add(new Numbers(2));
            numbers.add(new Numbers(3));
            numbers.add(new Numbers(4));
            numbers.add(new Numbers(5));
            numbers.add(new Numbers(6));
            numbers.add(new Numbers(7));
    
    
            NumberAdapter numberAdapter = new NumberAdapter(MainActivity.this, numbers);
            ListView listView = (ListView) findViewById(R.id.listview_flavor);
            listView.setAdapter(numberAdapter);
        }
        }

NumbersAdapter: public class NumberAdapter extends ArrayAdapter {

        public NumberAdapter(Activity context, ArrayList<Numbers> numbers) {

            super(context, 0, numbers);
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Check if the existing view is being reused, otherwise inflate the view
            View listItemView = convertView;
            if(listItemView == null) {
                listItemView = LayoutInflater.from(getContext()).inflate(
                        R.layout.list_item, parent, false);
            }
            Numbers currentNumber = getItem(position);
            TextView nameTextView = (TextView) listItemView.findViewById(R.id.version_name);
            nameTextView.setText(currentNumber.getNumbers());

            return listItemView;
        }

    }

Answer

You are setting integer value on Textview which is wrong you have to set string value please replace

    nameTextView.setText(currentNumber.getNumbers());

with

nameTextView.setText(String.valueOf(currentNumber.getNumbers()));

Leave a Reply

Your email address will not be published. Required fields are marked *