Reputation: 33876
I am new to programming and I am not sure I properly understand how to add TextView
programmatically. I am finding lots of people doing this but they use this
in the context. Which normally I understand, But in my case here, it is not going to work.
I am retrieving the Objects
's via parse.com, and trying to set the String
's in .findInBackground()
. Here is my code:
private void Retrieve2() {
final ParseObject Fighters = new ParseObject("FightersDB");
ParseQuery query = new ParseQuery("FightersDB");
query.whereEqualTo("Name", "The First Guy");
query.findInBackground(new FindCallback(){
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
Log.d("Status", "Retrieved suuccessfully");
String name, record, age;
name = Fighters.getString("Name");
age = Fighters.getString("Age");
record = Fighters.getString("Record");
set(name, record, age);
} else {
Log.d("Status", "Error: " + e.getMessage());
}
}
private void set(String name, String record, String age) {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
TextView tv = new TextView(this); //<---- RIGHT HERE IS MY PROBLEM
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT, (int)LayoutParams.WRAP_CONTENT);
params.leftMargin=0;
params.topMargin=80;
tv.setPadding(10, 0, 0, 0);
tv.setText("" + name + "" + record + "" + age);
tv.setTextSize((float) 20);
tv.setLayoutParams(params);
rl.addView(tv);
}
});
Where I noted in the code, I am getting an error that states The constructor TextView(new FindCallback(){}) is undefined
I don't know how to add a TextView
without using this
in the Context. Maybe I am going about it all wrong on how to add this.
So my question is what do I put in the context
to make this work? I am new to programming so please be thorough in your explanations.
Upvotes: 0
Views: 1206
Reputation: 18873
this
refers to the current object of the class. Usually you see many people use this when making a View programmatically in an Activity class :
TextView tv = new TextView(this);
this refers to Activity.this , and most View require Context. And Activity extends from Context, so you can just pass in the Activity as Context.
Your solution:
Currently you didn't show what class this is in, if this is an Activity class, just use this, if this is an innerClass of Activity class, use ActivityClass.this, if this is not an Activity class, you have to get the Context in the parameters of the method(or constructor/field);
Update Since you said its an inner class of Activity class, you can use ActivityClass.this to pass as Context :
TextView(ActivityClass.this);
Upvotes: 3
Reputation: 4007
Try using ActivityClass.this
, e.g.
TextView tv = new TextView(ActivityClass.this);
Upvotes: 2