Not entirely sure if I'm using those terms (context, helper class) correctly, so excuse me if I'm not.
Anyway, my issue is that I have a class chessboard which needs to call a method in another class, this being the class for the 'knight' piece. The method in this knight class generates a list of possible moves that the knight could make given its current position on the chessboard.
Once the 'possibleMoves' method (located in the knight class) is called, I want it to generate a Toast that will be displayed. However I'm having trouble filling the Toast with a context to use. I'm not sure how to pass the context of the chessboard to the knight class when it's called.
I hope that makes sense.
My code is below -
chessboard portion:
final ImageView s01 = (ImageView) findViewById(R.id.s01);
s01.setClickable(true);
s01.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (s01.getDrawable() != (null)) {
String Piece = s01.getTag().toString();
switch (Piece) {
case "black_knight":
knight blackKnight = new knight();
blackKnight.possibleMoves(0, 1);
break;
}
}
}
});
knight class:
public class knight extends piece {
static String chessBoard[][] = chessboard.chessBoard;
//private static Context boardContext;
@Override
public String possibleMoves(int r, int c) {
String list="", oldPieceRecord;
for (int j=-1; j<=1; j+=2) {
for (int k=-1; k<=1; k+=2) {
try {
if (Character.isUpperCase(chessBoard[r+j][c+k*2].charAt(0)) || " ".equals(chessBoard[r+j][c+k*2])) {
oldPieceRecord=chessBoard[r+j][c+k*2];
chessBoard[r][c]=" ";
chessBoard[r+j][c+k*2]="k";
if (kingSafe()) {
list=list+r+c+(r+j)+(c+k*2)+oldPieceRecord+(oldPieceRecord.equals(" ") ? "": " ");
}
chessBoard[r][c]="k";
chessBoard[r+j][c+k*2]=oldPieceRecord;
}
} catch (Exception e) {}
try {
if (Character.isUpperCase(chessBoard[r+j*2][c+k].charAt(0)) || " ".equals(chessBoard[r+j*2][c+k])) {
oldPieceRecord=chessBoard[r+j*2][c+k];
chessBoard[r][c]=" ";
chessBoard[r+j*2][c+k]="k";
if (kingSafe()) {
list=list+r+c+(r+j*2)+(c+k)+oldPieceRecord;
}
chessBoard[r][c]="k";
chessBoard[r+j*2][c+k]=oldPieceRecord;
}
} catch (Exception e) {}
}
}
Toast.makeText( ,list, Toast.LENGTH_LONG).show();
return list;
}
I understand that some context indicator should go before the comma in the Toast.
You can require a Context object in the constructor:
private Context mContext;
public knight(Context context)
{
mContext=context;
super();
}
...
Toast.makeText(mContext ,list, Toast.LENGTH_LONG).show();
As was said in the comments by peace walker and codeMagic, you can instantiate this object like so:
In a Fragment:
knight blackKnight = new knight(getActivity());
In an Activity :
knight blackKnight = new knight(ChessboardActivity.this);
or
knight blackKnight = new knight(View.getContext());
Hope i can help you (i don't know english). You can create constructor of class knight like this.
public class knight extends piece {
static String chessBoard[][] = chessboard.chessBoard;
private Context mContext;
public knight(Context mContext){
this.mContext = mContext;
}
// your code
}
and :
knight blackKnight = new knight(yourContext);
Now you can use context in your class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.