我想在列表视图中添加名称节点,如何在listview中添加单个壁炉节点?我尝试了几件事,但无法做到,请有人告诉我该怎么做,这是我的代码,我收到错误 txt_one。的setText(tecnames);它说它应该是char
这是我的火力库数据库:
代码
public class Technician extends Activity {
private DatabaseReference mDatabase;
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, Skills.class);
startActivity(i);
finish();
}
});
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("user").child("name");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Pojo pojo=new Pojo();
tecnames.add(pojo.getName(value));
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
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);
}
}
这是我的BaseAdapter
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);
return view;
}
}
答案 0 :(得分:1)
当您使用database.getReference("user").child("name")
附加监听器时,它会将监听器附加到/user/name
。从您分享的屏幕截图中,该位置不存在。
如果您知道要收听的用户的UID,则可以附加一个监听器:
DatabaseReference myRef = database.getReference("user").child(uid).child("name");
myRef.addValueEventListener(new ValueEventListener() {
或更短:
DatabaseReference myRef = database.getReference("user/"+uid+"/name");
myRef.addValueEventListener(new ValueEventListener() {
另一方面,如果您不知道UID,则必须侦听整个/user
节点,然后导航到{{{}中的正确用户及其name
属性。 1}}回调:
onDataChange
需要DatabaseReference myRef = database.getReference("user");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String value = childSnapshot.getValue(String.class);
Pojo pojo=new Pojo();
tecnames.add(pojo.getName(value));
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
以上的循环,因为dataSnapshot.getChildren()
下可能有多个用户。在这种情况下,快照包含这些用户节点的列表。