Listview不会第一次填充

时间:2018-04-16 22:17:13

标签: android firebase listview firebase-realtime-database android-arrayadapter

我通过在打开的活动中填充ListView并在另一个活动中将其作为ArrayList打开来创建ListView

当我打开第二个活动时,我的ListView仅在我打开活动时填充,Get_Issue活动会打开空白。当我返回并重新进入活动时(第二次调用Get_Issue.class)。然后填充ListView

我猜在创建/填充listView之前屏幕正在加载?

如何改进我的代码,以便在我第一次调用Get_Issue活动时填充listView。我不想打开一个空的活动。

class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
    DatabaseReference databaseIssue;
    public static List<Issue> issueList = new ArrayList<>();
    public static List<Issue> getIssueList() {
        return issueList;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        getData = (Button) findViewById(R.id.getData);
        databaseIssue  = FirebaseDatabase.getInstance().getReference("Issue");
    }

    private void getTheData(){
        databaseIssue.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                issueList.clear();
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    Issue getData = postSnapshot.getValue(Issue.class);
                    issueList.add(getData);}}
        });
    }
    public void onClick(View view) {
        if(view == getData){
            getTheData();
            startActivity(new Intent(this, Get_Issue.class));
        }
    }
}

适配器类

public class Get_Issue extends AppCompatActivity {
    public void populateListView() {
        List<Issue> issues = ProfileActivity.getIssueList();

        ArrayAdapter<Issue> adapter = new ArrayAdapter<>(
                this,
                R.layout.da_item,
                issues);

        ListView list = findViewById(R.id.listView);
        list.setAdapter(adapter);
    }
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.issue_list);
        populateListView();
    }
}

0 个答案:

没有答案