public class HomeScreen_Contacts extends Fragment {

TextView noUsersText;

ProgressDialog mProgressDialogue;

FirebaseAuth mAuth;
FirebaseUser currentUser;
String UID;

String name;
String rId;
ArrayAdapter<String> adapter;

ListView usersList;
ArrayList<String> al = new ArrayList<>();
ListView listView;
DatabaseReference rootRef;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
    usersList = (ListView) rootView.findViewById(R.id.usersList);
    noUsersText = (TextView) rootView.findViewById(R.id.noUsersText);

    mAuth = FirebaseAuth.getInstance();
    currentUser = mAuth.getCurrentUser();
    UID = mAuth.getCurrentUser().getUid();

    mProgressDialogue = new ProgressDialog(getActivity());
    mProgressDialogue.setMessage("Loading...");
    mProgressDialogue.show();

    rootRef = FirebaseDatabase.getInstance().getReference();
    listView = (ListView) rootView.findViewById(R.id.usersList);



    readData(new MyCallback() {
        @Override
        public void onCallback(List<String> al) {
            for (String name : al) {
                Log.d("TAG", name);
            }
        }
    });

    usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(getActivity(), Chat.class);
            intent.putExtra("Recievers_Id", rId);
            intent.putExtra("Recievers_Name", name);
            Log.d("RID", rId);
            Log.d("RN", name);
            startActivity(intent);
        }
    });


    return rootView;

}


public void readData(final MyCallback myCallback) {
    DatabaseReference usersRef = rootRef.child("Users");
    ValueEventListener eventListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, al);
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                name = ds.child("Name").getValue().toString();
                rId = ds.child("Phone_Number").getValue().toString();
                al.add(name);


            }
            listView.setAdapter(adapter);
            mProgressDialogue.dismiss();
            myCallback.onCallback(al);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(getActivity(), "Please Check Your Internet Connection", Toast.LENGTH_SHORT).show();
        }
    };
    usersRef.addListenerForSingleValueEvent(eventListener);
}


public interface MyCallback {
    void onCallback(List<String> al);
}


}

无论我单击哪个项目,都会将我引导到同一房间。我想改变那个。我希望用户根据他在列表视图中单击的项目去另一个房间。我的逻辑正确吗?如果是,我哪里出错了

遵循了教程。不确定在myCallback下应该发生什么...将其留空...您能帮我吗

再次更新我的代码...有什么问题吗?

最佳答案

您不能简单地将rIdname声明为全局变量,而只能在onDataChange()之外使用它们。发生这种情况是因为onDataChange()具有异步行为,并且当您将这些变量添加到Intent对象时,这些变量尚未加载。

解决问题的快速方法是仅在onDataChange()方法中使用这些值,或者如果需要在外部使用它们,则需要创建自己的自定义回调,为此,我建议您从此,并同时查看此post

要获得被单击的项目,您需要使用以下方法之一:

adapter.getItem(position);


要么

al.get(position)


Edit1:根据您的最新修改,请按照以下步骤操作:

像这样更改界面:

public interface MyCallback {
    void onCallback(List<String> al);
}


然后在onDataChange()方法内的mProgressDialogue.dismiss();行之后,添加以下代码行:

myCallback.onCallback(al);


最后,像这样遍历ArrayList

readData(new MyCallback() {
    @Override
    public void onCallback(List<String> al) {
        for(String name : al) {
            Log.d("TAG", name);
        }
    }
})


Edit2:这是完整的代码。定义接口:

public interface MyCallback {
    void onCallback(List<String> al);
}


定义方法:

public void readData(final MyCallback myCallback) {
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usersRef = rootRef.child("Users");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<String> al = new ArrayList<>();
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.child("Name").getValue(String.class);
                String rId = ds.child("Phone_Number").getValue(String.class);
                al.add(name);
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, al);
            ListView listView = (ListView) rootView.findViewById(R.id.usersList);
            listView.setAdapter(adapter);
            mProgressDialogue.dismiss();
            myCallback.onCallback(al);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(getActivity(), "Please Check Your Internet Connection", Toast.LENGTH_SHORT).show();
        }
    };
    usersRef.addListenerForSingleValueEvent(eventListener);
}


然后只需在onCreate()方法中调用该方法:

readData(new MyCallback() {
    @Override
    public void onCallback(List<String> al) {
        for (String name : al) {
            Log.d("TAG", name);
        }
    }
});


输出应该是用户的全部名称。

09-27 00:13