2

I wrote a compound component and was adding a custom listener to react.

Inside the class for the compound component which uses an xml file.

public class VerticalCounterBlock extends LinearLayout {

    public interface VerticalCounterBlockListener {
        public void onCountChanged(int newCount);
    }

    private VerticalCounterBlockListener mVerticalCounterBlockListener = null;

    public void setVerticalCounterBlockListener(VerticalCounterBlockListener listener){
        mVerticalCounterBlockListener = listener;
    }

    // ... Other functions
}

I got my interface, I got the listener and I got the setter and I engage the listener like this in the button I have in the compound component. I can see that toast that is showing there when I test

addBtn = (Button)findViewById(R.id.btn_addcount);
addBtn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        count++;
        counttv.setText(String.format("%1$d", count));
        Toast.makeText(getContext(), "VCB", Toast.LENGTH_SHORT).show();
        if(mVerticalCounterBlockListener != null) {
            mVerticalCounterBlockListener.onCountChanged(count);
        }
    }
});

In my main activity

m20_vcb = (VerticalCounterBlock) findViewById(R.id.vcb_m20);
m20_vcb.setVerticalCounterBlockListener(new VerticalCounterBlock.VerticalCounterBlockListener() {
    @Override
    public void onCountChanged(int newCount) {
        increasePreachCountTotal();
        Toast.makeText(CounterActivity.this, String.format("%1$d", newCount), Toast.LENGTH_SHORT).show();
    }
});

I do not see that toast nor does it engage the function call. What am I missing?

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
John Bravado
  • 137
  • 4
  • 10

2 Answers2

0

I can suggest you several improvement scope here mainly restructuring the current format.

Lets not keep the interface as a inner class. So here's your VerticalCounterBlockListener.java

public interface VerticalCounterBlockListener {
    public void onCountChanged(int newCount);
}

Now implement this interface in your MainActivity

public class MainActivity implements VerticalCounterBlockListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        m20_vcb = (VerticalCounterBlock) findViewById(R.id.vcb_m20);
        m20_vcb.setVerticalCounterBlockListener(this);
    }

    // ... Other methods

    // Override the onCountChanged function. 
    @Override
    public void onCountChanged(int newCount) {
        increasePreachCountTotal();
        Toast.makeText(CounterActivity.this, String.format("%1$d", newCount), Toast.LENGTH_SHORT).show();
    }
}

You might consider removing the Toast from the addBtn click listener which might create exception.

addBtn = (Button)findViewById(R.id.btn_addcount);
addBtn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        count++;
        counttv.setText(String.format("%1$d", count));

        if(mVerticalCounterBlockListener != null) {
            mVerticalCounterBlockListener.onCountChanged(count);
        }
    }
});
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • All good suggestions. I tried your first and used a separate interface class. That did not help so I moved out back for my sanity and organization preference. Would suggestion 2/3 cause issues with listener? I only used toast because my debugger got broke and kept disconnecting. – John Bravado Dec 31 '16 at 04:00
  • The code segment 2 shows keeping a public listener inside your `Activity`. Did you try that too? – Reaz Murshed Dec 31 '16 at 04:02
  • I did try that but it did not work. Is there something with the linear layout that would prevent operation? – John Bravado Dec 31 '16 at 04:16
  • This should work. But you might consider adding the `onClickListener` of the `addBtn` in the `MainActivity` – Reaz Murshed Dec 31 '16 at 05:44
  • It does work i had to restart my computer i think the android debugger and interface and the overwriting of the app got messed up. when i woke up i turned on computer and uninstalled app on phone and tried again and it worked. – John Bravado Dec 31 '16 at 13:18
0

This was good there was something wrong with my system. i uninstaklled app and restarted computer and it worked as expected.

John Bravado
  • 137
  • 4
  • 10