我试图填充我的ListView,但我似乎无法解决这个问题。我能够打印这些值并且还能够吐司,然后他们就不会填充我的ListView了。有没有人可以帮助我填充它?
MainActivity
package uk.co.jaunt_app.jaunt;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.crashlytics.android.answers.Answers;
import com.crashlytics.android.answers.CustomEvent;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import static uk.co.jaunt_app.jaunt.R.id.Posts;
public class MainActivity extends AppCompatActivity{
private FloatingActionButton addmap;
private Button settingbtn;
private TextView mNameTextView;
private TextView mEmailTextView;
ListView mPosts;
ArrayAdapter<String> adapter;
private FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.jauntlogodark);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, Posts);
Firebase.setAndroidContext(this);
Firebase ref = new Firebase(Config.FIREBASE_URL);
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
String uid = currentFirebaseUser.getUid();
Firebase usersRef = ref.child("Users").child(uid).child("name");
Firebase emailRef = ref.child("Users").child(uid).child("email");
Firebase postRef = ref.child("Users").child(uid).child("Maps");
mNameTextView = (TextView) findViewById(R.id.NameTextView);
mPosts = (ListView) findViewById(Posts);
postRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (final DataSnapshot postSnapshot : snapshot.getChildren()) {
//Getting the data from snapshot
Person person = postSnapshot.getValue(Person.class);
String posts = "Name: " + person.getMapName() + "\n\n";
Toast.makeText(getApplicationContext(),
"Name: " + person.getMapName(), Toast.LENGTH_LONG)
.show();
mPosts.setAdapter(adapter);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
mEmailTextView = (TextView) findViewById(R.id.EmailTextView);
emailRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String email = dataSnapshot.getValue(String.class);
mEmailTextView.setText(email);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
addmap = (FloatingActionButton) findViewById(R.id.add_map);
addmap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Answers.getInstance().logCustom(new CustomEvent("Added Map"));
Intent intent = new Intent(MainActivity.this, MapStartActivity.class);
startActivity(intent);
}
});
settingbtn = (Button) findViewById(R.id.settingbtn);
settingbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
});
}
}
答案 0 :(得分:3)
1)移动
mPosts.setAdapter(adapter);
到这里
mPosts = (ListView) findViewById(Posts);
mPosts.setAdapter(adapter);
2)执行此操作以实际显示数据
String mapName = person.getMapName();
adapter.add(mapName);
3)替换
android.R.layout.simple_dropdown_item_1line
使用
android.R.layout.android simple_list_item_1
答案 1 :(得分:0)
将您的适配器添加到列表视图
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.jauntlogodark);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, Posts);
Firebase.setAndroidContext(this);
Firebase ref = new Firebase(Config.FIREBASE_URL);
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
String uid = currentFirebaseUser.getUid();
Firebase usersRef = ref.child("Users").child(uid).child("name");
Firebase emailRef = ref.child("Users").child(uid).child("email");
Firebase postRef = ref.child("Users").child(uid).child("Maps");
mNameTextView = (TextView) findViewById(R.id.NameTextView);
mPosts = (ListView) findViewById(Posts);
mPosts.setAdapter(adapter);