如何在Android的另一个活动中从吸气剂中检索数据

时间:2019-03-13 18:09:09

标签: android firebase android-activity android-lifecycle

我正在创建一个Android应用程序,该应用程序存储在firebase中,并在一个活动中显示带有子项的列表,然后通过单击按钮在另一个活动中访问该列表。我已经在firebase和第一个活动中存储并显示了数据,但在其他活动中却无法获取该列表。  我的代码:-

主要活动:-

public class MainActivity extends AppCompatActivity {
EditText getName, getNum;
Button btn,btnNext;
TextView textView;
ListView listView;
DatabaseReference fb;
List<Contact> contactList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getName = findViewById(R.id.ed1);
    getNum = findViewById(R.id.ed2);
    textView = findViewById(R.id.text);
    btn = findViewById(R.id.btn);
    btnNext = findViewById(R.id.btnNext);

    listView = findViewById(R.id.listView);

    fb = FirebaseDatabase.getInstance().getReference("Contacts");

    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
            startActivity(intent);
            finish();
        }
    });

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addContacts();
        }
    });

    fb.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Contact contact = dataSnapshot.getValue(Contact.class);
            contactList.add(contact);
            listadapter list = new listadapter(MainActivity.this,contactList);
            listView.setAdapter(list);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            notify();
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

public void addContacts() {
    String name = getName.getText().toString();
    String num = getNum.getText().toString();
    String id = fb.push().getKey();
    Contact contact = new Contact(name,num);
    fb.child(id).setValue(contact);
}
}

联系吸气剂课程

   public class Contact {

   String contact;
   String name;

public Contact() {  }

public Contact(String name, String contact) {
    this.name = name;
    this.contact = contact;
}

public String getName() {
    return name;
}

public String getContact() {
    return contact;
}
}

自定义适配器类

public class listadapter extends ArrayAdapter<Contact> {

private Activity context;
private List<Contact> contactList;

public listadapter(Activity context, List<Contact> contactList) {
    super(context,R.layout.list,contactList);

    this.context = context;
    this.contactList = contactList;
}

public View getView(int position, @Nullable View convertView, @NonNull   ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();

    View listViewItem = inflater.inflate(R.layout.list, null, true);

    TextView textViewName = listViewItem.findViewById(R.id.name);
    TextView textViewContact = listViewItem.findViewById(R.id.num);

    Contact contact = contactList.get(position);


    textViewName.setText(contact.getName());
    textViewContact.setText(contact.getContact());

    return listViewItem;
}
}

我要提取的活动

public class Main2Activity extends AppCompatActivity {
Button getSub;
List<Contact> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    getSub = findViewById(R.id.getSub);

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

            Toast.makeText(getApplicationContext(),"Num : "+list.get(0).getContact(),Toast.LENGTH_LONG).show();
        }
    });
}
}

解决方案:-我直接从firebase提取了列表,并且有效

        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        call = findViewById(R.id.call);

       db = FirebaseDatabase.getInstance().getReference("Trusted contacts");

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
    @Override
        public void run() {
            msgAll();
            for(int i=0;i<ex.size();i++) {
                getNum = "tel: "+ex.get(i).getmText2();
                callAll(getNum);

            }

        }
    }, 500);
}
       @Override
       protected void onStart() {
       super.onStart();
    db.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            ExampleItem contact = dataSnapshot.getValue(ExampleItem.class);
            ex.add(contact);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

1 个答案:

答案 0 :(得分:0)

Main2Activity中,您已将引用变量list创建为new ArrayList<>(),但从未对其添加任何内容。

如果您希望能够访问数据,则需要将其从一个Actvity传递到另一个,或将对它的引用存储在一个static变量中,您可以访问该变量来自两个班级。