我正在尝试将ratdatalist添加到名为ratData的rat_data的listview中,但它不起作用,有人可以告诉我为什么吗?
我正在制作一个数组列表,填充它然后将其添加到适配器,但是当我运行它时它不会出现
public class RatData extends AppCompatActivity implements View.OnClickListener {
ListView ratData;
Button addB;
Button mapB;
private final Activity thisActivity = this;
public static int args;
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
List<String> ratDatalist = new ArrayList<>();
/**
* automatically read csv file & displays keys
*
* @param savedInstanceState bundle object used upon creation
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rat_data);
System.out.println("reach here");
addB = (Button) findViewById(R.id.add);
mapB = (Button) findViewById(R.id.map);
addB.setOnClickListener(this);
mapB.setOnClickListener(this);
//ratData = (ListView) findViewById(R.id.ratData);
mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseDatabase = mFirebaseInstance.getReference("rats");
try {
DatabaseReference mFirebaseInstance1 = FirebaseDatabase.getInstance().getReference().child("rats");
mFirebaseInstance1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String ratkeysName = (String) ds.getKey();
ratDatalist.add(ratkeysName);
System.out.println("here");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
} catch(Exception e){
}
final ArrayAdapter adapter = new ArrayAdapter<String>(RatData.this,
R.layout.rat_data, ratDatalist);
ratData = (ListView) findViewById(R.id.ratData);
ratData.setAdapter(adapter);
System.out.println("hello");
}
我的问题确实达到了问候,列表正确填充,但它不会出现在应用中。
答案 0 :(得分:0)
你就是那样
public class RatData extends AppCompatActivity implements View.OnClickListener {
ListView ratData;
Button addB;
Button mapB;
private final Activity thisActivity = this;
public static int args;
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
List<String> ratDatalist = new ArrayList<>();
/**
* automatically read csv file & displays keys
*
* @param savedInstanceState bundle object used upon creation
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rat_data);
System.out.println("reach here");
addB = (Button) findViewById(R.id.add);
mapB = (Button) findViewById(R.id.map);
addB.setOnClickListener(this);
mapB.setOnClickListener(this);
final ArrayAdapter adapter = new ArrayAdapter<String>(RatData.this,
R.layout.rat_data, ratDatalist);
ratData = (ListView) findViewById(R.id.ratData);
ratData.setAdapter(adapter);
System.out.println("hello");
mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseDatabase = mFirebaseInstance.getReference("rats");
try {
DatabaseReference mFirebaseInstance1 = FirebaseDatabase.getInstance().getReference().child("rats");
mFirebaseInstance1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String ratkeysName = (String) ds.getKey();
ratDatalist.add(ratkeysName);
System.out.println("here");
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
} catch(Exception e){
}
}