DinoArmadillo
DinoArmadillo

Reputation: 41

Android Studio OnClickListener in Java instead of XML due to fragment

I've been trying to implement a simple SQLite db into a fragment of my application I am creating. Ive been informed that due to it being a fragment I cannot have my onClicks in my XML as the Java cannot find them, so for a while iv been searching for methods of implementing my buttons directly into the Java instead and maybe even the input and result text if necessary but all information I have found have been specific to Activities and everything else I receive that Cannot Resolve Symbol “OnClickListener”If anyone has any ideas it would be much appreciated

package com.test.test.app;
import android.os.Bundle;
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
  * A simple {@link Fragment} subclass.
  */
public class MemoFragment extends Fragment{
   EditText Input;
   TextView LyricText;
   MyDBHandler dbHandler;
   Button addButtonClicked;
   Button deleteButtonClicked;

   public MemoFragment() {
       // Required empty public constructor
   }
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       // Inflate the layout for this fragment
       View v = inflater.inflate(R.layout.fragment_memo, container, false);

       Input = (EditText) v.findViewById(R.id.Input);
       LyricText = (TextView) v.findViewById (R.id.LyricText);
       dbHandler = new MyDBHandler(getActivity() ,null, null, 1);
       printDatabase ();
       return v;
   }

   //add lyric to database
   public void addButtonClicked(View view){
       Lyrics lyrics = new Lyrics(Input.getText().toString());
       dbHandler.addLyric(lyrics);
       printDatabase();
   }

   //delete items
   public void deleteButtonClicked(View view){
       String inputtext = Input.getText().toString();
       dbHandler.deleteLyrics(inputtext);
       printDatabase();


   }

   public void printDatabase(){
       String dbString = dbHandler.databaseToString();
       LyricText.setText(dbString);
       Input.setText("");
   }

}

Upvotes: 2

Views: 56

Answers (2)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20646

You can do a simple onClickListener() for your Button, follow this :

addButtonClicked = (Button) v.findViewById(R.id.YOURID);

Then you can do this :

addButtonClicked.setOnClickListener(getActivity()); 

But you have to implements OnClickListener so it will generate this method :

@Override
public void onClick(View v) {

  int id = v.getId();
   switch(id){
   ....
 }

}

And you'll be able to do whatever you want when a Button is clicked.

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317760

Say, for example, in your onCreateView() of the fragment, you found a button:

Button button = (Button) v.findViewById(R.id.button);

You could attach a click listener to it like this:

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Code here to handle the click
    }
});

Upvotes: 1

Related Questions