我正在构建一个Android应用,当用户以技术人员的身份创建其帐户时,他的姓名ID显示在listview
中,其他用户没有firebase
outh.the问题是我无法在listview
中显示技术人员的数据。
这是我的firebase
数据库
这是我的代码,返回0项或空白listview
。我尝试了几件事,但无法显示技术人员的信息。任何人请帮助我,我是firebase
中的新人。
public class Technician extends Activity {
ListView listView;
ArrayList<String> tecnames = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_technician);
ImageButton Back_Btn = (ImageButton) findViewById(R.id.back);
Back_Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Technician.this, Technician_main.class);
startActivity(i);
finish();
}
});
listView = (ListView) findViewById(R.id.list_item);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(Technician.this, Book_Now.class);
startActivity(intent);
finish();
}
});
Technician.CostumAdapter costumAdapter = new Technician.CostumAdapter();
listView.setAdapter(costumAdapter);
GetItems();
}
private class CostumAdapter extends BaseAdapter {
@Override
public int getCount() {
return tecnames.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.cuctom_listview, null);
TextView txt_one = (TextView) view.findViewById(R.id.skill_txt);
txt_one.setText(tecnames.get(i));
return view;
}
}
private void GetItems() {
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = database.child("user");
final Query itemsQuery = ref.orderByChild("name");
itemsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
Pojo item = singleSnapshot.getValue(Pojo.class);
tecnames.add(String.valueOf(item));
Log.e(TAG,"value of name"+itemsQuery);
}
}else {
Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Log.e(TAG, "Returning " + tecnames.size() + " items");
}
}
答案 0 :(得分:0)
而不是:
Technician.CostumAdapter costumAdapter = new Technician.CostumAdapter();
写下这个:
Technician.CostumAdapter costumAdapter = new Technician.CostumAdapter(getApplicationContext(),tecnames);
然后在类中添加此构造函数:
private ArrayList<String> datas;
Context mContext;
public CustomAdapter(Context context, ArrayList<String> datas) {
super(context, R.layout.row_item, datas);
this.mContext=context;
this.datas = datas;
}
将addListenerforSingleValueEvent
更改为addValueEventListener
答案 1 :(得分:0)
好的,你需要修理的东西很少:
1.您的数组列表tecnames应该是您的POJO类
ArrayList<POJO> tecnames = new ArrayList<>();
现在将tecnames.add(String.valueOf(item));
更改为tecnames.add(item);
2.正如@Peter Haddad建议的那样,您需要将此列表作为数据集提供给您的适配器。
3.Firebase数据库侦听器是异步的,因此您需要在获取数据集(列表)中的数据后通知适配器。在foreach循环之后在onDataChange
内添加以下代码:
costumAdapter.notifyDataSetChanged();
您可以参考this link获得第3点。