0

I want to be able to strike through my child item. I found this on here:

textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

But I'm confused as to how I can get the child. I have the event listener set already for when its tapped. I'm using AndroidHive's Expandable List View

Edit: Currently getting a cast exception for linearlayout to textview.

MainActivity.java (See the commented section "Right Here" below in the first method, onCreate):

package com.example.groceryrunnerv4;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;


public class MainActivity extends Activity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

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

        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);

        // Listview Group click listener
        /*expListView.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                 Toast.makeText(getApplicationContext(),
                 "Group Clicked " + listDataHeader.get(groupPosition),
                 Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Listview Group collapsed listener
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Collapsed",
                        Toast.LENGTH_SHORT).show();

            }
        });*/

        // Listview on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // RIGHT HERE --------------------------------------------
                            // RIGHT HERE --------------------------------------------
                ((TextView) v).setPaintFlags(((TextView) v).getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition)   .setPaintFlags(CHILD.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                return false;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


     // Adds food group data 
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data groups
        listDataHeader.add("Produce");
        listDataHeader.add("Grains");
        listDataHeader.add("Meat & Seafood");
        listDataHeader.add("Frozen");
        listDataHeader.add("Canned");
        listDataHeader.add("Bakery");
        listDataHeader.add("Beverages");
        listDataHeader.add("Other");

        // Adding child data items
        List<String> Produce = new ArrayList<String>();
        Produce.add("Chaquita Bananas");
        Produce.add("Apples (8)");
        Produce.add("Kiwi");
        Produce.add("Romaine Lettuce (3)");

        List<String> Grains = new ArrayList<String>();
        Grains.add("Whole Grain Bread");
        Grains.add("Whole Wheat English Muffins");
        Grains.add("Pasta");
        Grains.add("Oatmeal");

        List<String> MeatSeafood = new ArrayList<String>();
        MeatSeafood.add("My dead friends");

        List<String> Frozen = new ArrayList<String>();
        Frozen.add("Edamame");
        Frozen.add("Bean Burgers");

        List<String> Canned = new ArrayList<String>();
        Canned.add("Amy's Lentils");
        Canned.add("Jam");
        Canned.add("Peanu Butter");

        List<String> Bakery = new ArrayList<String>();
        Canned.add("Fresh Bread");

        List<String> Beverages = new ArrayList<String>();
        Canned.add("Water");

        listDataChild.put(listDataHeader.get(0), Produce);
        listDataChild.put(listDataHeader.get(1), Grains);
        listDataChild.put(listDataHeader.get(2), MeatSeafood);
        listDataChild.put(listDataHeader.get(3), Frozen);
        listDataChild.put(listDataHeader.get(4), Canned);
        listDataChild.put(listDataHeader.get(5), Bakery);
        listDataChild.put(listDataHeader.get(6), Beverages);
    }

    // Method for activity events
    public void onButtonClick(View v) {
        final int id = v.getId();
        switch (id) {
        case R.id.CreateLG:
            createLGPopup(v);
            break;
        case R.id.EditButton:
            createEditButtonPopup(v);
            break;
        case R.id.SaveButton:
            Toast.makeText(getApplicationContext(), "List saved.",
                    Toast.LENGTH_SHORT).show();
            break;
        case R.id.ListButton:
            // chooseListDialog()
        }
    }
    // findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
    // TextView text = (TextView) findViewById(R.id.GetStarted);
    // text.setText(choice);

    // CreateLG Button's Popup Menu
    public void createLGPopup(View v) {
        PopupMenu LGMenu = new PopupMenu(this, v);
        LGMenu.getMenuInflater().inflate(R.menu.createlg_menu, LGMenu.getMenu());
        LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                String choice = new String((String) item.getTitle());
                if (choice.equals("Create List")) {
                    createListDialog();
                }
                else if (choice.equals("Create Group")) {
                    createGroupDialog();
                }
                return false;
            }
        });
        LGMenu.show();
    }

    // Create Edit Button's Popup Menu
        public void createEditButtonPopup(View v) {
            PopupMenu EditMenu = new PopupMenu(this, v);
            EditMenu.getMenuInflater().inflate(R.menu.editlist_menu, EditMenu.getMenu());
            EditMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    String choice = new String((String) item.getTitle());
                    if (choice.equals("Edit List Name")) {
                        editListDialog();
                    }
                    else if (choice.equals("Clear All Items")) {
                        Toast.makeText(getApplicationContext(), "All list items deleted.",
                                Toast.LENGTH_SHORT).show();
                    }
                    else if (choice.equals("Delete List")) {
                        TextView text = (TextView) findViewById(R.id.ListName);
                        text.setText("Grocery Runner");
                        Toast.makeText(getApplicationContext(), "\"" + text.getText().toString() + "\" list edited.",
                                Toast.LENGTH_SHORT).show();
                    }
                    return false;
                }
            });
            EditMenu.show();
        }

        // Create List Dialog
        public AlertDialog.Builder dialogBuilder;
        private void createListDialog() {
            dialogBuilder = new AlertDialog.Builder(this);
            final EditText textInput = new EditText(this);      
            dialogBuilder.setTitle("Create new list");
            dialogBuilder.setMessage("Name your list: ");
            dialogBuilder.setView(textInput);
            dialogBuilder.setPositiveButton("Create", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    TextView text = (TextView) findViewById(R.id.ListName);
                    text.setText(textInput.getText().toString());
                    Toast.makeText(getApplicationContext(), "\"" + textInput.getText().toString() + "\" list created.",
                            Toast.LENGTH_SHORT).show();

                    //add list to ListsButton
                }
            });
            dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "Cancelled.",
                            Toast.LENGTH_SHORT).show();
                }
            });
            // Output
            AlertDialog dialogue = dialogBuilder.create();
            dialogue.show();
        }

        // Create Group Dialog
        private void createGroupDialog() {
            dialogBuilder = new AlertDialog.Builder(this);
            final EditText textInput = new EditText(this);      
            dialogBuilder.setTitle("Create new group");
            dialogBuilder.setMessage("Name your group: ");
            dialogBuilder.setView(textInput);
            dialogBuilder.setPositiveButton("Create", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    createGroup(textInput.getText().toString());
                    Toast.makeText(getApplicationContext(), "\"" + textInput.getText().toString() + "\" group created.",
                            Toast.LENGTH_SHORT).show();
                }
            });
            dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "Cancelled.",
                            Toast.LENGTH_SHORT).show();
                }
            });
            // Output
            AlertDialog dialogue = dialogBuilder.create();
            dialogue.show();    
        }

        public void createGroup(String inputGroup){
            listDataHeader = new ArrayList<String>();
            listDataChild = new HashMap<String, List<String>>();
            // Adding child data group
            listDataHeader.add(inputGroup);
            // Adding child data items
            List<String> group = new ArrayList<String>();
            Integer groupIndex = listDataHeader.indexOf(inputGroup);
            listDataChild.put(listDataHeader.get(groupIndex), group);
        }

        // Create List Dialog
        private void editListDialog() {
            dialogBuilder = new AlertDialog.Builder(this);
            final EditText textInput = new EditText(this);      
            dialogBuilder.setTitle("Edit list name");
            dialogBuilder.setMessage("Name your list: ");
            dialogBuilder.setView(textInput);
            dialogBuilder.setPositiveButton("Create", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    TextView text = (TextView) findViewById(R.id.ListName);
                    text.setText(textInput.getText().toString());
                    Toast.makeText(getApplicationContext(), "\"" + textInput.getText().toString() + "\" list edited.",
                            Toast.LENGTH_SHORT).show();

                }
            });
            dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "Cancelled.",
                            Toast.LENGTH_SHORT).show();
                }
            });
            // Output
            AlertDialog dialogue = dialogBuilder.create();
            dialogue.show();
        }

}

