ListView项目在启动时不会立即显示

时间:2017-03-15 00:42:53

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

我的程序有一个奇怪的故障。我试图关注使用firebase生成和显示listView项目tutorial

我的程序存在的问题是,在启动时我看不到任何列表项,但是当我开始创建一个新项目时,列表会填充所有以前的条目和我新创建的条目。

根据我对类似问题的研究,一直有人谈论使用notifydatasetchanged来更新我的listview,但是有些人说在调用ArrayAdapter时没有必要这样做,因为数据不断更新。

**CustomAdapter Class**

/**
 * Created by MarkB on 14/03/2017.
 */

public class CustomAdapter extends BaseAdapter {
    Context c;
    ArrayList<User> users;

    public CustomAdapter (Context c, ArrayList<User> users) {
        this.c = c;
        this.users = users;
    }

    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public Object getItem(int position) {
        return users.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (convertView==null)
        {
            convertView = LayoutInflater.from(c).inflate(R.layout.model,viewGroup,false);
        }

        //Find TextView items on the model.xml
        TextView nameTxt = (TextView) convertView.findViewById(R.id.nameTxt);
        TextView surnameTxt = (TextView) convertView.findViewById(R.id.surnameTxt);
        TextView ageTxt = (TextView) convertView.findViewById(R.id.ageTxt);
        TextView weightTxt = (TextView) convertView.findViewById(R.id.weightTxt);
        TextView genderTxt = (TextView) convertView.findViewById(R.id.genderTxt);

        final User u = (User) this.getItem(position);

        nameTxt.setText(u.getName());
        surnameTxt.setText(u.getSurname());
        ageTxt.setText(u.getAge());
        weightTxt.setText(u.getWeight());
        genderTxt.setText(u.getGender());

        //ONITEMCLICK
        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(c,"Hi " + u.getName(), Toast.LENGTH_SHORT).show();

                Intent startMenuActivity = new Intent(c, Menu_Activity.class);
                c.startActivity(startMenuActivity);
            }
        });

        return convertView;
    }
}

** Home_Screen_Activity Class *

public class Home_Screen_Activity extends AppCompatActivity {

    DatabaseReference db;
    FirebaseHelper helper;
    CustomAdapter adapter;
    ListView lv;
    EditText nameTxt, surnameTxt, ageTxt, weightTxt;
    Spinner genderSpinner;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        lv = (ListView) findViewById(R.id.listView);

        //INITIALISE FIREBASE DB
        db = FirebaseDatabase.getInstance().getReference();
        helper = new FirebaseHelper(db);

        //ADAPTER
        adapter = new CustomAdapter(this, helper.retrieve());
        lv.setAdapter(adapter);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                displayInputDialog();
            }

        });
    }

    //DISPLAY INPUT DIALOG
    private void displayInputDialog() {
        final Dialog d = new Dialog(this);
        d.setTitle("Save To Firebase");
        d.setContentView(R.layout.input_dialog);

        nameTxt = (EditText) d.findViewById(R.id.nameEditText);
        surnameTxt = (EditText) d.findViewById(R.id.surnameEditText);
        ageTxt = (EditText) d.findViewById(R.id.ageEditText);
        weightTxt = (EditText) d.findViewById(R.id.weightEditText);
        genderSpinner = (Spinner) d.findViewById(R.id.genderSpinner);

        Button saveBtn = (Button) d.findViewById(R.id.saveBtn);

        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //GET DATA
                String name = nameTxt.getText().toString();
                String surname = surnameTxt.getText().toString();
                String age = ageTxt.getText().toString();
                String weight = weightTxt.getText().toString();
                String gender = genderSpinner.getSelectedItem().toString();


                //SET DATA
                User u = new User();
                u.setName(name);
                u.setSurname(surname);
                u.setAge(age);
                u.setWeight(weight);
                u.setGender(gender);

                //SIMPLE VALIDATION
                if (name != null && name.length() > 0) {
                    //THEN SAVE
                    if (helper.save(u)) {
                        //IF SAVED CLEAR EDITTXT
                        nameTxt.setText("");
                        surnameTxt.setText("");
                        ageTxt.setText("");
                        weightTxt.setText("");
                        genderSpinner.setSelection(0);


                        adapter = new CustomAdapter(Home_Screen_Activity.this, helper.retrieve());
                        lv.setAdapter(adapter);

                        d.dismiss();
                    }
                }
                else
                {
                    Toast.makeText(Home_Screen_Activity.this, "Name Must Not Be Empty", Toast.LENGTH_SHORT).show();
                }
            }
        });
        d.show();
    }
}

感谢任何帮助。老实说,我不明白这个小故障的来源。我的logcat中没有提到这个小故障

0 个答案:

没有答案