有没有办法在onCreate方法之外引用变量?

时间:2019-12-04 03:36:03

标签: android global-variables pass-by-reference

我似乎无法在onCreate方法之外引用变量。有办法克服吗?我是学生,还在学习。如果有人可以帮助,那就太好了。谢谢。我想将“ url”变量引用到“ onItemClick”方法,它显示“无法解析符号”。

这是我的代码:

public class CominSoonActivity extends AppCompatActivity implements RecyclerViewClickItemInterface {

    DatabaseReference reference;
    RecyclerView recyclerView;
    ArrayList<Event> list;
    MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comin_soon);

        recyclerView = findViewById(R.id.myRecycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        list = new ArrayList<Event>();

        adapter = new MyAdapter(CominSoonActivity.this, list, this);
        recyclerView.setAdapter(adapter);

        reference = FirebaseDatabase.getInstance().getReference().child("EventDisplay");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
                {
                    Event e = dataSnapshot1.getValue(Event.class);
                    String url = (String) dataSnapshot1.child("website").getValue();
                    list.add(e);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(CominSoonActivity.this, "Opps.... Something went wrong", Toast.LENGTH_SHORT).show();

            }
        });

        //add back button
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

    }

    @Override
    public void onItemClick(int position) {
        //Uri uri = Uri.parse("http://fareezdanial19.wixsite.com/merlimaulibrary");
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);

    }

    //taking the user back to the previous activity
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home) {

            Intent homeActivity = new Intent(getApplicationContext(), HomeActivity.class);
            startActivity(homeActivity);
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


}

1 个答案:

答案 0 :(得分:0)

您可以这样做,

public class CominSoonActivity extends AppCompatActivity implements RecyclerViewClickItemInterface {

    DatabaseReference reference;
    RecyclerView recyclerView;
    ArrayList<Event> list;
    MyAdapter adapter;
    String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comin_soon);

        recyclerView = findViewById(R.id.myRecycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        list = new ArrayList<Event>();

        adapter = new MyAdapter(CominSoonActivity.this, list, this);
        recyclerView.setAdapter(adapter);

        reference = FirebaseDatabase.getInstance().getReference().child("EventDisplay");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
                {
                    Event e = dataSnapshot1.getValue(Event.class);
                    url = (String) dataSnapshot1.child("website").getValue();
                    list.add(e);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(CominSoonActivity.this, "Opps.... Something went wrong", Toast.LENGTH_SHORT).show();

            }
        });

        //add back button
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

    }

    @Override
    public void onItemClick(int position) {
        //Uri uri = Uri.parse("http://fareezdanial19.wixsite.com/merlimaulibrary");
        if(!url.isEmpty()){
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    }

    //taking the user back to the previous activity
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home) {

            Intent homeActivity = new Intent(getApplicationContext(), HomeActivity.class);
            startActivity(homeActivity);
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


}