gpt4 book ai didi

java - ListView 不显示来自后端数据库的数据

转载 作者:行者123 更新时间:2023-12-01 18:30:03 25 4
gpt4 key购买 nike

我正在 android studio 中构建一个联系人应用程序。所有其他事情(例如登录、注册)都在应用程序中运行。我没有从 android studio 的 ListView 中的无后端数据库获取数据。但我的 Activity 正在运行,并且应用程序内存在崩溃。有人可以帮忙吗?

view_contacts.java

package com.example.testapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.backendless.Backendless;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.exceptions.BackendlessFault;
import com.backendless.persistence.DataQueryBuilder;

import java.util.List;

public class view_contact extends AppCompatActivity {

ListView lvlist;
ContactAdapter adapter;
TextView contacts;

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

contacts=findViewById(R.id.tvcontacts);
lvlist=findViewById(R.id.list_view);

String whereClause="userEmail = '" + TestApplication.user.getEmail() +"'";
DataQueryBuilder queryBuilder=DataQueryBuilder.create();
queryBuilder.setWhereClause(whereClause);
queryBuilder.setGroupBy("name");

Backendless.Persistence.of(Contact.class).find(queryBuilder, new AsyncCallback<List<Contact>>() {
@Override
public void handleResponse(List<Contact> response) {

TestApplication.contacts = response;
adapter=new ContactAdapter(view_contact.this,TestApplication.contacts);
lvlist.setAdapter(adapter);

}

@Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(view_contact.this, "Error : "+fault.getMessage(), Toast.LENGTH_SHORT).show();
}
});



}
}

ContactsAdapter.java

package com.example.testapp;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

public class ContactAdapter extends ArrayAdapter<Contact> {


private Context context;
private List<Contact> contacts;

public ContactAdapter(@NonNull Context context, List<Contact> contacts) {
super(context,R.layout.row_layout);
this.context = context;
this.contacts = contacts;
}

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

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= inflater.inflate(R.layout.row_layout,parent,false);

TextView tvchar=convertView.findViewById(R.id.tvchar);
TextView tvname=convertView.findViewById(R.id.tvname);
TextView tvmail=convertView.findViewById(R.id.tvmail);

tvchar.setText(contacts.get(position).getName().toUpperCase().charAt(0));
tvname.setText(contacts.get(position).getName());
tvmail.setText(contacts.get(position).getEmail());

return convertView;

}
}

TestApplication.java

// I have set up my backendless database code above and my list is declared public below
public static List<Contact> contacts;

logcat:-

2020-02-12 18:27:55.078 13205-13229/com.example.testapp D/EGL_emulation: eglMakeCurrent: 0xa1107260: ver 2 0 (tinfo 0xa901b4f0)
2020-02-12 18:27:58.118 13205-13229/com.example.testapp D/EGL_emulation: eglMakeCurrent: 0xa1107260: ver 2 0 (tinfo 0xa901b4f0)
2020-02-12 18:27:58.138 13205-13229/com.example.testapp D/EGL_emulation: eglMakeCurrent: 0xa1107260: ver 2 0 (tinfo 0xa901b4f0)
2020-02-12 18:27:58.152 13205-13229/com.example.testapp D/EGL_emulation: eglMakeCurrent: 0xa1107260: ver 2 0 (tinfo 0xa901b4f0)
2020-02-12 18:27:58.170 13205-13229/com.example.testapp I/chatty: uid=10090(com.example.testapp) RenderThread identical 1 line
2020-02-12 18:27:58.178 13205-13229/com.example.testapp D/EGL_emulation: eglMakeCurrent: 0xa1107260: ver 2 0 (tinfo 0xa901b4f0)
2020-02-12 18:27:59.649 13205-13229/com.example.testapp D/EGL_emulation: eglMakeCurrent: 0xa1107260: ver 2 0 (tinfo 0xa901b4f0)

最佳答案

您的应用程序崩溃了吗?如果是,请提供崩溃日志。

您是否从服务器收到联系人列表?确保 Contact 表中的条目与您的 whereClause 匹配。

关于java - ListView 不显示来自后端数据库的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60180477/

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