user2100364
  • 87
  • 1
  • 1
  • 15
  • 1
    The `TextView` you are interested in is probably the `View v` that is passed into `onChildClick`. You might need to cast it to a `TextView` before you call the `setPaintFlags` method. – NasaGeek Jan 27 '14 at 07:50
  • Ok that worked to build but I got this error saying that I can't cast: android.widget.LinearLayout cannot be cast to android.widget.TextView ----- Here's the line currently: ((TextView) v).setPaintFlags(((TextView) v).getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); – user2100364 Jan 27 '14 at 17:57
  • 1
    ah, that probably means your row layout is a `LinearLayout`. Just call `findViewById(R.id.yourtextviewid)` to the `TextView`. – NasaGeek Jan 27 '14 at 18:13
  • aha! That worked! Only thing is, for some reason it will only cross out the first item, but no other items, even though I gave it the ID. Kinda stumped.. any clue as to why this would happen? – user2100364 Jan 27 '14 at 19:30

1 Answers1

0

probably, when the onChildClicked is called, you are getting the whole item, which is a linear layout containing the TextView. so you need to find the child textview of the clicked linear layout and then apply paintFlags on it.

gaurav5430
  • 9,899
  • 5
  • 32
  • 73
  • Ok I'm try that out. I do know that each child is initially a string added to an arraylist in the PrepareListData method. – user2100364 Jan 27 '14 at 18:18