如何调用另一个活动的对象?

时间:2018-05-05 19:11:08

标签: java android firebase

我想在databasereference活动中调用Firebase.java活动的showFullList.java变量,该怎么做?我必须使用RecyclerView在此活动中显示从firebase检索到的列表。请帮忙。代码在这里。

Firebase.java

公共类Firebase扩展了AppCompatActivity {

EditText EDname;
Button addB;
Spinner choose;

DatabaseReference databaseReference;

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

    databaseReference = FirebaseDatabase.getInstance().getReference("School");

    EDname= (EditText)findViewById(R.id.etname);
    addB= (Button)findViewById(R.id.buttonadd);
    choose = (Spinner)findViewById(R.id.spinnerchoose);

    addB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            addData();
        }
    });
}

private void addData()
{
    String name = EDname.getText().toString().trim();
    String section = choose.getSelectedItem().toString();

    if(!TextUtils.isEmpty(name))
    {

        String id = databaseReference.push().getKey();
        Model model = new Model(id,name,section);
        databaseReference.child(id).setValue(model);
        Toast.makeText(this,"Artist Added",Toast.LENGTH_LONG).show();


    }else
        {

            Toast.makeText(this,"Enter Name ",Toast.LENGTH_LONG).show();

        }
}

}

showFullList.java

public class ShowFullList extends AppCompatActivity {

    RecyclerView recyclerViewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_full_list);
        recyclerViewList = (RecyclerView)findViewById(R.id.recyclerview);

    }
}

如何使用onStart()addEventValueListener方法中访问firebase实例?

1 个答案:

答案 0 :(得分:1)

  1. 您可以在不同的活动中多次启动DatabaseReference
  2. 您不应该编写依赖代码。
  3. 对于未确保堆叠的活动。您应该创建新的变量实例,而不是使用以前的活动实例。
  4. 你可以像这样实现public class ShowFullList extends AppCompatActivity { RecyclerView recyclerViewList; DatabaseReference databaseReference; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_full_list); recyclerViewList = (RecyclerView)findViewById(R.id.recyclerview); databaseReference = FirebaseDatabase.getInstance().getReference("School"); } }

    for ($i = 0, $n = count($array_station) ; $i < $n ; $i++)
    {
        $station= curl_init();
        curl_setopt($station, CURLOPT_VERBOSE, true);
        curl_setopt($station, CURLOPT_URL, $array_station[$i]);
        curl_setopt($station, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($station, CURLOPT_FOLLOWLOCATION, true);
    
    
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    
        $response = curl_exec($station);
    
        if (curl_errno($station)){
            $response .= 'Retreive Base Page Error: ' . curl_error($station);
        }
        else {
          $skip = intval(curl_getinfo($station, CURLINFO_HEADER_SIZE)); 
          $head = substr($response ,0,$skip);
          $response = substr($response ,$skip);
          $info = var_export(curl_getinfo($station),true);
        }
        echo $head;
        echo $info;
    
    
    
        curl_close($station);
    }