如何通过用户ID获取常规聊天室(Listview)上的聊天位置项,以便可以与此用户打开私人聊天

时间:2018-08-30 01:39:33

标签: java android

我正在制作一个简单的聊天应用程序,需要通过用户ID在常规聊天室(Listview)上的Firebase数据库中通过用户ID获取用户ID的聊天位置,才能与该用户打开私人聊天。以及在哪里放置代码?请帮我。抱歉,我是Java的新手。

public class ChateRoom extends AppCompatActivity {

private DatabaseReference root;
private String temp_key;
private Button btn_send_msg;
private EditText input_msg;
private ListView listView_chat;
ArrayList<String> list_chat = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
private String name;

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

    root = FirebaseDatabase.getInstance().getReference().child("MainChatRoom");

    listView_chat = (ListView) findViewById(R.id.listView_chat);
    input_msg = (EditText) findViewById(R.id.input_msg);
    btn_send_msg = (Button) findViewById(R.id.btn_send_msg);

    name = FirebaseAuth.getInstance().getCurrentUser().getEmail();

    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_chat);
    listView_chat.setAdapter(arrayAdapter);

    btn_send_msg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Map<String, Object> map = new HashMap<String, Object>();
            temp_key = root.push().getKey();
            root.updateChildren(map);

            DatabaseReference message_root = root.child(temp_key);
            Map<String, Object> map2 = new HashMap<String, Object>();
            map2.put("name", name);
            map2.put("msg", input_msg.getText().toString());

            message_root.updateChildren(map2);
        }
    });


    root.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Add_Chat(dataSnapshot);
        }

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

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}


private String chat_msg, chat_user_name;

private void Add_Chat(DataSnapshot dataSnapshot) {

    Iterator i = dataSnapshot.getChildren().iterator();
    input_msg.setText("");
    while (i.hasNext()) {

        chat_msg = (String) ((DataSnapshot) i.next()).getValue();
        chat_user_name = (String) ((DataSnapshot) i.next()).getValue();

        list_chat.add(chat_user_name + " : " + chat_msg);
        arrayAdapter.notifyDataSetChanged();
        listView_chat.setSelection(list_chat.size());
    }


}

我找到了此方法并将其放在onCreate方法中

listView_chat.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent i = new Intent(Client.this, test.class);
        String uid = list_chat.get(position);
        i.putExtra("user_id", uid);
        startActivity(i);
    }
    });

但没有获得正确的用户ID

0 个答案:

没有答案