1

EDIT: Found my answer in this post: https://stackoverflow.com/a/28304164/2911440


I'm trying to initialize functions from a button inside a CardView.

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="24sp"

<Button
    android:id="@+id/btnStart"
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="startFunctionClick"
    android:text="START" />

But in my attempt to find an identifier for which function/class to run(that is, which button I have pressed) it gave the same result for all four buttons. I'm creating the cards from this dataset:

private String[] dataSet = {"Face Detection", "Circle Detection", "Foreground Detection", "Color Detection"};

I'm trying to use the TextView on the same card to identify which class to initialize. But the Toast will only output "Face Detection"

public void startFunctionClick(View v) {
    TextView t = (TextView) findViewById(R.id.title);
    String s = t.getText().toString();
    Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
    //initialize class....
}

Any ideas to what I can do different?

Community
  • 1
  • 1
simen
  • 11
  • 5

1 Answers1

0

So I found this post that answered my question perfectly. What I needed to do is create a custom OnClickListener interface and present an instance of it in the constructor.

public interface OnItemClickListener {
    public void onItemClick(View v, int pos);
}

public MainMenuAdapter(Context context, String[] dataSet, Boolean isNative, OnItemClickListener mOnItemClickListener){
    this.context = context;
    this.dataSet = dataSet;
    this.isNative = isNative;
    this.mOnItemClickListener = mOnItemClickListener;
}

Next I add the clickListener to the correct object in the onBindViewHolder-method.

cardViewHolder.btnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mOnItemClickListener.onItemClick(v, position);
        }
    });

In the activity I then passed the OnItemClickListener when i initialized the adapter.

mAdapter = new MainMenuAdapter(MainMenuActivity.this, dataSet, isNative, new MainMenuAdapter.OnItemClickListener(){
        @Override
        public void onItemClick(View v, int pos) {
            Intent intent = new Intent();
            if (pos == 0){
                intent = new Intent(MainMenuActivity.this, FaceDetection.class);
            } else if (pos == 1){
                intent = new Intent(MainMenuActivity.this, CircleDetection.class);
            } else if (pos == 2){
                intent = new Intent(MainMenuActivity.this, foregroundDetection.class);
            } else if (pos == 3){
                intent = new Intent(MainMenuActivity.this, Drawtivity.class);
            } else {
                Toast.makeText(getApplicationContext(), "Invalid function", Toast.LENGTH_SHORT).show();
            }
            startActivity(intent);
        }
    });
Community
  • 1
  • 1
simen
  • 11
  • 5