K
K
Koshkasobaka2021-03-15 08:41:54
Java
Koshkasobaka, 2021-03-15 08:41:54

Why is ListView not responding to button click?

In the console, you can see that names is changing, but nothing happens on the emulator

public class MainActivity extends Activity {
    private int count = 1;
    private Button addOne;
    String[] names = {"0", "00", "000"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lvPyramid = (ListView) findViewById(R.id.lvPyramid);


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

        addOne = findViewById(R.id.b_addOne);

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (String name : names) {
                    String number = name.substring(0, 1);
                    String nextNumber = Integer.toString(count);
                    name = name.replace(number, nextNumber);
                }
                count++;
                if (count == 10) count = 0;
            }
        };

        addOne.setOnClickListener(onClickListener);

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-03-15
@Koshkasobaka

In the console, you can see that names is changing

Not true, this code does not change anything. Only for some reason reassigns a local variable. Start changing the list. Then call the adapter update method(notifyDatasetChanged). Then throw out the leaves and start using the RecyclerView already.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question