只是想知道我的列表视图是否将用户引导到每个项目的新屏幕

时间:2018-03-26 15:10:46

标签: java android database firebase firebase-realtime-database

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应该包含什么......把它留空......你可以帮我解决一下吗

再次更新我的代码......有什么不对吗?

2 个答案:

答案 0 :(得分:1)

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

快速解决您的问题的方法是仅在onDataChange()方法中使用这些值,或者如果您在外面需要它们,则需要创建自己的自定义回调,为此,我建议您查看最后一部分我来自 post 的广告,并看一下 video

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

adapter.getItem(position);

al.get(position)

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

更改界面如下:

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);
        }
    }
});

输出应该是您所有用户的名称。

答案 1 :(得分:0)

使用onItemclickListener中的索引从数据集中获取rId和name的值。 目前,您为每个点击的项目传递存储在rId和name变量中的值,这就是为什么您总是得到相同的值