Footer button in listview, how to get value from custom list adapter

klijakub

I have a listview with switch button in each row. I store sum of checked switches in variable. In MainActivity where I start custom adapter I want to get a sum of checked switches. So how to pass value from custom adapter to parent activity? In the footer of listview I have a button "Dalej" I've create it in MainActivity, and want to get value (this sum) from adapter when user click on it?

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cardio);
    String[] cardio_1 = getResources().getStringArray(R.array.cardio_1);
    ListView listView = (ListView) findViewById(R.id.listView);

    View footer = getLayoutInflater().inflate(R.layout.cardio_footer, null);
    listView.addFooterView(footer);
    ListAdapter listAdapter = new Adapter_cardio(this, cardio_1);
    cardio.addAll(Arrays.asList(cardio_1))
    listView.setAdapter(listAdapter);

    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

    });

Adapter

   public Adapter_cardio(Context context,  String[] cardios ) {

    super(context, R.layout.cardio_row, cardios);
    this.context = context;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Log.d("TAG", "tessst1");
    zaznaczone = new ArrayList();
}

public String suma(String sum) {
    return sum;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    String pytanie = getItem(position);


    if (convertView==null){
        convertView = inflater.inflate(R.layout.cardio_row, null);
        holder = new ViewHolder();
        holder.question = (TextView) convertView.findViewById(R.id.question);
        holder.s = (Switch)convertView.findViewById(R.id.switch1);
        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }
    holder.question.setText(pytanie);
    //Model_cardio question = getItem(position);
    //final boolean doCheck = (position == 4) || (position == 5);


    holder.s.setTag(position);
    holder.s.setOnCheckedChangeListener(null);
    if (zaznaczone.contains(position) )
    {
        holder.s.setChecked(true);
    }
    else
    {
        holder.s.setChecked(false);
    }

    holder.s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                zaznaczone.add((Integer) buttonView.getTag());
            } else {
                zaznaczone.remove((Integer) buttonView.getTag());
            }

        }
    });
    sum = zaznaczone.size();
    String x = "2";
    Log.d("TAG_Switch_all", "suma = " + sum);

    return convertView;
}
ρяσѕρєя K

want to get value (this sum) from adapter when user click on it?

Create a method as public which return sum from Adapter_cardio class as:

public int getSum(){

 return zaznaczone.size();
}

and call getSum method on Button click using listAdapter object as:

public void onClick(View v) {
     Log.d("TAG_Switch_all", "suma = " + ((Adapter_cardio)listAdapter).getSum());
 }

if listAdapter not accessible inside onClick method then declare it as final

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get data from custom adapter in ListView?

From Dev

How to get specific button and edit its text from a row in custom adapter belonging to ListView in Android

From Dev

Refreshing ListView from Custom Adapter on Button Click

From Dev

How to show that a listView is empty with a custom list adapter

From Dev

How to show that a listView is empty with a custom list adapter

From Dev

How to get a button in my custom adapter for my listview to call a phone number

From Dev

How to get value from custom dropdown list?

From Dev

How to set on button clicked event for Randomly Custom ListView Adapter?

From Dev

How to get specific TextView (View ) from a ListView without use of custom Adapter?

From Dev

How to get data from ListView in Android when I use custom adapter

From Dev

how to set value list item in adapter class and get value on Button click in android

From Dev

how to set value list item in adapter class and get value on Button click in android

From Dev

Passing value from MainActivity to custom listview adapter class

From Dev

Android How to Identify Listview Button clicked event from outside adapter?

From Dev

How to refresh listview in a custom adapter?

From Dev

Animate button inside custom adapter for listview

From Dev

How to pass an Intent Extra from a custom ListView Adapter?

From Dev

How to get the items from adapter and store it in list?

From Dev

how to get the content of listview with a button outside the list

From Dev

How to get data from custom list view adapter and pass to intent put extra?

From Dev

Get list of items in custom adapter

From Dev

Saving and restoring ListView with Custom List Adapter

From Dev

android change list item in listview with custom adapter

From Dev

How to create custom adapter for custom listview with Hashmap

From Dev

How to get value of combobox from custom list view

From Dev

How to Load Custom Listview with a List from SQLite

From Dev

How to get tapped List Item position in Adapter class on button click

From Dev

Get "No results" Message Custom Adapter ListView Android

From Dev

Get "No results" Message Custom Adapter ListView Android

Related Related

  1. 1

    How to get data from custom adapter in ListView?

  2. 2

    How to get specific button and edit its text from a row in custom adapter belonging to ListView in Android

  3. 3

    Refreshing ListView from Custom Adapter on Button Click

  4. 4

    How to show that a listView is empty with a custom list adapter

  5. 5

    How to show that a listView is empty with a custom list adapter

  6. 6

    How to get a button in my custom adapter for my listview to call a phone number

  7. 7

    How to get value from custom dropdown list?

  8. 8

    How to set on button clicked event for Randomly Custom ListView Adapter?

  9. 9

    How to get specific TextView (View ) from a ListView without use of custom Adapter?

  10. 10

    How to get data from ListView in Android when I use custom adapter

  11. 11

    how to set value list item in adapter class and get value on Button click in android

  12. 12

    how to set value list item in adapter class and get value on Button click in android

  13. 13

    Passing value from MainActivity to custom listview adapter class

  14. 14

    Android How to Identify Listview Button clicked event from outside adapter?

  15. 15

    How to refresh listview in a custom adapter?

  16. 16

    Animate button inside custom adapter for listview

  17. 17

    How to pass an Intent Extra from a custom ListView Adapter?

  18. 18

    How to get the items from adapter and store it in list?

  19. 19

    how to get the content of listview with a button outside the list

  20. 20

    How to get data from custom list view adapter and pass to intent put extra?

  21. 21

    Get list of items in custom adapter

  22. 22

    Saving and restoring ListView with Custom List Adapter

  23. 23

    android change list item in listview with custom adapter

  24. 24

    How to create custom adapter for custom listview with Hashmap

  25. 25

    How to get value of combobox from custom list view

  26. 26

    How to Load Custom Listview with a List from SQLite

  27. 27

    How to get tapped List Item position in Adapter class on button click

  28. 28

    Get "No results" Message Custom Adapter ListView Android

  29. 29

    Get "No results" Message Custom Adapter ListView Android

HotTag

Archive