gpt4 book ai didi

java - Android studio : CutomListView gives strange output, 它应该提供 firebase 数据。有什么解决办法吗?

转载 作者:行者123 更新时间:2023-12-02 09:57:25 26 4
gpt4 key购买 nike

虽然我想制作一个聊天应用程序,但我对 android studio 开发还很陌生。使用 Firebase 身份验证的登录和注册运行良好,一般聊天也是如此。但我想创建一个更好的外观,并搜索了如何制作自定义 ListView 的方法。但没有教程展示使用 firebase 数据的方法,所以我拼命地自己尝试。可悲的是,它在我的手机上给了我一个奇怪的输出。它在 ImageView 中显示标准启动器图标,该图标应该是用户的个人资料图片。一个 TextView 显示一些奇怪的输出,例如: com.glowchat.app.DataClass@3012dc4 -> Glowchat 是我的应用程序的名称。 DataClass 是我的包 com.glowchat.app 中的一个类。第二个 TextView 不显示任何更改,如果文本更改不起作用,它应该显示“[Message]”。我确信下面的代码完全是垃圾,但感谢您查看它并尝试帮助我。

PS:我保留了旧的标准适配器,以便在它们之间轻松切换以进行测试,所以不要对此感到困惑。

TestserverActivity.java

package com.glowchat.app;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.net.URL;
import java.util.ArrayList;

public class TestserverActivity extends AppCompatActivity {

private Button sendbutton;
private EditText sendmessage;
private ListView data;

private ArrayList<String> arrayList = new ArrayList<>();
private ArrayList<DataClass> arrList = new ArrayList<>();
private ArrayAdapter<String> adapter;
private ArrayAdapter<DataClass> adap;

private DatabaseReference database;
private FirebaseUser user;
private FirebaseAuth auth;

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

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
adap = new ArrayAdapter<DataClass>(this, R.layout.datadisplay, R.id.displayname, arrList);

auth = FirebaseAuth.getInstance();

database = FirebaseDatabase.getInstance().getReference().child("Testserverdata");
user = auth.getCurrentUser();

sendbutton = (Button) findViewById(R.id.serversendbutton);
sendmessage = (EditText) findViewById(R.id.serversendmessage);
data = (ListView) findViewById(R.id.serverdata);

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

if(sendmessage.getText().toString().isEmpty()) {

} else {
database.push().setValue(sendmessage.getText().toString());

sendmessage.setText("");
}
}
});

database.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String string = dataSnapshot.getValue(String.class);
String tmpname = user.getEmail();
String tmpmessage = string;
Uri image = user.getPhotoUrl();

arrList.add(new DataClass(tmpname, tmpmessage, image));

adap.notifyDataSetChanged();

data.setAdapter(adap);
}

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

}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

}

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

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

}
}

ChatAdapter.java

package com.glowchat.app;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class ChatAdapter extends BaseAdapter {

Context context;
ArrayList<DataClass> arrList;

public ChatAdapter(Context context, ArrayList<DataClass> arrList) {
this.context = context;
this.arrList = arrList;
}

@Override
public int getCount() {
return arrList.size();
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

convertView = LayoutInflater.from(context).inflate(R.layout.datadisplay, parent, false);

TextView name = (TextView) convertView.findViewById(R.id.displayname);
TextView message = (TextView) convertView.findViewById(R.id.displaymessage);
ImageView image = (ImageView) convertView.findViewById(R.id.profilepic);

name.setText(arrList.get(position).getName());
message.setText(arrList.get(position).getMessage());
image.setImageURI(arrList.get(position).getImage());

return convertView;
}

}

DataClass.java

package com.glowchat.app;

import android.net.Uri;

public class DataClass {

String name;
String message;
Uri image;

public DataClass(String name, String message, Uri image) {
this.name = name;
this.message = message;
this.image = image;

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Uri getImage() {
return image;
}

public void setImage(Uri image) {
this.image = image;
}

}
<小时/>

Firebase JSON 数据:

{
"Testserverdata" : {
"-LdXa2gqdIND7Pk6094N" : "Hello",
"-LdXbb_1Fvh0jxsC5zYx" : "u4witsit",
"-LdXblvyFdMW9XMnbYpW" : "jgug",
"-LdXtP9ZzjL_a4A4Yqqf" : "moin",
"-LdYDEBP-ikhIyYWnh3r" : "zgxcc",
"-LdYEwHcWLzUhM9Rjp4y" : "jfufuru",
"-LdYwkwKkuxm9plle4hZ" : "hcch",
"-LdZFhvuXuIJIMRSECNO" : "fjdvve",
"-LdZFjM7DMr9krCGGei4" : "Nsjabwbs",
"-LdZNpphDsXfG5JDGd1a" : "uxx"
}
}

最佳答案

当您从 Firebase 获取数据时,您正在执行以下操作:

database.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String string = dataSnapshot.getValue(String.class);
String tmpname = user.getEmail();
String tmpmessage = string;
Uri image = user.getPhotoUrl();

arrList.add(new DataClass(tmpname, tmpmessage, ));

adap.notifyDataSetChanged();

data.setAdapter(adap);
}

onDataChange 的第一行中,您将获取 dataSnapshot 的字符串值:dataSnapshot.getValue(String.class)。这意味着数据库中的任何 JSON 值或对象都会转换为字符串。

如果您有一个 JSON 对象,您更有可能希望执行 dataSnapshot.getValue(DataClass.class),然后从结果对象中获取正确的属性。

关于java - Android studio : CutomListView gives strange output, 它应该提供 firebase 数据。有什么解决办法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55891154/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com