0

My xml has several buttons how do I when I click on a particular button load an arraylist in the listview

example

Button1 clicked -> arraylist <Questions> loads in listview1
Button2 clicked -> arraylist <Replies> loads in listview1
Button3 clicked -> arraylist <Null> loads in listview1

using the same listview, I'm currently doing this with several activitys created one for each button I think this is not a good practice.

1 Answer 1

2

In order to to do something when you press a button you need to add an OnClickListener to each of your buttons.

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            lv = (ListView) findViewById(R.id.your_list_view_id);
            ArrayList<String> list1 = new ArrayList();
            // This is the array adapter, it takes the context of the activity as a 
            // first parameter, the type of list view as a second parameter and your 
           // array as a third parameter.
           ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
             this, 
             android.R.layout.simple_list_item_1,
             your_array_list );

             lv.setAdapter(arrayAdapter); 
        }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you works perfectly, the source of the item is very weak you would know why? follows a print of how it is getting prntscr.com/id0x84 prntscr.com/id0xgx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.