好的,所以我在我的 android 應用程式中有一個 listView 來顯示訊息,自從我及時得到幫助后一切都很好,但現在我遇到了一個錯誤,最后一條訊息不會顯示。
訊息部分不會顯示它是否是“系統”訊息。這與firebase有關。
但是當我得到資料時,除了訊息型別之外,它對每個專案都有效。
// this is what the message looks like in the database
"messages": "hi",
"number": "1",
"pfp": "",
"time": "2022-04-22T00:39:00.520Z",
// this tells the app what type of message it is
"type": "user",
"user_uid": "user id",
"username": "frugalnormal"
甚至切換到recycleview和同樣的事情
注意:我只是舉杯來告訴我它是否認為該訊息是系統的,并且每次在此處輸入影像描述時,我都會發送一條“用戶”訊息,它告訴我它是系統訊息并且不會顯示該部分但仍然可以顯示時間
時間也應該出現在短信上
更新: 好的,所以我已經厭倦了現在最后一條訊息顯示的所有內容,但應用程式仍然認為最后一條訊息始終是“系統”訊息
而且由于某種原因,開關以某種方式回傳“用戶”和“系統”
update2: 所以現在我真的很困惑,滾動它會使系統訊息的事情在不同的訊息上隨機發生
更新 3 //////////////////////////////////////// ////////////////////////// 整個配接器的代碼:
這是為了將資料應用于視圖:
_ch_child_listener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot _param1, String _param2) {
GenericTypeIndicator<HashMap<String, Object>> _ind = new GenericTypeIndicator<HashMap<String, Object>>() {};
final String _childKey = _param1.getKey();
final HashMap<String, Object> _childValue = _param1.getValue(_ind);
ch.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot _dataSnapshot) {
lop = new ArrayList<>();
try {
GenericTypeIndicator<HashMap<String, Object>> _ind = new GenericTypeIndicator<HashMap<String, Object>>() {};
for (DataSnapshot _data : _dataSnapshot.getChildren()) {
HashMap<String, Object> _map = _data.getValue(_ind);
lop.add(_map);
}
}
catch (Exception _e) {
_e.printStackTrace();
}
listview2.setAdapter(new Listview2Adapter(lop));
((BaseAdapter)listview2.getAdapter()).notifyDataSetChanged();
x.add(_childKey);
}
這是其余的:
public class Listview2Adapter extends BaseAdapter {
ArrayList<HashMap<String, Object>> _data;
public Listview2Adapter(ArrayList<HashMap<String, Object>> _arr) {
_data = _arr;
}
@Override
public int getCount() {
return _data.size();
}
@Override
public HashMap<String, Object> getItem(int _index) {
return _data.get(_index);
}
@Override
public long getItemId(int _index) {
return _index;
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public View getView(final int _position, View _v, ViewGroup _container) {
LayoutInflater _inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.messgebubble, listview2, false);
}
final LinearLayout time = _view.findViewById(R.id.time);
final TextView textview1 = _view.findViewById(R.id.textview1);
final LinearLayout linear4 = _view.findViewById(R.id.linear4);
final TextView timetext = _view.findViewById(R.id.timetext);
final LinearLayout left = _view.findViewById(R.id.left);
final LinearLayout linear1 = _view.findViewById(R.id.linear1);
final de.hdodenhof.circleimageview.CircleImageView circleimageview1 = _view.findViewById(R.id.circleimageview1);
final TextView message = _view.findViewById(R.id.message);
final ImageView imageview1 = _view.findViewById(R.id.imageview1);
final TextView SystemText = _view.findViewById(R.id.systemtext);
final LinearLayout system = _view.findViewById(R.id.system);
Instant i = Instant.parse(_data.get(_position).get("time").toString());
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withZone(ZoneId.systemDefault());;
String Time = dateTimeFormatter.format(i);
timetext.setText(Time);
String messageType = lop.get(_position).get("type").toString();
switch (messageType){
case "system":
SystemText.setText(_data.get(_position).get("messages").toString());
Toast.makeText(GroupChatActiviy.this, "This is a system message", Toast.LENGTH_SHORT).show();
system.setVisibility(View.VISIBLE);
break;
case "user":
linear4.setVisibility(View.VISIBLE);
if (_data.get((int)_position).get("messages").toString().equals("")) {
if (_data.get((int)_position).containsKey("image")) {
imageview1.setVisibility(View.VISIBLE);
if (_data.get((int)_position).get("user_uid").toString().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
linear4.setGravity(Gravity.RIGHT);
linear1.setGravity(Gravity.RIGHT);
}
else {
linear4.setGravity(Gravity.LEFT);
linear1.setGravity(Gravity.LEFT);
}
}
else {
}
}
else {
message.setText(_data.get((int)_position).get("messages").toString());
message.setVisibility(View.VISIBLE);
if (lop.get((int)_position).get("user_uid").toString().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
linear4.setGravity(Gravity.RIGHT);
linear1.setGravity(Gravity.RIGHT);
linear1.setBackgroundResource(R.drawable.start);
}
else {
linear4.setGravity(Gravity.LEFT);
linear1.setGravity(Gravity.LEFT);
linear1.setBackgroundResource(R.drawable.right);
}
if (_data.get((int)_position).containsKey("image")) {
imageview1.setVisibility(View.VISIBLE);
}
else {
}
}
String LastTimeText = _data.get((int)_position - 1).get("time").toString();
Instant LastI = Instant.parse(LastTimeText);
Duration Dur = Duration.between(LastI, i);
if (Dur.getSeconds() <= 600) {
}
else {
if (lop.get((int)_position).get("user_uid").toString().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
}
else {
circleimageview1.setVisibility(View.VISIBLE);
textview1.setVisibility(View.VISIBLE);
//Glide.with(getApplicationContext()).load(Uri.parse(_data.get((int)_position).get("pfp").toString())).into(circleimageview1);
textview1.setText(_data.get((int)_position).get("username").toString());
}
time.setVisibility(View.VISIBLE);
}
break;
}
if(_data.get((int)_position).get("number") != null){
if (_data.get((int)_position).get("number").toString().equals("")) {
}
else {
if (Numberrr < Double.parseDouble(_data.get((int)_position).get("number").toString())) {
Numberrr = Double.parseDouble(_data.get((int)_position).get("number").toString()) 1;
}
}
}
return _view;
}
}
uj5u.com熱心網友回復:
您的配接器基于,_data但訊息型別是從lop. 隨著新資料的進入,這兩者可能會定期不同步。確保您的配接器僅從其中訪問行專案資訊,_data并且它應該按預期作業。
uj5u.com熱心網友回復:
問題可能是可見性,每當專案渲染時,我們應該始終像您可以創建組一樣隱藏所有內容user,system并首先隱藏它的所有視圖。在每個系結視圖上,我們將首先隱藏所有內容,并且根據打開資料將使特定的所有視圖可見并且然后進行資料設定。
在配接器的 get View 方法中執行以下代碼
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public View getView(final int _position, View _v, ViewGroup _container) {
LayoutInflater _inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.messgebubble, listview2, false);
}
final LinearLayout time = _view.findViewById(R.id.time);
final TextView textview1 = _view.findViewById(R.id.textview1);
final LinearLayout linear4 = _view.findViewById(R.id.linear4);
final TextView timetext = _view.findViewById(R.id.timetext);
final LinearLayout left = _view.findViewById(R.id.left);
final LinearLayout linear1 = _view.findViewById(R.id.linear1);
final de.hdodenhof.circleimageview.CircleImageView circleimageview1 = _view.findViewById(R.id.circleimageview1);
final TextView message = _view.findViewById(R.id.message);
final ImageView imageview1 = _view.findViewById(R.id.imageview1);
final TextView SystemText = _view.findViewById(R.id.systemtext);
final LinearLayout system = _view.findViewById(R.id.system);
time.setVisibility(View.GONE);
textview1.setVisibility(View.GONE);
linear4.setVisibility(View.GONE);
timetext.setVisibility(View.GONE);
left.setVisibility(View.GONE);
linear1.setVisibility(View.GONE);
circleimageview1.setVisibility(View.GONE);
message.setVisibility(View.GONE);
imageview1.setVisibility(View.GONE);
SystemText.setVisibility(View.GONE);
system.setVisibility(View.GONE);
Instant i = Instant.parse(_data.get(_position).get("time").toString());
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withZone(ZoneId.systemDefault());;
String Time = dateTimeFormatter.format(i);
timetext.setText(Time);
String messageType = lop.get(_position).get("type").toString();
switch (messageType){
case "system":
SystemText.setText(_data.get(_position).get("messages").toString());
Toast.makeText(GroupChatActiviy.this, "This is a system message", Toast.LENGTH_SHORT).show();
system.setVisibility(View.VISIBLE);
break;
case "user":
linear4.setVisibility(View.VISIBLE);
if (_data.get((int)_position).get("messages").toString().equals("")) {
if (_data.get((int)_position).containsKey("image")) {
imageview1.setVisibility(View.VISIBLE);
if (_data.get((int)_position).get("user_uid").toString().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
linear4.setGravity(Gravity.RIGHT);
linear1.setGravity(Gravity.RIGHT);
}
else {
linear4.setGravity(Gravity.LEFT);
linear1.setGravity(Gravity.LEFT);
}
}
else {
}
}
else {
message.setText(_data.get((int)_position).get("messages").toString());
message.setVisibility(View.VISIBLE);
if (lop.get((int)_position).get("user_uid").toString().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
linear4.setGravity(Gravity.RIGHT);
linear1.setGravity(Gravity.RIGHT);
linear1.setBackgroundResource(R.drawable.start);
}
else {
linear4.setGravity(Gravity.LEFT);
linear1.setGravity(Gravity.LEFT);
linear1.setBackgroundResource(R.drawable.right);
}
if (_data.get((int)_position).containsKey("image")) {
imageview1.setVisibility(View.VISIBLE);
}
else {
}
}
String LastTimeText = _data.get((int)_position - 1).get("time").toString();
Instant LastI = Instant.parse(LastTimeText);
Duration Dur = Duration.between(LastI, i);
if (Dur.getSeconds() <= 600) {
}
else {
if (lop.get((int)_position).get("user_uid").toString().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
}
else {
circleimageview1.setVisibility(View.VISIBLE);
textview1.setVisibility(View.VISIBLE);
//Glide.with(getApplicationContext()).load(Uri.parse(_data.get((int)_position).get("pfp").toString())).into(circleimageview1);
textview1.setText(_data.get((int)_position).get("username").toString());
}
time.setVisibility(View.VISIBLE);
}
break;
}
if(_data.get((int)_position).get("number") != null){
if (_data.get((int)_position).get("number").toString().equals("")) {
}
else {
if (Numberrr < Double.parseDouble(_data.get((int)_position).get("number").toString())) {
Numberrr = Double.parseDouble(_data.get((int)_position).get("number").toString()) 1;
}
}
}
return _view;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/467684.html
