When I try to access a list tournaments, which has been initialized in the following class:
public class AdminControl extends AppCompatActivity {
Firebase ref;
FloatingActionButton fab;
public static List <Tournament> tournaments;
ListView list;
Tournament [] temp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_control);
Toolbar b = (Toolbar) findViewById(R.id.toolbar);
b.setTitle("Tournaments");
setSupportActionBar(b);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
createNewTournament(v);
}
});
AdminLogin.firebase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
tournaments = (ArrayList) dataSnapshot.getValue();
Object [] objArr = tournaments.toArray();
temp = new Tournament[objArr.length];
for (int i = 0; i < objArr.length; i++){
temp[i] = (Tournament) objArr[i];
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
if (tournaments == null){
tournaments = new ArrayList<Tournament>();
}
list = (ListView) findViewById(R.id.listView);
ArrayAdapter<Tournament> tempAdapter = new ArrayAdapter<Tournament>(
this, android.R.layout.simple_list_item_1, temp);
list.setAdapter(tempAdapter);
}
public void createNewTournament(View v) {
Intent newIntent = new Intent(this, TournamentCreator.class);
startActivity(newIntent);
}
}
After I leave that task and instantiate a new activity, TournamentCreator by calling the createNewTournament method right above, I try to access the static list tournaments from the TournamentCreator activity. However, it is displayed as null (Object a). I thought that static variables would always remain constant. Is there some sort of problem in what I am doing?
Tournament Creator class
public class TournamentCreator extends AppCompatActivity {
private Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
Object a = AdminControl.tournaments;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tournament_creator);
b = (Button) findViewById(R.id.button3);
b.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
beginTournament(v);
}
});
}
public void beginTournament(View v) {
EditText text = (EditText) findViewById(R.id.editText);
Tournament newTourn = new Tournament(text.getText().toString());
AdminControl.tournaments.add(newTourn);
Firebase userRef = AdminLogin.firebase.child("users").child(AdminLogin.firebase.getAuth().getUid());
userRef.setValue(AdminControl.tournaments);
finish();
}
}
Related
So I am new and working on an app, so I have created homepage of it and then when we click on anyone of it it opens a new activity in which when clicking the button to get the answer doesnt work and it goes back to the homepage. Please help me solve this!
This is my code of main activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CardView Currency = (CardView) findViewById(R.id.Currency);
CardView Area = (CardView) findViewById(R.id.Area);
CardView Length = (CardView) findViewById(R.id.Length);
CardView Temperature = (CardView) findViewById(R.id.Temperature);
CardView Time = (CardView) findViewById(R.id.Time);
CardView Weight = (CardView) findViewById(R.id.Weight);
Currency.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Currency.class);
startActivity(intent);
}
});
Weight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Weight.class);
startActivity(intent);
}
});
Time.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Time.class);
startActivity(intent);
}
});
Length.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Length.class);
startActivity(intent);
}
});
Area.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Area.class);
startActivity(intent);
}
});
Temperature.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Temperature.class);
startActivity(intent);
}
});
}
And this is of my new activity to open at homepage and convert the value
public class Weight extends AppCompatActivity {
private TextView textView;
private Button button;
private EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weight);
button = findViewById(R.id.button);
textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(Weight.this, "Request Proceeded", Toast.LENGTH_SHORT).show();
String s = editText.getText().toString();
Integer.parseInt(s);
int kg = Integer.parseInt(s);
double pound = 2.205 * kg;
textView.setText("The corresponding value in Pound is" + pound);
}
});
}
I am trying just to pass the int user input to the next class and print it, see that it works before continuing on using it or something else.
Home.java
start and exit button
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) findViewById(R.id.b1);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToSecondActivity();
}
});
Button two = (Button) findViewById(R.id.b2);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
private void goToSecondActivity() {
Intent i = new Intent(this, SelectNumberOfPlayers.class);
startActivity(i);
}
}
SelectNumberOfPlayers.java
Taking only the numbers from the input and passing it to StartGame.class
public class SelectNumberOfPlayers extends AppCompatActivity {
EditText numberOfPlayers;
Button three;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_number_of_players);
three = (Button) findViewById(R.id.button3);
three.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String txt = numberOfPlayers.getText().toString();
Intent i = new Intent(getApplicationContext(), StartGame.class);
i.putExtra("players", txt);
startActivity(i);
}
});
}
}
StartGame.java
Receiving Int and printing to TextView
public class StartGame extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("Player number"));
}
}
Where is the error happening? I've set the input keyboard to take only numbers
1. you forgot to initialize youe editext first initialize it in your SelectNumberOfPlayers Activity
numberOfPlayers = (EditText) findViewById(R.id.numberOfPlayers);
2.
the key must be same to pass and receive data with intent
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("players"));
}
i coded this lines to change textview1 Continuous when user clicked button but it sucks at value 1 in output why :\
public class dobeyti extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = 0;
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Becuase you defined the num value inside the onclick method so it will be reassigned to 0 every time.
Just put int num=0 outside the method like below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
int num = 0;
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Move int num = 0; outside OnClickListener. Every time you click button value on variable num is set to 0 -> increased to 1 and set as text, that is why it stuck at 1.
int num = 0;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Hi i am new to programming. I want access arraylist outside from the onCreate but i get error.Below is code.
public class WelcomeOnFootActivity extends AppCompatActivity {
ImageButton next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
List<UserDate> data= new ArrayList();
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle();
}
});
}
private void handle(){
String info=data.getUserInfo;// here i get error
}
}
Just declare the list globally at class level. In your case before OnCreate.
List<UserDate> data= new ArrayList();
en#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle();
}
});
}
private void handle(){
String info=data.getUserInfo;
}
Hope it will help.
public class WelcomeOnFootActivity extends AppCompatActivity {
ImageButton next;
List<UserDate> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
data= new ArrayList();
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle();
}
});
}
private void handle(){
String info=data.getUserInfo;// here i get error
}
}
public class WelcomeOnFootActivity extends AppCompatActivity {
ImageButton next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
final List<UserDate> data= new ArrayList();
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle(data);
}
});
}
private void handle(List<UserDate> listUD){
String info=listUD.getUserInfo;// here i get error
}
}
[Here is a screenshot of my output][1]
I am trying to sort the elements of an array.
I have stored the elements to the array from EditText1(ed) and I want to sort them and display them in EditText2.
I am done with storing them and displaying them, i wanted to sort them using Collections.sort(array); but it shows me that there is something wrong.
This is my code so far:
public class MainActivity extends AppCompatActivity {
List<EditText> allEds = new ArrayList<EditText>();
EditText ed,ed2;
RelativeLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed= (EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
final Button b=(Button)findViewById(R.id.button);
container = (RelativeLayout)findViewById(R.id.rl);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
allEds.add(ed);
String[] strings = new String[allEds.size()];
for(int i=0; i < allEds.size(); i++){
strings[i] = allEds.get(i).getText().toString();
Log.e("My data", strings[i]);
ed2.setText(strings[i]);
}
}
});
[1]: http://i.stack.imgur.com/dowdE.jpg
I think this will probably work (replace the code in the onClick with this):
String[] strings = ed.getText().toString().split("\\r?\\n");
Arrays.sort(strings);
String output = TextUtils.join("\\n",strings);
ed2.setText(output);
may be something like this?
public class MainActivity extends AppCompatActivity {
List<String> allEds = new ArrayList<String>();
EditText ed,ed2;
RelativeLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed= (EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
final Button b=(Button)findViewById(R.id.button);
container = (RelativeLayout)findViewById(R.id.rl);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str = ed.getText().toString();
String lines[] = str.split("\\r?\\n");
Arrays.sort(lines);
ed2.setText(TextUtils.join("\n", lines));
}
});